Question

        using (ClientContext clientContext = new ClientContext("https://company.sharepoint.com/mysite"))
        {
            string password = pass;

            string userName = abc@contoso.com;

            SecureString secureString = new SecureString();
            password.ToList().ForEach(secureString.AppendChar);
            clientContext.Credentials = new SharePointOnlineCredentials(userName, secureString);
            Web web = clientContext.Web;
            clientContext.Load(web.Webs, we => we.Include(w => w.Url, w => w.Title));
            clientContext.ExecuteQuery();
            WebCollection site = web.Webs;
            foreach (Web w in site)
            {
                Console.WriteLine(w.Title);
                Console.WriteLine(w.Url);
            }
        }

The account I use has Visitor Read permission in mysite, when I run the above code, it throws the following error:

Access denied. You do not have permission to perform this action or access this resource.

However, if I any browser to open that site, I have no problem viewing the content and its sub-sites.

So, what permission do I need for this account to make my code works?

Était-ce utile?

La solution

You need to have Site collection admin or owner to execute the above code.

However, if you get the results that you view in the UI, i.e permission trimmed list of subsites, then you can use Web.GetSubwebsForCurrentUser method to fetch that data.

You can modify the below mentioned sample code:

SecureString secureString = new SecureString();
password.ToList().ForEach(secureString.AppendChar);
clientContext.Credentials = new SharePointOnlineCredentials(userName, secureString);
Web web = clientContext.Web;

WebCollection collWeb = clientContext.Web.GetSubwebsForCurrentUser(null);

clientContext.Load(collWeb, we => we.Include(w => w.Title, w => w.Url));
clientContext.ExecuteQuery();

foreach (Web w in collWeb)
{
    Console.WriteLine(w.Title);
    Console.WriteLine(w.Url);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top