Azure

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

  1. Create an Azure perform
  2. Add PnP.framework and Microsoft.SharePointOnline.CSOM bundle
  3. Use AuthenticationManager class to authenticate SharePoint.
  4. Publish the Azure features in Azure Portal ( Have to have an lively Azure subscription)
  5. 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.

Authenticate SharePoint using Pnp.Framework in an Azure Functions

Step 4

Choose the Azure features v3(.NET Core),  choose HTTP set off and choose Anonyms and click on on Create.

Authenticate SharePoint using Pnp.Framework in an Azure Functions

Set up NuGet Bundle

Choose answer and right-click on dependencies and NuGet Course of.

Set up the packages,

  1. Microsoft.SharePoint on-line.CSOM
  2. PnP.Framework

Authenticate SharePoint using Pnp.Framework in an Azure Functions

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,

Authenticate SharePoint using Pnp.Framework in an Azure Functions

Step 2

Choose Goal as Azure and click on on Subsequent,

Authenticate SharePoint using Pnp.Framework in an Azure Functions

Step 3

Choose Azure perform app (Home windows) and click on on Subsequent.

Authenticate SharePoint using Pnp.Framework in an Azure Functions

Step 4

Create a brand new Azure Perform as proven within the picture,

Authenticate SharePoint using Pnp.Framework in an Azure Functions

Step 5

Present the identify of functionApp, Useful resource Group, and different fields and click on on Create.

Authenticate SharePoint using Pnp.Framework in an Azure Functions

Step 6

After that click on on the End button as you will note your new Perform App.

Authenticate SharePoint using Pnp.Framework in an Azure Functions

Step 7

Now click on on Publish in order that this Perform app can be deployed to the Azure Portal.

Authenticate SharePoint using Pnp.Framework in an Azure Functions

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.

Authenticate SharePoint using Pnp.Framework in an Azure Functions

Step 2

Click on on the features and get the perform URL for Function1 and paste within the browser to test the output.

Authenticate SharePoint using Pnp.Framework in an Azure Functions

Step 3

After shopping the Perform URL within the browser you possibly can see the outcomes.

Authenticate SharePoint using Pnp.Framework in an Azure Functions

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.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button