What i need to do to replace PnP.Core.Services with CSOM for our Azure Function which uses .net 6.0

john john 946 Reputation points
2023-02-04T18:24:05.72+00:00

I have an Azure Function which uses PnP.Core.Services to interact with SharePoint to create a list item. The Azure function is based on .net version 6.0.

I have this startup.cs:-

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PnP.Core.Auth;
using System.Security.Cryptography.X509Certificates;

[assembly: FunctionsStartup(typeof(FunctionApp2.Startup))]
namespace FunctionApp2
{
    class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {

            var config = builder.GetContext().Configuration;
            var azureFunctionSettings = new AzureFunctionSettings();
            config.Bind(azureFunctionSettings);
            builder.Services.AddPnPCore(options =>
            {
                options.DisableTelemetry = true;
                var authProvider = new X509CertificateAuthenticationProvider(azureFunctionSettings.ClientId,
                    azureFunctionSettings.TenantId,
                    StoreName.My,
                    StoreLocation.CurrentUser,
                    azureFunctionSettings.CertificateThumbprint);
                options.DefaultAuthenticationProvider = authProvider;

                options.Sites.Add("Default", new PnP.Core.Services.Builder.Configuration.PnPCoreSiteOptions

                {
                    SiteUrl = azureFunctionSettings.SiteUrl,
                    AuthenticationProvider = authProvider


                });

            });

        }

    }
}

and this Function1.cs:-

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using PnP.Core.Services;
using PnP.Core.Model.SharePoint;
using System.Collections.Generic;

namespace FunctionApp2
{
    public class Function1

    {
        private readonly IPnPContextFactory pnpContextFactory;
        public Function1(IPnPContextFactory pnpContextFactory)
        {
            this.pnpContextFactory = pnpContextFactory;

        }
        [FunctionName("Function1")]
        public void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            using (var context = pnpContextFactory.Create("Default"))
            {
                var myList = context.Web.Lists.GetByTitle("SubFolders");
                Dictionary
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,300 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. MughundhanRaveendran-MSFT 12,431 Reputation points
    2023-03-01T05:27:32.9733333+00:00

    @john john

    Please refer to the below article to authenticate using CSOM and then use csom library to fetch the data from lists.

    https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/using-csom-for-dotnet-standard

    .NET Standard version of the CSOM Assemblies is included in the existing NuGet package called Microsoft.SharePointOnline.CSOM from version 16.1.20211.12000 onwards. The below sample requires this version or higher to work in a .Net core/standard targeted project.

     https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/

    FLow can be :

    1. register an app in AAD
    2. Create secret or cert 
    3. get this scret or cert in keyvault

    Use Managed identity for connection kv from Azure function. In the code we would have id and secret/cert. Use this to connect to SP onine using SPO modern authSharePointOnlineCredentials class and then query list library using csom lirbary

    0 comments No comments