Unable to Clone SharePoint Context from tenant to Specific Site Collection in Azure AD App

Copper Contributor

I Created a web application with Azure AD authentication using Azure AD Application. I had granted all permission for SharePoint Online, Graph API and Active Directory to the App for provisioning Site collections(creation and Deletion) which is working fine.


In my web application, I am accessing the site collections through tenant Context(https://tenant-admin.sharepoint.com) using access tokens from the Azure AD App. I am trying to switch the client context of tenant to Site Collection  Context using the clone method in  OfficeDevPnP.Core. Below is the code sample.


              var tenant = new Tenant(spcontext);
                var site = tenant.GetSiteByUrl(webUrl);
                string pageName = "mypage.aspx";
                ClientSidePage page = ClientSidePage.Load(spcontext, pageName);
                var components = page.AvailableClientSideComponents();
                string wpName = "Intranet Manager";
                List<string> componentsnames = components.Select(k => k.Name).ToList();
                var webPartToAdd = components.Where(wp => wp.ComponentType == 1 && wp.Name == wpName).FirstOrDefault();
                if (webPartToAdd != null)
                    ClientSideWebPart clientWp = new ClientSideWebPart(webPartToAdd) { Order = -1 };

I am getting the error I am Getting the error "The remote server returned an error: (401) Unauthorized." at the line ClientSidePage page = ClientSidePage.Load(newSiteContext, pageName);


If we pass user credentials with the necessary permission for creating the tenant Context, the clone was working as fine. When we tried to the same with the access tokens we are getting the above error. The Azure AD App is given all required permission in the Azure portal.

0 Replies