public static void PublishDocuments()
{
string siteURL = "http://demo/pwa";
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
using (var clientContext = new ClientContext(siteURL))
{
// SharePoint Online Credentials
clientContext.Credentials = new NetworkCredential(userName, securePassword);
// Get the SharePoint web
Web web = clientContext.Web;
clientContext.Load(web, website => website.Webs, website => website.Title);
// Execute the query to the server
clientContext.ExecuteQuery();
// Loop through all the webs
foreach (Web subWeb in web.Webs)
{
string serverRelativeUrl = "/pwa/" + subWeb.Title;
var serverrelativeUrlOfTheFile = serverRelativeUrl + "/Documents";//这里给每个网站添加Documents文档库
Console.WriteLine(subWeb.Title);
if (!TryCheckDocumentLibrary(subWeb, serverrelativeUrlOfTheFile))
{
ListCreationInformation lci = new ListCreationInformation();
lci.Description = "Documents";
lci.Title = "Documents";
lci.TemplateType = 101;
List newLib = subWeb.Lists.Add(lci);
clientContext.Load(newLib);
clientContext.ExecuteQuery();
}
}
}
}
}
//检测该网站是否有该文档库方法
public static bool TryCheckDocumentLibrary(Web web, string serverRelativeUrl)
{
var ctx = web.Context;
try
{
List list = web.GetList(serverRelativeUrl);
ctx.Load(list);
ctx.ExecuteQuery();
return true;
}
catch (Microsoft.SharePoint.Client.ServerException ex)
{
if (ex.ServerErrorTypeName == "System.IO.FileNotFoundException")
{
return false;
}
else
throw;
}
}
CSOM批量添加项目网站的文档库
最新推荐文章于 2018-12-31 22:16:08 发布