Leggere ricorsivamente i siti di SharePoint tramite il Client Object Model
Una esempio in C# per leggere, ricorsivamente, i siti di SharePoint OnLine:
C#
//https://www.sgart.it
using Microsoft.SharePoint.Client;
static void Main(string[] args)
{
string userName = "xxx@xxx.onmicrosoft.com";
string password = "???";
var webs = LoadWebsTree("https://sgart.sharepoint.local", userName, password);
foreach (var w in webs)
{
Console.WriteLine(w.Key + " | " + w.Value);
}
Console.ReadLine();
}
private static Dictionary<string, string> LoadWebsTree(string webUrl, string userName, string password)
{
Dictionary<string, string> result = new Dictionary<string, string>();
using (ClientContext ctx = GetContext(webUrl, userName, password))
{
// cari co i dati del sito root
Web web = ctx.Web;
ctx.Load(web, w => w.Title, w => w.Url);
ctx.ExecuteQuery();
result.Add(web.Url, web.Title);
// leggo i sottositi
LoadSubWebs(ctx, web, result);
}
return result;
}
private static void LoadSubWebs(ClientContext ctx, Web parentWeb, Dictionary<string, string> result)
{
// carico i dati dei sottositi
ctx.Load(parentWeb, w => w.Webs, w => w.Title, w => w.Url);
ctx.ExecuteQuery();
foreach (Web web in parentWeb.Webs)
{
result.Add(web.Url, web.Title);
// leggo i sottositi
LoadSubWebs(ctx, web, result);
}
}
private static ClientContext GetContext(string webUrl, string userName, string password)
{
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}
ClientContext ctx = new ClientContext(webUrl);
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword);
return ctx;
}