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

            string userName = [email protected];

            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?

1 Answer 1

0

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);
}
1
  • I also have another site on the same domain, located in company.sharepoint.com/sites/dev (notice the /sites/). The method above does not load this site. What method should I use to load it?
    – Jack Le
    Commented Mar 5, 2019 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.