Authenticate SharePoint Utilizing Pnp.Framework In An Azure Capabilities
Introduction
On this article, you’ll learn to authenticate SharePoint on-line and get a context in Azure features utilizing PnP.Framework.
Steps
- Create an Azure perform
- Add PnP.framework and Microsoft.SharePointOnline.CSOM bundle
- Use AuthenticationManager class to authenticate SharePoint.
- Publish the Azure features in Azure Portal ( Have to have an lively Azure subscription)
- Take a look at the perform
Create an Azure perform
Step 1
Login to Visual Studio 2019 and Create a brand new Challenge.
Step 2
Choose the Azure features and click on on Subsequent.
Step 3
Present the identify and click on on Create.
Step 4
Choose the Azure features v3(.NET Core), choose HTTP set off and choose Anonyms and click on on Create.
Set up NuGet Bundle
Choose answer and right-click on dependencies and NuGet Course of.
Set up the packages,
- Microsoft.SharePoint on-line.CSOM
- PnP.Framework
Add AuthenticationManager
This AuthenticationManager is from PnP, the framework which helps to get token and supply the context of the SharePoint.
Copy-paste the beneath code within the Perform,
utilizing System;
utilizing System.IO;
utilizing System.Threading.Duties;
utilizing Microsoft.AspNetCore.Mvc;
utilizing Microsoft.Azure.WebJobs;
utilizing Microsoft.Azure.WebJobs.Extensions.Http;
utilizing Microsoft.AspNetCore.Http;
utilizing Microsoft.Extensions.Logging;
utilizing Newtonsoft.Json;
utilizing Microsoft.SharePoint.Shopper;
utilizing PnP.Framework;
utilizing System.Safety;
namespace Demo_Function
{
public static class Function1
{
[FunctionName("Function1")]
public static async Activity<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
Internet currentWeb;
string userLogin = "sp@sp1226.onmicrosoft.com"; //enter your account
string userPassword = "*****"; //enter your password for account
string sharePointUrl = "https://sp1226.sharepoint.com/websites/portal"; // enter your web site URL right here
var securePassword = new SecureString();
foreach (char c in userPassword)
{
securePassword.AppendChar(c);
}
AuthenticationManager auth = new AuthenticationManager(userLogin, securePassword);
utilizing (ClientContext ctx = await auth.GetContextAsync(sharePointUrl))
{
currentWeb = ctx.Internet;
ctx.Load(currentWeb);
await ctx.ExecuteQueryRetryAsync();
}
log.LogInformation($"Internet's title : {currentWeb.Title}");
var myObj = new { Title = currentWeb.Title, Descr = currentWeb.Description };
// return new HttpResponse($"Internet's title : {currentWeb.Title}" );
return new JsonResult(myObj);
}
}
}
Publish the Azure perform
Step 1
Construct the challenge after which Proper-click on the challenge to click on on Publish,
Step 2
Choose Goal as Azure and click on on Subsequent,
Step 3
Choose Azure perform app (Home windows) and click on on Subsequent.
Step 4
Create a brand new Azure Perform as proven within the picture,
Step 5
Present the identify of functionApp, Useful resource Group, and different fields and click on on Create.
Step 6
After that click on on the End button as you will note your new Perform App.
Step 7
Now click on on Publish in order that this Perform app can be deployed to the Azure Portal.
Take a look at the Azure perform
Now you can see the perform app deployed to your Azure useful resource.
Step 1
Navigate to the Azure portal utilizing portal.azure.com and see the perform app.
Step 2
Click on on the features and get the perform URL for Function1 and paste within the browser to test the output.
Step 3
After shopping the Perform URL within the browser you possibly can see the outcomes.
As we’re getting the positioning title and outline from the Perform app in our code similar is returned again from the URL.
Conclusion
On this method, we are able to authenticate the SharePoint utilizing the person identify and password within the .Web core initiatives. On this method, we don’t have to make use of the token helper class as a token is obtained from the AuthenticationManager class within the backend. So we solely have to jot down our SharePoint logic.
We will additionally use the Azure AD app-only technique to authenticate the SharePoint the place we are able to use ClientId and certificates to authenticate and therefore don’t must have to make use of the credentials.
Be aware
On this, I’ve used credentials within the code itself however you would possibly must fetch it from the Azure Key vault in precise growth eventualities.
Hope you discover it useful.