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-06T07:45:37.1533333+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<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

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,321 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,686 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 31,681 Reputation points Microsoft Vendor
    2023-02-07T01:31:45.9266667+00:00

    Hi @john john

    You need to use .net framework 4.6 or later to support CSOM. For the CSOM code you can refer to following

    using Microsoft.SharePoint.Client;
     
    using (ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection"))
    {
    // clientcontext.Web.Lists.GetById - This option also can be used to get the list using List GUID
    // This value is NOT List internal name
    List targetList = clientContext.Web.Lists.GetByTitle("List Name");
     
    ListItemCreationInformation oListItemCreationInformation = new ListItemCreationInformation();
    ListItem oItem = targetList.AddItem(oListItemCreationInformation);
    oItem["Title"] = "New List Item";
     
    oItem.Update();
    clientContext.ExecuteQuery();
    }
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.