0

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<string, object> values = new Dictionary<string, object>
    {
        { "Title", System.DateTime.Now }
    };

                // Use the AddBatch method to add the request to the current batch
                myList.Items.AddBatch(values);
                context.Execute();
            }
        }
    }
}

and this AzureFunctionSettings.cs:-

using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace FunctionApp2
{
    internal class AzureFunctionSettings
    {
        public string SiteUrl { get; set; }
        public string TenantId { get; set; }
        public string ClientId { get; set; }
        public StoreName CertificateStoreName { get; set; }
        public StoreLocation CertificateStoreLocation { get; set; }
        public string CertificateThumbprint { get; set; }


    }
}

now i am working with a client and they do not allow us to use any open source technologies like PnP. so what i need to do to replace my above PnP code with CSOM code? and is there a CSOM code for .net 6?

Thanks

1 Answer 1

1

Yes, you can use CSOM with .NET 6. The Nuget package is Microsoft.SharePointOnline.CSOM.

For more information see: Using CSOM for .NET Standard instead of CSOM for .NET Framework.

enter image description here

Here are a couple resources that show how to use the Microsoft Authentication Library (MSAL) to authenticate and get an access token which you can use when making calls with the Client Object Model. The posts are a little dated but the process is the same in .NET 6.

SharePoint, Azure Functions, and Visual Studio (Part 2) - See the section entitled Implementing the GetLists function

Using .NET Standard CSOM and MSAL.NET for App-Only auth in SharePoint Online

2
  • ok thanks for the info, but do you have any supporting documentation ro code on what i need to add inside my project startup.cs ? to replace the PnP code for authentication ?
    – John John
    Feb 6, 2023 at 10:32
  • I updated my answer to include links to resources that show how to authenticate and get an access token. Feb 6, 2023 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.