.net sharepoint文档库操作

 
 1 /// <summary>
 2         /// 检查文档库
 3         /// </summary>
 4         /// <param name="siteUrl"></param>
 5         /// <param name="listName"></param>
 6         /// <returns></returns>
 7         public static bool IsSPDocExists(string mainSiteUrl,string siteUrl, string documentName)
 8         {
 9 
10             using (SPSite site = new SPSite(mainSiteUrl))
11             {
12                 if (mainSiteUrl == siteUrl)
13                 {
14                     using (SPWeb web = site.OpenWeb())
15                     {
16                         SPList list = web.Lists.TryGetList(documentName);
17                         if (list != null)
18                         {
19                             return true;
20                         }
21                         return false;
22 
23                     }
24                 }
25                 else
26                 {
27                     foreach (var item in site.AllWebs.ToList())
28                     {
29                         if (item.Url == siteUrl)
30                         {
31                             
32                             //当前子站
33                             using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
34                             {
35                                 SPList list  = web.Lists.TryGetList(documentName);
36                                 if (list != null)
37                                 {
38                                     return true;
39                                 }
40                                 return false;
41 
42                             }
43                         }
44                     }
45                 }
46                 return false;
47             }
48            
49         }
检查文档库
 1 /// <summary>
 2         /// 创建文档库
 3         /// </summary>
 4         /// <param name="siteUrl"></param>
 5         /// <param name="documentName"></param>
 6         /// <returns></returns>
 7         public static string CreateDocumentLibrary(string mainSiteUrl, string siteUrl, string documentName)
 8         {
 9             string result = "false";
10 
11             SPSecurity.RunWithElevatedPrivileges(delegate
12             {
13                 try
14                 {
15 
16                     if (SPSite.Exists(new Uri(mainSiteUrl)))
17                     {
18                         using (SPSite site = new SPSite(mainSiteUrl))
19                         {
20                             if (mainSiteUrl == siteUrl)
21                             {
22                                 using (SPWeb web = site.OpenWeb())
23                                 {
24 
25                                     web.AllowUnsafeUpdates = true;
26                                     SPList spdoc = web.Lists.TryGetList(documentName);
27                                     if (spdoc == null)
28                                     {
29                                         web.Lists.Add(documentName, "上传附件文档库", SPListTemplateType.DocumentLibrary);
30 
31                                     }
32                                     web.Update();
33                                     web.AllowUnsafeUpdates = false;
34                                     result = "true";
35                                 }
36                             }
37                             else
38                             {
39                                 var allWebs = site.AllWebs.ToList();
40                                 foreach (var item in allWebs)
41                                 {
42                                     if (item.Url == siteUrl)
43                                     {
44                                         //当前子站
45                                         using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
46                                         {
47                                             web.AllowUnsafeUpdates = true;
48                                             SPList spdoc = web.Lists.TryGetList(documentName);
49                                             if (spdoc == null)
50                                             {
51                                                 web.Lists.Add(documentName, "上传附件文档库", SPListTemplateType.DocumentLibrary);
52 
53                                             }
54                                             web.Update();
55                                             web.AllowUnsafeUpdates = false;
56                                             result = "true";
57                                         }
58 
59                                     }
60                                 }
61                             }
62                         }
63                     }
64                     result = "true";
65                 }
66                 catch (Exception ex)
67                 {
68                     result = "CreateDocumentLibrary|" + ex.Message;
69                 }
70 
71             });
72 
73             return result;
74 
75         }
创建文档库
  1 //子文件夹断继承,并remove掉所有组
  2         public static void SetFolderPermission(string mainSiteUrl,string siteUrl,string documentName, string folderName)
  3         {
  4             SPSecurity.RunWithElevatedPrivileges(delegate
  5             {
  6                 if (SPSite.Exists(new Uri(siteUrl)))
  7                 {
  8                     using (SPSite site = new SPSite(siteUrl))
  9                     {
 10                         using (SPWeb web = site.OpenWeb())
 11                         {
 12 
 13                             web.AllowUnsafeUpdates = true;
 14                             SPList sPList = web.Lists[documentName];
 15                             SPFolder folder = null;
 16                             for (int i = 0; i < sPList.RootFolder.SubFolders.Count; i++)
 17                             {
 18                                 if (sPList.RootFolder.SubFolders[i].Name == folderName)
 19                                 {
 20                                     folder = sPList.RootFolder.SubFolders[i];
 21                                 }
 22                             }
 23                             folder.Item.BreakRoleInheritance(true);
 24                             SPGroupCollection groupCollection = web.Groups;
 25                             foreach (SPGroup var in groupCollection)
 26                             {
 27                                 if (!var.Name.Contains("项目文档查看"))
 28                                 {
 29                                     //remove掉除(项目文档查看)组的所有其他组
 30                                     folder.Item.RoleAssignments.Remove(var);
 31 
 32                                 }
 33                             }
 34                             //移除掉所有人员权限
 35                             SPUserCollection userCollection = web.Users;
 36                             foreach (SPUser user in userCollection)
 37                             {
 38                                 folder.Item.RoleAssignments.Remove(user);
 39                             }
 40                             sPList.Update();
 41                             web.AllowUnsafeUpdates = false;
 42                         }
 43                     }
 44                 }
 45             });
 46 
 47         }
 48 
 49         public static void SetFolderPermissionClearly(string mainSiteUrl, string siteUrl, string documentName, string folderName)
 50         {
 51             SPSecurity.RunWithElevatedPrivileges(delegate
 52             {
 53                 if (SPSite.Exists(new Uri(siteUrl)))
 54                 {
 55                     using (SPSite site = new SPSite(siteUrl))
 56                     {
 57                         using (SPWeb web = site.OpenWeb())
 58                         {
 59 
 60                             web.AllowUnsafeUpdates = true;
 61                             SPList sPList = web.Lists[documentName];
 62                             SPFolder folder = null;
 63                             for (int i = 0; i < sPList.RootFolder.SubFolders.Count; i++)
 64                             {
 65                                 if (sPList.RootFolder.SubFolders[i].Name == folderName)
 66                                 {
 67                                     folder = sPList.RootFolder.SubFolders[i];
 68                                 }
 69                             }
 70                             folder.Item.BreakRoleInheritance(true);
 71                             SPGroupCollection groupCollection = web.Groups;
 72                             foreach (SPGroup var in groupCollection)
 73                             {
 74                                 folder.Item.RoleAssignments.Remove(var);
 75                             }
 76                             //移除掉所有人员权限
 77                             SPUserCollection userCollection = web.Users;
 78                             foreach (SPUser user in userCollection)
 79                             {
 80                                 folder.Item.RoleAssignments.Remove(user);
 81                             }
 82                             sPList.Update();
 83                             web.AllowUnsafeUpdates = false;
 84                         }
 85                     }
 86                 }
 87             });
 88 
 89         }
 90 
 91 
 92         public static void DeleteSubFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName, string strFolderNameSub)
 93         {
 94             SPSecurity.RunWithElevatedPrivileges(delegate
 95             {
 96                 if (SPSite.Exists(new Uri(siteUrl)))
 97                 {
 98                     using (SPSite site = new SPSite(siteUrl))
 99                     {
100                         using (SPWeb web = site.OpenWeb())
101                         {
102                             web.AllowUnsafeUpdates = true;
103                             SPList sPList = web.Lists[documentName];
104                             sPList.EnableFolderCreation = true;
105 
106 
107                             for (int i = 0; i < sPList.RootFolder.SubFolders[strFolderName].SubFolders.Count; i++)
108                             {
109                                 if (sPList.RootFolder.SubFolders[strFolderName].SubFolders[i].Name == strFolderNameSub)
110                                 {
111                                     sPList.RootFolder.SubFolders[strFolderName].SubFolders[i].Delete();
112                                 }
113                             }
114                             sPList.Update();
115                             web.AllowUnsafeUpdates = false;
116                         }
117                     }
118                 }
119             });
120         }
121         
122         public static void CreateFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName, string currentUser)
123         {
124             SPSecurity.RunWithElevatedPrivileges(delegate
125             {
126                 if (SPSite.Exists(new Uri(mainSiteUrl)))
127                 {
128                     using (SPSite site = new SPSite(mainSiteUrl))
129                     {
130                         using (SPWeb web = site.OpenWeb())
131                         {
132                             web.AllowUnsafeUpdates = true;
133                             SPList sPList = web.Lists[documentName];
134                             sPList.EnableFolderCreation = true;
135                             for (int i = 0; i < sPList.RootFolder.SubFolders.Count; i++)
136                             {
137                                 if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
138                                 {
139                                     return;
140                                 }
141                             }
142                             SPListItem sPListItem = sPList.Items.Add(sPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
143                             sPListItem["上传者"] = currentUser;
144                             sPListItem.Update();
145                             sPList.Update();
146                             web.AllowUnsafeUpdates = false;
147                         }
148                     }
149                 }
150             });
151         }
152 
153         public static void CreateSubFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName, string strFolderNameSub)
154         {
155             SPSecurity.RunWithElevatedPrivileges(delegate
156             {
157                 if (SPSite.Exists(new Uri(siteUrl)))
158                 {
159                     using (SPSite site = new SPSite(siteUrl))
160                     {
161                         using (SPWeb web = site.OpenWeb())
162                         {
163                             web.AllowUnsafeUpdates = true;
164                             SPList sPList = web.Lists[documentName];
165                             sPList.EnableFolderCreation = true;
166 
167                             for (int i = 0; i < sPList.RootFolder.SubFolders[strFolderName].SubFolders.Count; i++)
168                             {
169                                 if (sPList.RootFolder.SubFolders[strFolderName].SubFolders[i].Name == strFolderNameSub)
170                                 {
171                                     return;
172                                 }
173                             }
174                             SPListItem sPListItemSub = sPList.Items.Add(sPList.RootFolder.SubFolders[strFolderName].ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderNameSub);
175                             sPList.Update();
176                             sPListItemSub.Update();
177                             web.AllowUnsafeUpdates = false;
178                         }
179                     }
180                 }
181             });
182         }
其他
 1 /// <summary>
 2         /// 删除文件夹
 3         /// </summary>
 4         /// <param name="documentName">文档库名</param>
 5         /// <param name="strFolderName">文件夹名</param>
 6         public static void DeleteFolder(string mainSiteUrl, string siteUrl, string documentName, string strFolderName)
 7         {
 8             SPSecurity.RunWithElevatedPrivileges(delegate
 9             {
10                 if (SPSite.Exists(new Uri(siteUrl)))
11                 {
12                     using (SPSite site = new SPSite(siteUrl))
13                     {
14                         using (SPWeb web = site.OpenWeb())
15                         {
16                             web.AllowUnsafeUpdates = true;
17                             SPList sPList = web.Lists[documentName];
18                             sPList.EnableFolderCreation = true;
19 
20 
21                             for (int i = 0; i < sPList.RootFolder.SubFolders.Count; i++)
22                             {
23                                 if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
24                                 {
25                                     sPList.RootFolder.SubFolders[i].Delete();
26                                 }
27                             }
28                             sPList.Update();
29                             web.AllowUnsafeUpdates = false;
30                         }
31                     }
32                 }
33             });
34         }
删除文件夹
 1  /// <summary>
 2         /// 创建文件夹
 3         /// </summary>
 4         /// <param name="documentName">文档库名</param>
 5         /// <param name="strFolderName">文件夹名</param>
 6         public static void CreateFolder(string mainSiteUrl,string siteUrl,string documentName, string strFolderName)
 7         {
 8             SPSecurity.RunWithElevatedPrivileges(delegate
 9             {
10                 if (SPSite.Exists(new Uri(mainSiteUrl)))
11                 {
12                     using (SPSite site = new SPSite(mainSiteUrl))
13                     {
14                         if (mainSiteUrl == siteUrl)
15                         {
16                             using (SPWeb web = site.OpenWeb())
17                             {
18                                 web.AllowUnsafeUpdates = true;
19                                 SPList sPList = web.Lists[documentName];
20                                 sPList.EnableFolderCreation = true;
21                                 for (int i = 0; i < sPList.RootFolder.SubFolders.Count; i++)
22                                 {
23                                     if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
24                                     {
25                                         return;
26                                     }
27                                 }
28                                 SPListItem sPListItem = sPList.Items.Add(sPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
29                                 sPList.Update();
30                                 sPListItem.Update();
31                                 web.AllowUnsafeUpdates = false;
32                             }
33                         }
34                         else
35                         {
36                             var allWebs = site.AllWebs.ToList();
37                             foreach (var item in allWebs)
38                             {
39                                 if (item.Url == siteUrl)
40                                 {
41                                     //当前子站
42                                     using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
43                                     {
44                                         web.AllowUnsafeUpdates = true;
45                                         SPList sPList = web.Lists[documentName];
46                                         sPList.EnableFolderCreation = true;
47                                         for (int i = 0; i < sPList.RootFolder.SubFolders.Count; i++)
48                                         {
49                                             if (sPList.RootFolder.SubFolders[i].Name == strFolderName)
50                                             {
51                                                 return;
52                                             }
53                                         }
54                                         SPListItem sPListItem = sPList.Items.Add(sPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strFolderName);
55                                         sPList.Update();
56                                         sPListItem.Update();
57                                         web.AllowUnsafeUpdates = false;
58                                     }
59 
60                                 }
61                             }
62                         }
63                         
64                     }
65                 }
66             });
67         }
68         
创建文件夹
 1 /// <summary>
 2         /// 单个附件上传
 3         /// </summary>
 4         /// <param name="documentName"></param>
 5         /// <param name="folderName"></param>
 6         /// <param name="folderSubName"></param>
 7         /// <param name="file"></param>
 8         /// <param name="filename"></param>
 9         public static string SaveToFormLibrary(string mainSiteUrl, string siteUrl, string documentName, string folderName, byte[] file, string filename)
10         {
11             string ff = "";
12             SPSecurity.RunWithElevatedPrivileges(delegate
13             {
14                 if (SPSite.Exists(new Uri(mainSiteUrl)))
15                 {
16                     // Add the file to a document library
17                     using (SPSite site = new SPSite(mainSiteUrl))
18                     {
19                         if (mainSiteUrl == siteUrl)
20                         {
21                             using (SPWeb web = site.OpenWeb())
22                             {
23                                 web.AllowUnsafeUpdates = true;
24                                 //web.Folders[documentName].SubFolders[folderName].Files.Add(filename, file, true);
25                                 SPFile files = web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Add(filename, file, true);
26                                 ff = files.ServerRelativeUrl;
27                                 web.AllowUnsafeUpdates = false;
28                                 web.Close();
29                             }
30                         }
31                         else
32                         {
33                             var allWebs = site.AllWebs.ToList();
34                             foreach (var item in allWebs)
35                             {
36                                 if (item.Url == siteUrl)
37                                 {
38                                     //当前子站
39                                     using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
40                                     {
41                                         web.AllowUnsafeUpdates = true;
42                                         //web.Folders[documentName].SubFolders[folderName].Files.Add(filename, file, true);
43                                         SPFile files = web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Add(filename, file, true);
44                                         ff = files.ServerRelativeUrl;
45                                         web.AllowUnsafeUpdates = false;
46                                         web.Close();
47                                     }
48 
49                                 }
50                             }
51                         }
52                         
53                         site.Close();
54                     }
55                 }
56             });
57             return ff;
58         }
单个附件上传
 1 /// <summary>
 2         /// 单个附件删除 
 3         /// </summary>
 4         /// <param name="documentName"></param>
 5         /// <param name="folderName"></param>
 6         /// <param name="folderSubName"></param>
 7         /// <param name="file"></param>
 8         /// <param name="filename"></param>
 9         public static void DeleteFile(string mainSiteUrl, string siteUrl, string documentName, string folderName, string urlOfFile)
10         {
11             SPSecurity.RunWithElevatedPrivileges(delegate
12             {
13                 if (SPSite.Exists(new Uri(mainSiteUrl)))
14                 {
15                     // Add the file to a document library
16                     using (SPSite site = new SPSite(mainSiteUrl))
17                     {
18                         if (mainSiteUrl == siteUrl)
19                         {
20                             using (SPWeb web = site.OpenWeb())
21                             {
22                                 web.AllowUnsafeUpdates = true;
23                                 
24                                 try
25                                 {
26                                     web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Delete(urlOfFile);
27                                 }
28                                 catch
29                                 {
30 
31                                 }
32                                 web.AllowUnsafeUpdates = false;
33                                 web.Close();
34 
35                             }
36                         }
37                         else
38                         {
39                             var allWebs = site.AllWebs.ToList();
40                             foreach (var item in allWebs)
41                             {
42                                 if (item.Url == siteUrl)
43                                 {
44                                     //当前子站
45                                     using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
46                                     {
47                                         web.AllowUnsafeUpdates = true;
48 
49                                         try
50                                         {
51                                             web.Lists[documentName].RootFolder.SubFolders[folderName].Files.Delete(urlOfFile);
52                                         }
53                                         catch
54                                         {
55 
56                                         }
57                                         web.AllowUnsafeUpdates = false;
58                                         web.Close();
59                                     }
60 
61                                 }
62                             }
63                         }
64                         
65                         site.Close();
66                     }
67                 }
68             });
69         }
单个附件删除
 1 public static string UploadFile(string mainSiteUrl, string siteUrl, string documentName, string folderName, byte[] file, string filename)
 2         {
 3             string result = "false";
 4             string ff = "";
 5             SPSecurity.RunWithElevatedPrivileges(delegate()
 6             { 
 7                 try
 8                 {
 9                     using (SPSite site = new SPSite(siteUrl))
10                     {
11                         if (mainSiteUrl == siteUrl)
12                         {
13                             using (SPWeb web = site.OpenWeb())
14                             {
15                                 site.AllowUnsafeUpdates = true;
16                                 web.AllowUnsafeUpdates = true;
17 
18                                 SPList list = web.Lists[documentName];
19                                 string templateDispName = folderName;
20                                 SPFolder folder = null;
21                                 try
22                                 {
23                                     folder = list.RootFolder.SubFolders[templateDispName];
24                                 }
25                                 catch (Exception)
26                                 {
27                                     folder = list.RootFolder.SubFolders.Add(templateDispName);
28                                     folder.Update();
29                                 }
30 
31                                 SPFile newFile = folder.Files.Add(filename, file, true);
32                                 newFile.Update();
33                                 ff = newFile.ServerRelativeUrl;
34                                 site.AllowUnsafeUpdates = false;
35                                 web.AllowUnsafeUpdates = false;
36                                 web.Close();
37 
38                                 result = "true";
39                             }
40                         }
41                         else
42                         {
43                             var allWebs = site.AllWebs.ToList();
44                             foreach (var item in allWebs)
45                             {
46                                 if (item.Url == siteUrl)
47                                 {
48                                     //当前子站
49                                     using (SPWeb web = site.OpenWeb(item.ServerRelativeUrl))
50                                     {
51                                         site.AllowUnsafeUpdates = true;
52                                         web.AllowUnsafeUpdates = true;
53 
54                                         SPList list = web.Lists[documentName];
55                                         string templateDispName = folderName;
56                                         SPFolder folder = null;
57                                         try
58                                         {
59                                             folder = list.RootFolder.SubFolders[templateDispName];
60                                         }
61                                         catch (Exception)
62                                         {
63                                             folder = list.RootFolder.SubFolders.Add(templateDispName);
64                                             folder.Update();
65                                         }
66 
67                                         SPFile newFile = folder.Files.Add(filename, file, true);
68                                         newFile.Update();
69                                         ff = newFile.ServerRelativeUrl;
70                                         site.AllowUnsafeUpdates = false;
71                                         web.AllowUnsafeUpdates = false;
72                                         web.Close();
73 
74                                         result = "true";
75                                     }
76 
77                                 }
78                             }
79                         }
80                     }
81                 }
82                 catch (Exception ex)
83                 {
84                     result = "error|(UploadFile)" + ex.Message;
85                 }
86             });
87             return ff + "~" + result;
88         }

 

 

mainSiteUrl="http://192.168.110.51:8000/";

SiteUrl="http://192.168.110.51:8000/PPCS/";

 

sharepoint中删除文档库 

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

转载于:https://www.cnblogs.com/King-JJ/p/5552969.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值