How To Create SharePoint Online Site Group Using Azure Function

Introduction

Azure Functions is used for executing a small piece of code or “function” in a cloud and you pay only for the time your code executes. You can use any development language of choice, such as C#, F#, PHP, Java etc. Some of the key features of Functions are Choice of language, Pay-per-use pricing model, bring your own dependencies, integrated security, simplified integration, flexible development, and open-source. Please refer Azure Functions for more details. Refer my previous articles below,

 

 

In this blog, you will see how to create SharePoint Online site group using Azure Functions.

Schedule an Azure function

Log in to the Azure Portal.

Click New-> Compute -> Function App.

Enter all the required details and click Enter.

The Functions app will be provisioned within a few minutes.

Click Functions Apps->AzureFunctionsExamples (which you have created) -> Functions -> “+” to create new function.

Click Custom Function.

Select HTTP Trigger -> C#.

Enter the name of the new function and click the "Create" button.

Refer to my previous article to add CSOM dependencies. Replace the code in run.csx with the following code.

  1. #r "Microsoft.SharePoint.Client.dll"  
  2. #r "Microsoft.SharePoint.Client.Runtime.dll"  
  3.   
  4. using System.Net;  
  5. using Microsoft.SharePoint.Client;  
  6.   
  7. public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)  
  8. {      
  9.     string siteURL="https://c986.sharepoint.com/sites/Vijai/Subsite";  
  10.     string userName="[email protected]";  
  11.     string password="********";  
  12.   
  13.     log.Info("C# HTTP trigger function processed a request.");  
  14.   
  15.     // parse query parameter  
  16.     string title = req.GetQueryNameValuePairs()  
  17.         .FirstOrDefault(q => string.Compare(q.Key, "title"true) == 0)  
  18.         .Value;  
  19.   
  20.     string description = req.GetQueryNameValuePairs()  
  21.         .FirstOrDefault(q => string.Compare(q.Key, "description"true) == 0)  
  22.         .Value;  
  23.   
  24.     // Get request body  
  25.     dynamic data = await req.Content.ReadAsAsync<object>();  
  26.   
  27.     // Set name to query string or body data  
  28.     title = title ?? data?.name.title;  
  29.     description = description ?? data?.name.description;  
  30.   
  31.     System.Security.SecureString secureString=new System.Security.SecureString();  
  32.     foreach(char ch in password)  
  33.     {  
  34.         secureString.AppendChar(ch);          
  35.     }  
  36.   
  37.     SharePointOnlineCredentials creds=new SharePointOnlineCredentials(userName, secureString);  
  38.   
  39.     using(var ctx=new ClientContext(siteURL))  
  40.     {  
  41.         ctx.Credentials=creds;  
  42.         // Get the SharePoint web  
  43.         Web web = ctx.Web;  
  44.   
  45.         // Get all the site groups  
  46.         GroupCollection groupColl = web.SiteGroups;  
  47.   
  48.         // Create a new site group  
  49.         GroupCreationInformation creationInfo = new GroupCreationInformation();  
  50.         creationInfo.Title = title;  
  51.         creationInfo.Description = description;  
  52.         Group newGroup = groupColl.Add(creationInfo);  
  53.         ctx.Load(newGroup);  
  54.   
  55.         // Execute the query to the server  
  56.         ctx.ExecuteQuery();  
  57.   
  58.         return data.name == null  
  59.             ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")  
  60.             : req.CreateResponse(HttpStatusCode.OK, "Site group created successfully. Group Name: " + title+" Description: "+description);  
  61.     }  
  62. }  

Test the function

Update the request body. Save and Run the function.

You could check the logs to verify the Azure Functions execution. A new SharePoint Online site group is created successfully.

Result

Thus, in this blog, you saw how to create a SharePoint Online site group using Azure Functions.