I had Create 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.
Now i am trying to add Spfx Web part to a Modern page. I followed the Msdn article Customizing Modern site pages which is working fine with user Context.
But 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.
Now i want to know how to add spfx webpart to modern page of a specific site collection using the tenant context
Below is the code that i am trying to add the spfx webpart to modern pages using Tenant context.
var tenant = new Tenant(spcontext);
spcontext.Load(tenant);
spcontext.ExecuteQuery();
var site = tenant.GetSiteByUrl(webUrl);
spcontext.Load(site);
spcontext.Load(site.RootWeb);
spcontext.ExecuteQuery();
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 };
page.AddControl(clientWp);
}
page.Save(pageName);
spcontext.ExecuteQuery();
I am getting the below error
"Site https://tenant-admin.sharepoint.com does not have a sitepages library and therefore this page can't be a client side page.
Since I am using the tenant context. Can anyone guide me in right way?
Update:
Methods using for getting the Access token in the MVC Web Application. Here I am passing https://tenant-admin.sharepoint.com
as the parameter for the method GetSharePointContext
public ClientContext GetSharePointContext(string SiteUrl)
{
ClientContext spContext = null;
try
{
Task<string> result = Task.Run<string>(() => GetAccessToken(SiteUrl));
result.Wait();
if (result != null)
{
spContext = TokenHelper.GetClientContextWithAccessToken(SiteUrl, result.Result);
}
}
catch (System.Exception ex)
{
}
return spContext;
}
public async Task<string> GetAccessToken(string siteurl)
{
AuthenticationResult authenticationResult = null;
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
// get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
ClientCredential clientcred = new ClientCredential(clientId, appKey);
try
{
Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authenticationContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.microsoftonline.com/" + tenantID, new ADALTokenCache(signedInUserID));
authenticationResult = await authenticationContext.AcquireTokenSilentAsync(siteurl, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
return authenticationResult.AccessToken;
}
catch (System.Exception ex)
{
return null;
}
}