.net sharepoint文档库操作
/// <summary>
/// 检查文档库
/// </summary>
/// <param name="siteUrl"></param>
/// <param name="listName"></param>
/// <returns></returns>
public static bool IsSPDocExists(string mainSiteUrl,string siteUrl, string documentName)
{ using (SPSite site = new SPSite(mainSiteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList(documentName);
if (list != null)
{
return true;
}
return false; }
}
else
{
foreach (var item in site.AllWebs.ToList())
{
if (item.Url == siteUrl)
{ //当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
SPList list = web.Lists.TryGetList(documentName);
if (list != null)
{
return true;
}
return false; }
}
}
}
return false;
} }
检查文档库
/// <summary>
/// 创建文档库
/// </summary>
/// <param name="siteUrl"></param>
/// <param name="documentName"></param>
/// <returns></returns>
public static string CreateDocumentLibrary(string mainSiteUrl, string siteUrl, string documentName)
{
string result = "false"; SPSecurity.RunWithElevatedPrivileges(delegate
{
try
{ if (SPSite.Exists(new Uri(mainSiteUrl)))
{
using (SPSite site = new SPSite(mainSiteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{ web.AllowUnsafeUpdates = true;
SPList spdoc = web.Lists.TryGetList(documentName);
if (spdoc == null)
{
web.Lists.Add(documentName, "上传附件文档库", SPListTemplateType.DocumentLibrary); }
web.Update();
web.AllowUnsafeUpdates = false;
result = "true";
}
}
else
{
var allWebs = site.AllWebs.ToList();
foreach (var item in allWebs)
{
if (item.Url == siteUrl)
{
//当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
web.AllowUnsafeUpdates = true;
SPList spdoc = web.Lists.TryGetList(documentName);
if (spdoc == null)
{
web.Lists.Add(documentName, "上传附件文档库", SPListTemplateType.DocumentLibrary); }
web.Update();
web.AllowUnsafeUpdates = false;
result = "true";
} }
}
}
}
}
result = "true";
}
catch (Exception ex)
{
result = "CreateDocumentLibrary|" + ex.Message;
} }); return result; }
创建文档库
//子文件夹断继承,并remove掉所有组
public static void SetFolderPermission(string mainSiteUrl,string siteUrl,string documentName, string folderName)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(siteUrl)))
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{ web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
SPFolder folder = null;
for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == folderName)
{
folder = sPList.RootFolder.SubFolders[i];
}
}
folder.Item.BreakRoleInheritance(true);
SPGroupCollection groupCollection = web.Groups;
foreach (SPGroup var in groupCollection)
{
if (!var.Name.Contains("项目文档查看"))
{
//remove掉除(项目文档查看)组的所有其他组
folder.Item.RoleAssignments.Remove(var); }
}
//移除掉所有人员权限
SPUserCollection userCollection = web.Users;
foreach (SPUser user in userCollection)
{
folder.Item.RoleAssignments.Remove(user);
}
sPList.Update();
web.AllowUnsafeUpdates = false;
}
}
}
}); } public static void SetFolderPermissionClearly(string mainSiteUrl, string siteUrl, string documentName, string folderName)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(siteUrl)))
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{ web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
SPFolder folder = null;
for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == folderName)
{
folder = sPList.RootFolder.SubFolders[i];
}
}
folder.Item.BreakRoleInheritance(true);
SPGroupCollection groupCollection = web.Groups;
foreach (SPGroup var in groupCollection)
{
folder.Item.RoleAssignments.Remove(var);
}
//移除掉所有人员权限
SPUserCollection userCollection = web.Users;
foreach (SPUser user in userCollection)
{
folder.Item.RoleAssignments.Remove(user);
}
sPList.Update();
web.AllowUnsafeUpdates = false;
}
}
}
}); } public static void DeleteSubFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName, string strFolderNameSub)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(siteUrl)))
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true; for (int i = ; i < sPList.RootFolder.SubFolders[strFolderName].SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[strFolderName].SubFolders[i].Name == strFolderNameSub)
{
sPList.RootFolder.SubFolders[strFolderName].SubFolders[i].Delete();
}
}
sPList.Update();
web.AllowUnsafeUpdates = false;
}
}
}
});
} public static void CreateFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName, string currentUser)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(mainSiteUrl)))
{
using (SPSite site = new SPSite(mainSiteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true;
for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
{
return;
}
}
SPListItem sPListItem = sPList.Items.Add(sPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
sPListItem["上传者"] = currentUser;
sPListItem.Update();
sPList.Update();
web.AllowUnsafeUpdates = false;
}
}
}
});
} public static void CreateSubFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName, string strFolderNameSub)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(siteUrl)))
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true; for (int i = ; i < sPList.RootFolder.SubFolders[strFolderName].SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[strFolderName].SubFolders[i].Name == strFolderNameSub)
{
return;
}
}
SPListItem sPListItemSub = sPList.Items.Add(sPList.RootFolder.SubFolders[strFolderName].ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderNameSub);
sPList.Update();
sPListItemSub.Update();
web.AllowUnsafeUpdates = false;
}
}
}
});
}
其他
/// <summary>
/// 删除文件夹
/// </summary>
/// <param name="documentName">文档库名</param>
/// <param name="strFolderName">文件夹名</param>
public static void DeleteFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(siteUrl)))
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true; for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
{
sPList.RootFolder.SubFolders[i].Delete();
}
}
sPList.Update();
web.AllowUnsafeUpdates = false;
}
}
}
});
}
删除文件夹
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="documentName">文档库名</param>
/// <param name="strFolderName">文件夹名</param>
public static void CreateFolder(string mainSiteUrl,string siteUrl,string documentName, string strFolderName)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(mainSiteUrl)))
{
using (SPSite site = new SPSite(mainSiteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true;
for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
{
return;
}
}
SPListItem sPListItem = sPList.Items.Add(sPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
sPList.Update();
sPListItem.Update();
web.AllowUnsafeUpdates = false;
}
}
else
{
var allWebs = site.AllWebs.ToList();
foreach (var item in allWebs)
{
if (item.Url == siteUrl)
{
//当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
web.AllowUnsafeUpdates = true;
SPList sPList = web.Lists[documentName];
sPList.EnableFolderCreation = true;
for (int i = ; i < sPList.RootFolder.SubFolders.Count; i++)
{
if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
{
return;
}
}
SPListItem sPListItem = sPList.Items.Add(sPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
sPList.Update();
sPListItem.Update();
web.AllowUnsafeUpdates = false;
} }
}
} }
}
});
}
创建文件夹
/// <summary>
/// 单个附件上传
/// </summary>
/// <param name="documentName"></param>
/// <param name="folderName"></param>
/// <param name="folderSubName"></param>
/// <param name="file"></param>
/// <param name="filename"></param>
public static string SaveToFormLibrary(string mainSiteUrl, string siteUrl, string documentName, string folderName, byte[] file, string filename)
{
string ff = "";
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(mainSiteUrl)))
{
// Add the file to a document library
using (SPSite site = new SPSite(mainSiteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
//web.Folders[documentName].SubFolders[folderName].Files.Add(filename, file, true);
SPFile files = web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Add(filename, file, true);
ff = files.ServerRelativeUrl;
web.AllowUnsafeUpdates = false;
web.Close();
}
}
else
{
var allWebs = site.AllWebs.ToList();
foreach (var item in allWebs)
{
if (item.Url == siteUrl)
{
//当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
web.AllowUnsafeUpdates = true;
//web.Folders[documentName].SubFolders[folderName].Files.Add(filename, file, true);
SPFile files = web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Add(filename, file, true);
ff = files.ServerRelativeUrl;
web.AllowUnsafeUpdates = false;
web.Close();
} }
}
} site.Close();
}
}
});
return ff;
}
单个附件上传
/// <summary>
/// 单个附件删除
/// </summary>
/// <param name="documentName"></param>
/// <param name="folderName"></param>
/// <param name="folderSubName"></param>
/// <param name="file"></param>
/// <param name="filename"></param>
public static void DeleteFile(string mainSiteUrl, string siteUrl, string documentName, string folderName, string urlOfFile)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
if (SPSite.Exists(new Uri(mainSiteUrl)))
{
// Add the file to a document library
using (SPSite site = new SPSite(mainSiteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true; try
{
web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Delete(urlOfFile);
}
catch
{ }
web.AllowUnsafeUpdates = false;
web.Close(); }
}
else
{
var allWebs = site.AllWebs.ToList();
foreach (var item in allWebs)
{
if (item.Url == siteUrl)
{
//当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
web.AllowUnsafeUpdates = true; try
{
web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Delete(urlOfFile);
}
catch
{ }
web.AllowUnsafeUpdates = false;
web.Close();
} }
}
} site.Close();
}
}
});
}
单个附件删除
public static string UploadFile(string mainSiteUrl, string siteUrl, string documentName, string folderName, byte[] file, string filename)
{
string result = "false";
string ff = "";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
using (SPSite site = new SPSite(siteUrl))
{
if (mainSiteUrl == siteUrl)
{
using (SPWeb web = site.OpenWeb())
{
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true; SPList list = web.Lists[documentName];
string templateDispName = folderName;
SPFolder folder = null;
try
{
folder = list.RootFolder.SubFolders[templateDispName];
}
catch (Exception)
{
folder = list.RootFolder.SubFolders.Add(templateDispName);
folder.Update();
} SPFile newFile = folder.Files.Add(filename, file, true);
newFile.Update();
ff = newFile.ServerRelativeUrl;
site.AllowUnsafeUpdates = false;
web.AllowUnsafeUpdates = false;
web.Close(); result = "true";
}
}
else
{
var allWebs = site.AllWebs.ToList();
foreach (var item in allWebs)
{
if (item.Url == siteUrl)
{
//当前子站
using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
{
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true; SPList list = web.Lists[documentName];
string templateDispName = folderName;
SPFolder folder = null;
try
{
folder = list.RootFolder.SubFolders[templateDispName];
}
catch (Exception)
{
folder = list.RootFolder.SubFolders.Add(templateDispName);
folder.Update();
} SPFile newFile = folder.Files.Add(filename, file, true);
newFile.Update();
ff = newFile.ServerRelativeUrl;
site.AllowUnsafeUpdates = false;
web.AllowUnsafeUpdates = false;
web.Close(); result = "true";
} }
}
}
}
}
catch (Exception ex)
{
result = "error|(UploadFile)" + ex.Message;
}
});
return ff + "~" + result;
}
mainSiteUrl="http://192.168.110.51:8000/";
SiteUrl="http://192.168.110.51:8000/PPCS/";
sharepoint中删除文档库
库-》库设置-》删除此文档库
.net sharepoint文档库操作的更多相关文章
- 在Outlook中查看预览SharePoint文档库的文档
本文概况 阅读时间: 约2分钟 适用版本:SharePoint Server 2010及以上 面向用户:普通用户,管理员 难度指数:★★☆☆☆ 在日常工作中,总有一些常用的文档需要经常打开查看,其实我 ...
- SharePoint 文档库实现文件夹拖放到文档库
打开文档库-> 选择文件夹-> 在Ribbon中选择“库(list)”-> 在右边可以看到打开方式-> 选择用资源管理器打开-> 在新打开的资源管理器中可能实现对文夹的拖 ...
- 360安全卫士造成Sharepoint文档库”使用资源管理器打开“异常
备注:企业用户还是少用360为妙 有客户反馈:部门里的XP SP2环境客户机全部异常,使用资源管理器打开Sharepoint文档库,看到的界面样式很老土,跟本地文件夹不一样 ...
- 修改Sharepoint 文档库列表点击Excel文件默认跳转到Excel Service服务 xlviewer.aspx页面
在Sharepoint 文档库中,当点击库中的一个Excel文件时,Sharepoint默认为转跳到Excel Services上,无论是Sharepoint 的是否开启了Excel Service, ...
- SharePoint文档库,如何在新窗口打开中的文件
默认情况下,点击文档库中的文件是在当前浏览器中打开的(如果你设置的是在客户端软件打开,则不符合本文情况).那么如果让他在新窗口中打开呢? 这里需要借助jQuery,关于如何将jQuery集成到Shar ...
- 如何为SharePoint文档库、文件夹、文件单独设置权限
在这里使用截图的方式简单描述两个问题:设置SharePoint Server文档库权限和文档库中的文件夹权限 一.设置SharePoint Server文档库权限 Figure 1 - 打开文档库后, ...
- SharePoint文档库文件夹特殊字符转义
当我们在SharePoint网站文档库中新建文件夹时包含了~ " # % & * : < > ? / \ { | }字符时(一共15个), 或者以.开头或者结束,或者包含 ...
- 解决SharePoint文档库文件在搜索结果页面显示的标题和文档的标题不一致问题(search result)
问题表现: SharePoint 2013 爬网后,搜索一个文档,虽然搜到了,但是显示有点问题,如图: 原因分析: 造成该问题的原因是,该文档除了本身有一个名称外,在文档metadata的title属 ...
- 解决SharePoint 文档库itemadded eventhandler导致的上传完成后,编辑页面保持报错的问题,错误信息为“该文档已经被编辑过 the file has been modified by...”
在文档库中添加itemadded 后,在上传文件后,会自动打开文档属性的编辑页面,在保存的时候就会报错,说这个文档已经被编辑过了.这是应为默认itemadded实践是异步执行的,会在edit页面打开之 ...
随机推荐
- NDO to PNP( ndoutils to PNP4Nagios)
How to use this script The aim of this script is to import your ndo database directly into PNP4nagio ...
- 【Java布局】FlowLayout布局时设定组件大小
默认的JPanel中,采用的是FlowLayout布局 下面是api中的定义: JPanel(boolean isDoubleBuffered) 创建具有 FlowLayout 和 ...
- Ubuntu 14.04 英文系统 安装中文搜狗输入法
ubuntu默认的输入法是ibus框架,而搜狗输入法是基于fcitx的框架,因此需要先安装fcitx框架. STEP1: 在Ubuntu Software Center 搜索fcitx,安装fcitx ...
- Device nodes and device stacks
[Device nodes and device stacks] 链接:https://msdn.microsoft.com/en-us/library/windows/hardware/ff5547 ...
- 预习笔记 多态 --S2 4.3
第三章 多态 polymorphism 多态 instance 例子override 重载 constructor 构造器ClassCastException 类型转换异常upcasting 上抛 d ...
- 当前界面最上面添加视图(UIWimdow)
-(void)windowAddPushVc:(UIView *)pushView WithTitle:(NSString *)title { UIWindow *window = [UIApplic ...
- Android之RecyclerView(一)
概述 RecyclerView 是一个 ViewGroup,它用于渲染任何基于适配器的 View.它被官方定义为 ListView 和 GridView 的取代者,是在 Support V7 包中引入 ...
- table边框单线的实现方法
1.实现方法一: <table border="0" cellspacing="1" style=" 实现原理:利用table的单元 ...
- silverlight控件阴影效果示例
<ScrollViewer MaxHeight="400" VerticalScrollBarVisibility="Auto" HorizontalSc ...
- atoi()函数
原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...