How to get User Permissions for Sharepoint Online User via CSOM in WinForms App

Mike Lowe
Mike Lowe used Ask the Experts™
on
Hi,

C# Winforms app querying Sharepoint Online using the CSOM Sharepoint Client Classes.

All good so far but I need to get the permissions for the current user so I can check if they have access to the website and/or subsite. I'm connecting to my 'ClientContext' correctly and am using an AD Service Account which has permission to access all sites.

I've found several examples on the web that all give this example of how to retrieve the permissions for the current user:

User user = cxt.Site.RootWeb.EnsureUser(UserPrincipal.Current.EmailAddress);
cxt.Load(user);
cxt.ExecuteQuery();

 // Get all permissions for this user             
var permissions = cxt.Site.RootWeb.GetUserEffectivePermissions(user.LoginName);
 cxt.ExecuteQuery();

Open in new window


The line in bold fails with an 'unauthorized access exception'. The current user in this scenario is myself and I have permissions to open and view all sites except 1 test site.

My Context 'cxt' is running under the service account.

So, in effect I understand this as: run the context as the service account and then load a user and check their permissions, but it doesn't work.

Is it possible what I'm trying to do or is there another way? I've seen mentioned that you have to build a Web API to perform the user permission check because it's allowed to run with 'elevated permissions'.

Hope someone can help?

Thanks,

Mike.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Developer
Commented:
This is now fixed. The issue was that the service account I'm using to connect to the Sharepoint Context didn't have enough permissions. The service account needs to be a site-owner.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial