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.
- #r "Microsoft.SharePoint.Client.dll"
- #r "Microsoft.SharePoint.Client.Runtime.dll"
-
- using System.Net;
- using Microsoft.SharePoint.Client;
-
- public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
- {
- string siteURL="https://c986.sharepoint.com/sites/Vijai/Subsite";
- string userName="[email protected]";
- string password="********";
-
- log.Info("C# HTTP trigger function processed a request.");
-
-
- string title = req.GetQueryNameValuePairs()
- .FirstOrDefault(q => string.Compare(q.Key, "title", true) == 0)
- .Value;
-
- string description = req.GetQueryNameValuePairs()
- .FirstOrDefault(q => string.Compare(q.Key, "description", true) == 0)
- .Value;
-
-
- dynamic data = await req.Content.ReadAsAsync<object>();
-
-
- title = title ?? data?.name.title;
- description = description ?? data?.name.description;
-
- System.Security.SecureString secureString=new System.Security.SecureString();
- foreach(char ch in password)
- {
- secureString.AppendChar(ch);
- }
-
- SharePointOnlineCredentials creds=new SharePointOnlineCredentials(userName, secureString);
-
- using(var ctx=new ClientContext(siteURL))
- {
- ctx.Credentials=creds;
-
- Web web = ctx.Web;
-
-
- GroupCollection groupColl = web.SiteGroups;
-
-
- GroupCreationInformation creationInfo = new GroupCreationInformation();
- creationInfo.Title = title;
- creationInfo.Description = description;
- Group newGroup = groupColl.Add(creationInfo);
- ctx.Load(newGroup);
-
-
- ctx.ExecuteQuery();
-
- return data.name == null
- ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
- : req.CreateResponse(HttpStatusCode.OK, "Site group created successfully. Group Name: " + title+" Description: "+description);
- }
- }
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.