Skip to content

Instantly share code, notes, and snippets.

@JarbasHorst
Created August 1, 2017 18:03
Show Gist options
  • Save JarbasHorst/83a190422f1cd75db15dbc082a9a8b1d to your computer and use it in GitHub Desktop.
Save JarbasHorst/83a190422f1cd75db15dbc082a9a8b1d to your computer and use it in GitHub Desktop.
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Jarbas Horst wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.
* ----------------------------------------------------------------------------
*/
using System.Net;
using System.Collections.Generic;
using OfficeDevPnP.Core;
using OfficeDevPnP.Core.Entities;
using Microsoft.SharePoint.Client;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
try
{
// Gets data from request body.
dynamic data = await req.Content.ReadAsAsync<object>();
string siteUrl = data.SiteUrl;
if(String.IsNullOrEmpty(siteUrl))
return req.CreateResponse(HttpStatusCode.BadRequest, "Please pass site URL in request body!");
// Fetches client id and client secret from app settings.
string clientId = System.Environment.GetEnvironmentVariable("ClientId", EnvironmentVariableTarget.Process);
string clientSecret = System.Environment.GetEnvironmentVariable("ClientSecret", EnvironmentVariableTarget.Process);
// Obtains client context using the client id and client secret.
ClientContext ctx = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret);
// Retrieves site collection administrators.
List<UserEntity> admins = ctx.Web.GetAdministrators();
string response = String.Format("{0}", String.Join(", ", admins.Select(x => x.Title)));
return req.CreateResponse(HttpStatusCode.OK, response);
}
catch (Exception e)
{
return req.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment