Question

I am trying to access the Root site list using the sub site client context. But when I try to execute the clientcontext it gives Access Denied error.

I tried to create new clientcontext by passing the accessToken as well but no success. (Referred: This)

Below is the code:

clientContext.Load(clientContext.Site);
clientContext.ExecuteQuery();
string siteUrl = clientContext.Site.Url;
Uri webUri = new Uri(siteUrl);
string realm = TokenHelper.GetRealmFromTargetUrl(webUri);
string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, webUri.Authority, realm).AccessToken;
using (var cc = TokenHelper.GetClientContextWithAccessToken(siteUrl, accessToken))
{
    cc.Load(cc.Web);
    cc.ExecuteQuery();
}

Here, clientContext is the context of Sub-Site and cc is the context of Root site.

How can I access the cc using clientContext?

Was it helpful?

Solution

Found a solution!!

We can access the Root site lists from subsite using below.

clientContext.Site.RootWeb.Lists

No need to create a new context of the Root site.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top