0
votes

I am building a .net core web app in which i use AzureAD auth and Microsoft Graph to get data from a sharepoint online site.

I need to get the groups of the current user.

I tried to use graphClient.Me.MemberOf.Request().GetAsync();

I think i'm getting the role of the user in the Azure directory.

But i want the role of the current user for a specific sharepoint online site.

Is that possible to get this information and how ?

I don't find a working way to get this using Microsoft Graph.

EDIT:

As Microsoft Graph doesn't allow to get that data. I tried to call the following Sharepoint Online API endpoint :

https://{name}.sharepoint.com/sites/{name}/_api/web/currentUser?$select=Groups/Title&$expand=Groups

Using this api endpoint i can see all the roles of the current user in my browser. But i don't find how to call it from my .net core web app.

Tried the following :

        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Content-types", "application/json;odata=verbose");
        var response = await client.GetAsync("https://{name}.sharepoint.com/sites/{name}/_api/web/currentUser?$select=Groups/Title&$expand=Groups");
        if (response.IsSuccessStatusCode)
        {
            var json = await response.Content.ReadAsStringAsync();
        }

But that give me a 403 response.

EDIT 2 :

I am currently trying to use CSOM to get this informations(TCUE.NetCore.SharepointOnline.CSOM.16.1.8029.1200.)

But i don't find a way to get TokenHelper.cs.

        var token = TokenHelper.GetAppOnlyAccessToken(SharePointPrincipalId, webUri.Authority, null).AccessToken;

        var ctx = TokenHelper.GetClientContextWithAccessToken(webUri.ToString(), token);

I tried to add "AppForSharePointOnlineWebToolkit" and it did not add the needed files in the project.

How can i get the TokenHelper.cs file ?

Thanks for any help.

Tristan

2

2 Answers

1
votes

No. Microsoft Graph doesn't expose an endpoint that allows you to get the information of SharePoint Group and its members.

If you has this requirement, you could vote this idea on Microsoft Graph UserVoice.

2
votes

To execute CSOM code in .net core, do below settings.

We can install the package as below.

Install-Package TTCUE.NetCore.SharepointOnline.CSOM.16.1.8029.1200 -Version 16.1.8029.1200 More information is here: TTCUE.NetCore.SharepointOnline.CSOM.16.1.8029.1200

Or use the following solution from GitHub: NetCore.CSOM

Or follow the steps below.

1.Create a .NET Core console app.

2.Add the references: Microsoft.SharePoint.Client.Portable.dll, Microsoft.SharePoint.Client.Runtime.Portable.dll, and Microsoft.SharePoint.Client.Runtime.Windows.dll.

Note: If the project has references to Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll, please remove them.

These references can be accessed by installing CSOM library into another project, and then navigating to installed nuget packages in the file directory: c:\Users\user.nuget\packages\microsoft.sharepointonline.csom(version)\lib\netcore45

3.Add the code below to the .NET Core 2.0 console application:

Get current user role:

To get the current user role, you can use Web.GetUserEffectivePermissions method.

Ex:

ClientResult<BasePermissions> permission= web.GetUserEffectivePermissions(name);
context.ExecuteQuery();
 var res = permission.Value;

Refer below link to get clientcontext using access token: https://www.sharepointpals.com/post/how-to-get-the-client-context-using-app-access-token-by-passing-client-id-and-client-secret-id-using-csom-in-sharepoint-office-365/