How to authenticate with a tenant in PnP

%3CLINGO-SUB%20id%3D%22lingo-sub-277391%22%20slang%3D%22en-US%22%3EHow%20to%20authenticate%20with%20a%20tenant%20in%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-277391%22%20slang%3D%22en-US%22%3E%3CP%3EI%20need%20to%20create%20a%20method%20to%20do%20some%20operations%20in%20SPO%2C%20lets%20say%2C%20create%20a%20modern%20site%2C%20but%20I%20need%20it%26nbsp%3B%20in%20a%20REST%20API%2C%20that%20will%20be%20consumed%20later%20by%20a%20frontend%20angular%20or%20react%20app.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20found%20this%20sample%3A%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fmodern-experience-customizations-provisioning-sites%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fmodern-experience-customizations-provisioning-sites%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Evar%20targetTenantUrl%20%3D%20%22https%3A%2F%2F%5Btenant%5D.sharepoint.com%2F%22%3B%0A%0Ausing%20(var%20context%20%3D%20new%20ClientContext(targetTenantUrl))%0A%7B%0A%20%20%20%20context.Credentials%20%3D%20OfficeDevPnP.Core.Utilities.CredentialManager.GetSharePointOnlineCredential(%22%5BName-of-Your-Credentials%5D%22)%3B%0A%0A%20%20%20%20%2F%2F%20Create%20new%20%22modern%22%20communication%20site%20at%20the%20url%20https%3A%2F%2F%5Btenant%5D.sharepoint.com%2Fsites%2Fmymoderncommunicationsite%0A%20%20%20%20var%20communicationContext%20%3D%20await%20context.CreateSiteAsync(new%20CommunicationSiteCollectionCreationInformation%20%7B%0A%20%20%20%20%20%20%20%20Title%20%3D%20%22title%22%2C%20%2F%2F%20Mandatory%0A%20%20%20%20%20%20%20%20Description%20%3D%20%22description%22%2C%20%2F%2F%20Mandatory%0A%20%20%20%20%20%20%20%20Lcid%20%3D%201033%2C%20%2F%2F%20Mandatory%0A%20%20%20%20%20%20%20%20AllowFileSharingForGuestUsers%20%3D%20false%2C%20%2F%2F%20Optional%0A%20%20%20%20%20%20%20%20Classification%20%3D%20%22classification%22%2C%20%2F%2F%20Optional%0A%20%20%20%20%20%20%20%20SiteDesign%20%3D%20CommunicationSiteDesign.Topic%2C%20%2F%2F%20Mandatory%0A%20%20%20%20%20%20%20%20Url%20%3D%20%22https%3A%2F%2F%5Btenant%5D.sharepoint.com%2Fsites%2Fmymoderncommunicationsite%22%2C%20%2F%2F%20Mandatory%0A%20%20%20%20%7D)%3B%0A%20%20%20%20communicationContext.Load(communicationContext.Web%2C%20w%20%3D%26gt%3B%20w.Url)%3B%0A%20%20%20%20communicationContext.ExecuteQueryRetry()%3B%0A%20%20%20%20Console.WriteLine(communicationContext.Web.Url)%3B%0A%7D%3C%2FPRE%3E%0A%3CP%3EThe%20issue%20with%20this%20sample%20is%20that%20it%20takes%20a%20Credential%20as%20a%20parameter%20which%20is%20saved%20INTO%20the%20windows%20credentials%20manager.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOn%20the%20context%20of%20an%20Azure%20WebApp%2C%20I%20suppose%20this%20wont%20work%2C%20and%20this%20sample%20would%20only%20work%20on%20a%20console%20app.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20are%20other%20alternatives%20to%20get%20a%20Sharepoint%20Context%3F%20I%20know%20App-Only%20doesnt%20work%20(already%20tested%20it)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I need to create a method to do some operations in SPO, lets say, create a modern site, but I need it  in a REST API, that will be consumed later by a frontend angular or react app.

 

I found this sample:https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/modern-experience-customizations-p...

 

var targetTenantUrl = "https://[tenant].sharepoint.com/";

using (var context = new ClientContext(targetTenantUrl))
{
    context.Credentials = OfficeDevPnP.Core.Utilities.CredentialManager.GetSharePointOnlineCredential("[Name-of-Your-Credentials]");

    // Create new "modern" communication site at the url https://[tenant].sharepoint.com/sites/mymoderncommunicationsite
    var communicationContext = await context.CreateSiteAsync(new CommunicationSiteCollectionCreationInformation {
        Title = "title", // Mandatory
        Description = "description", // Mandatory
        Lcid = 1033, // Mandatory
        AllowFileSharingForGuestUsers = false, // Optional
        Classification = "classification", // Optional
        SiteDesign = CommunicationSiteDesign.Topic, // Mandatory
        Url = "https://[tenant].sharepoint.com/sites/mymoderncommunicationsite", // Mandatory
    });
    communicationContext.Load(communicationContext.Web, w => w.Url);
    communicationContext.ExecuteQueryRetry();
    Console.WriteLine(communicationContext.Web.Url);
}

The issue with this sample is that it takes a Credential as a parameter which is saved INTO the windows credentials manager.

 

On the context of an Azure WebApp, I suppose this wont work, and this sample would only work on a console app.

 

What are other alternatives to get a Sharepoint Context? I know App-Only doesnt work (already tested it)

 

0 Replies