如何使用 .NET 中的 CSOM 计算 SharePoint 网站中的子网站数量?
How to Count number of Subsites in a SharePoint Site using CSOM in .NET?
我想计算共享点中单个站点中可用的子站点、组和列表的数量,并将其显示在列表中。如何使用 CSOM 在 .NET 中执行此操作。我已经试过了,但这是行不通的。
Tenant tenant = new Tenant(ctx);
//SiteProperties sites = tenant.GetSitePropertiesByUrl("https://developer19.sharepoint.com/sites/codesite",true);
var sites = tenant.GetSiteProperties(0, true);
sites.Context.Load(sites);
sites.Context.ExecuteQuery();
foreach( var siteProp in sites)
{
Console.WriteLine("Site Collection '{0}' has '{1}' subwebs", siteProp.Url, siteProp.WebsCount);
}
最后我执行了这段代码并得到了站点中的子站点数量:)
using System;
using System.Security;
using Microsoft.SharePoint.Client;
namespace GetAllSubsites
{
class Program
{
static void Main(string[] args)
{
string userName = "kailash@kailash.cf";
string siteURL = "https://developer19.sharepoint.com/sites/codesite";
SecureString password = new SecureString();
foreach (char c in "pAsSwOrD".ToCharArray())
password.AppendChar(c);
using (var clientContext = new ClientContext(siteURL))
{
clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
Web web = clientContext.Web;
clientContext.Load(web, website => website.Webs);
clientContext.ExecuteQuery();
int b = 0;
foreach (Web subWeb in web.Webs)
{
if (subWeb.Url.Contains(siteURL))
b += 1;
}
Console.WriteLine("Total Number of Subsite is " + b);
}
Console.ReadLine();
}
}
}
以下CSOM C#代码供您参考。
public static int subSiteCount = 0;
public static int groupCount = 0;
public static int listCount = 0;
static void Main(string[] args)
{
string siteUrl = "https://tenant.sharepoint.com/sites/team";
string userName = "test@tenant.onmicrosoft.com";
string password = "xxx";
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
var credential = new SharePointOnlineCredentials(userName, securePassword);
GetAllSubWebs(siteUrl, userName, securePassword);
Console.WriteLine("Sub Site Count:"+subSiteCount+" Group Count:"+groupCount+" List Count:"+listCount);
}
private static void GetAllSubWebs(string path, string userName, SecureString password)
{
// ClienContext - Get the context for the SharePoint Online Site
using (var clientContext = new ClientContext(path))
{
// SharePoint Online Credentials
clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
// 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)
{
// Check whether it is an app URL or not - If not then get into this block
if (subWeb.Url.Contains(path))
{
string newpath = subWeb.Url;
GetAllSubWebs(newpath, userName, password);
clientContext.Load(subWeb.SiteGroups);
clientContext.Load(subWeb.Lists);
clientContext.ExecuteQuery();
subSiteCount += 1;
groupCount += subWeb.SiteGroups.Count;
listCount += subWeb.Lists.Count;
//Console.WriteLine("GroupCount:" + subWeb.SiteGroups.Count);
//Console.WriteLine("ListCount:" + subWeb.Lists.Count);
//Console.WriteLine(subWeb.Title + "-------" + subWeb.Url);
}
}
}
}
我想计算共享点中单个站点中可用的子站点、组和列表的数量,并将其显示在列表中。如何使用 CSOM 在 .NET 中执行此操作。我已经试过了,但这是行不通的。
Tenant tenant = new Tenant(ctx);
//SiteProperties sites = tenant.GetSitePropertiesByUrl("https://developer19.sharepoint.com/sites/codesite",true);
var sites = tenant.GetSiteProperties(0, true);
sites.Context.Load(sites);
sites.Context.ExecuteQuery();
foreach( var siteProp in sites)
{
Console.WriteLine("Site Collection '{0}' has '{1}' subwebs", siteProp.Url, siteProp.WebsCount);
}
最后我执行了这段代码并得到了站点中的子站点数量:)
using System;
using System.Security;
using Microsoft.SharePoint.Client;
namespace GetAllSubsites
{
class Program
{
static void Main(string[] args)
{
string userName = "kailash@kailash.cf";
string siteURL = "https://developer19.sharepoint.com/sites/codesite";
SecureString password = new SecureString();
foreach (char c in "pAsSwOrD".ToCharArray())
password.AppendChar(c);
using (var clientContext = new ClientContext(siteURL))
{
clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
Web web = clientContext.Web;
clientContext.Load(web, website => website.Webs);
clientContext.ExecuteQuery();
int b = 0;
foreach (Web subWeb in web.Webs)
{
if (subWeb.Url.Contains(siteURL))
b += 1;
}
Console.WriteLine("Total Number of Subsite is " + b);
}
Console.ReadLine();
}
}
}
以下CSOM C#代码供您参考。
public static int subSiteCount = 0;
public static int groupCount = 0;
public static int listCount = 0;
static void Main(string[] args)
{
string siteUrl = "https://tenant.sharepoint.com/sites/team";
string userName = "test@tenant.onmicrosoft.com";
string password = "xxx";
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
var credential = new SharePointOnlineCredentials(userName, securePassword);
GetAllSubWebs(siteUrl, userName, securePassword);
Console.WriteLine("Sub Site Count:"+subSiteCount+" Group Count:"+groupCount+" List Count:"+listCount);
}
private static void GetAllSubWebs(string path, string userName, SecureString password)
{
// ClienContext - Get the context for the SharePoint Online Site
using (var clientContext = new ClientContext(path))
{
// SharePoint Online Credentials
clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
// 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)
{
// Check whether it is an app URL or not - If not then get into this block
if (subWeb.Url.Contains(path))
{
string newpath = subWeb.Url;
GetAllSubWebs(newpath, userName, password);
clientContext.Load(subWeb.SiteGroups);
clientContext.Load(subWeb.Lists);
clientContext.ExecuteQuery();
subSiteCount += 1;
groupCount += subWeb.SiteGroups.Count;
listCount += subWeb.Lists.Count;
//Console.WriteLine("GroupCount:" + subWeb.SiteGroups.Count);
//Console.WriteLine("ListCount:" + subWeb.Lists.Count);
//Console.WriteLine(subWeb.Title + "-------" + subWeb.Url);
}
}
}
}