.net sharepoint文档库操作

时间:2024-04-05 16:34:54
 /// <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中删除文档库

库-》库设置-》删除此文档库