Programatically check if a user is a Global Admin
-
08-10-2020 - |
Domanda
I do have a client context ClientContext context
and current user:
var currentUser = context.Web.CurrentUser;
context.Load(currentUser);
context.ExecuteQuery();
I can also use rest API.
Soluzione
Solved this using Graph API rest request:
https://graph.windows.net/me/memberOf?api-version=1.6
Documentation: https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/users-operations
Altri suggerimenti
I use this for SharePoint Online:
_clientContext.Load(_clientContext.Site, s => s.Url);
_clientContext.ExecuteQueryRetry();
var tenantUrl = $"https://{new Uri(_clientContext.Url).Host.Replace(".sharepoint.com", "-admin.sharepoint.com")}";
var tenantCtx = new ClientContext(tenantUrl);
tenantCtx.Credentials = _clientContext.Credentials;
var tenant = new Tenant(tenantCtx);
var props = tenant.GetSitePropertiesByUrl(_clientContext.Site.Url, false);
tenant.Context.Load(props);
tenant.Context.ExecuteQuery(); // will throw 401 exception here, if current user is not tenant admin
Try this:
if (clientcontext.Web.CurrentUser.IsSiteAdmin)
{
Your Code
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange