Skip to content

Instantly share code, notes, and snippets.

@dgusoff
Last active September 12, 2017 21:15
Show Gist options
  • Save dgusoff/fe8ff2ceab677f6ff045d4b800633bb5 to your computer and use it in GitHub Desktop.
Save dgusoff/fe8ff2ceab677f6ff045d4b800633bb5 to your computer and use it in GitHub Desktop.
Azure Function get SharePoint groups
{
"groupName": "Flow Test Users",
"siteUrl": "https://devtenant.sharepoint.com/sites/contoso"
}
{
"frameworks": {
"net46":{
"dependencies": {
"SharePointPnPCoreOnline": "2.17.1708.1"
}
}
}
}
#r "Newtonsoft.Json"
using System.Net;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;
using System.Security;
using Newtonsoft.Json;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
dynamic data = await req.Content.ReadAsAsync<object>();
string groupName = data.groupName;
string siteUrl = data.siteUrl;
List<SPUser> users = new List<SPUser>();
log.Info("Querying site " + siteUrl + " for group " + groupName);
using(ClientContext ctx = GetContext(siteUrl))
{
Web web = ctx.Web;
Group sharepointGroup = web.SiteGroups.GetByName(groupName);
ctx.Load(web);
ctx.Load(sharepointGroup, grp => grp.Title, grp => grp.Users);
ctx.ExecuteQuery();
log.Info("Group has " + sharepointGroup.Users.Count + " users.");
foreach(User usr in sharepointGroup.Users)
{
SPUser newUser = new SPUser()
{
DisplayName = usr.Title,
Email = usr.Email
};
users.Add(newUser);
}
}
return req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(users));
}
public static ClientContext GetContext(string siteUrl)
{
ClientContext ctx = new ClientContext(siteUrl);
ctx.Credentials = new SharePointOnlineCredentials("[email protected]", GetPassword("pass@word1"));
return ctx;
}
private static SecureString GetPassword(string passwd)
{
var secure = new SecureString();
foreach (char c in passwd)
{
secure.AppendChar(c);
}
return secure;
}
public class SPUser
{
public string DisplayName{ get; set;}
public string Email { get; set;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment