Issue - You have a SharePoint site and you want to check the roles of the user.
Solution - You can do that easily by checking what kind of permission the users have.
For Ex - You might know that the owner will have full control & some other user may be contributing. In this case, you can check the specific permissions of the user like the owner can manage list items, add, delete, edit the Items but the other user can only add/delete/edit but cannot manage lists.
You can just perform the operation, stated above, using the code, given below-.
CSOM
- var _context = null;
- if (_context == null)
- {
- var Request = HttpContext.Current.Request;
- Uri _hostWeb = new Uri(Request.QueryString["SPHostUrl"]);
- _context = TokenHelper.GetS2SClientContextWithWindowsIdentity(_hostWeb, HttpContext.Current.User.Identity as System.Security.Principal.WindowsIdentity);
- _context.Load(_context.Web, w => w.EffectiveBasePermissions);
- _context.ExecuteQuery();
- }
- var IsOwner = _context.Web.EffectiveBasePermissions.Has(PermissionKind.ManageLists);
Now, you can see, I have checked if the user with whom’s identity the context has been made has Manage lists Permission or not, which in turn states whether he is the owner or not.
You can also club the permissions and check if you have custom permission levels.