I had site collection is SharePoint online with the below hierarchy.
The value in the bracket is the site hierarchy (added for understanding only). I want to get all the subsite details along with Hierarchy level. I am able to get all the subsite details with the below code.
using (var clientContext = new ClientContext(destinationSiteUrl))
{
clientContext.Credentials = new SharePointOnlineCredentials(UserName, secure);
DataTable dtsubsites = new DataTable();
Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite.Webs);
clientContext.ExecuteQuery();
Dictionary<int, Web> mydict = new Dictionary<int, Web>();
var allWebs = new List<Web>();
var objallWebs = LoadAllWebs(clientContext.Site.RootWeb, allWebs);
foreach (var web in objallWebs)
{
ddlsubsites.Items.Add(web.Url.ToString());
}
}
public List<Web> LoadAllWebs(Web web, List<Web> allWebs)
{
var ctx = web.Context;
ctx.Load(web);
ctx.Load(web.Webs);
ctx.ExecuteQuery();
allWebs.Add(web);
foreach (var subWeb in web.Webs)
{
LoadAllWebs(subWeb, allWebs);
}
return allWebs;
}
How to get the hierarchy level of each site? Any Suggestions, please.