Using CSOM from Azure Functions

I got back to active SharePoint and CSOM development some months ago and first thing to do was to port bunch of workflows from in-prem SharePoint to cloud. Where I live we don’t usually have any simple workflows. Most of them need some backing code due to custom logic. So, my only option was to go with Microsoft Flow or Azure Logic Apps and Azure Functions. Here’s the project with one dummy function to get started.

Are you new to Azure Functions? If yes, then you can read my posts Getting started with Azure Functions and Short introduction to serverless architecture to get better understanding of Azure Functions and serverless computing.

First of all – I was damn surprised to find out that CSOM doesn’t work with .NET Core and I have to use first generation of Azure Functions and .NET Framework 4.x (4.6.1 in my case). Here’s the project file that works for me.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <AzureFunctionsVersion>v1</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <None Remove=".gitignore" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="Microsoft.SharePoint.Client">
      <HintPath>C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.SharePoint.Client.Runtime">
      <HintPath>C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

With .NET Framework come also .NET Framework classes and stuff that doesn’t work with .NET Core. Here’s the dummy function to use as a starting point for functions that talk to Office 365 SharePoint using CSOM.

[FunctionName("DummyFunction")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage request, TraceWriter logger)
{
    logger.Info("C# HTTP trigger function processed a request.");
    foreach (var key in request.GetQueryNameValuePairs())
    {
        logger.Info(key.Key + ": " + key.Value);
    }

    return request.CreateResponse(HttpStatusCode.OK);
}

If you configure this function to use function key you can easily try it out directly from your browser. You can make this function synchronous if you don’t use asynchronous calls in CSOM.

Wrapping up

Not all revenue generating services are supported on .NET Core yet and it was big surprise to me to see that CSOM is not available for .NET Core yet. There are third-party NuGet paclages available to make CSOM work with .NET Core but if you prefer to go with officially supported libraries then full .NET Framework is your only choice today.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    One thought on “Using CSOM from Azure Functions

    Leave a Reply

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