I am trying to get a list of subsites that are accessible to the user. I was initially using,
var query = context.Web.Webs.OrderBy(x => x.ServerRelativeUrl)
var sites = context.LoadQuery(query);
clientContext.ExecuteQuery()
But I was getting UnauthorizedException. So I tried using the GetSubwebsForCurrentUser,
WebCollection webs = clientContext.Web.GetSubwebsForCurrentUser(null);
clientContext.Load(webs);
clientContext.ExecuteQuery();
However, here as well I am getting an Unauthorized Exception. Why am I getting an Unauthorized when GetSubwebsForCurrentUser should only return sites where current user has access?
Am I doing something wrong? Is there another way to achieve this?
Full Code:
IEnumerable<Web> GetAllWebs(String username, String password, String url)
{
using (ClientContext clientContext = new ClientContext(url))
{
SecureString passWord = new SecureString();
foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(username, passWord);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
Console.WriteLine(web.Title);
WebCollection webs = clientContext.Web.GetSubwebsForCurrentUser(null);
clientContext.Load(webs);
clientContext.ExecuteQuery();
return webs;
}
}