Want a way to check the users current security permission group membership? No? Well…this is awkward………. I’m going to ignore you.
In the Namespace deceleration, add this bad boy here. It make sure your C# knows what to do when looking for user permissions group.
using Microsoft.SharePoint;
Then we’re going to actually add our code within the rest of your class.
#region Check Permissions of user protected bool CheckIfCurrentUserInGroup(string sGroups) { bool bSecure = false; string[] groups = sGroups.Split(','); foreach (string group in groups) { if (SPContext.Current.Web.SiteGroups[group].ContainsCurrentUser) { bSecure = true; break; } } return bSecure; } #endregion
Now all you should need to do is call
CheckIfCurrentUserInGroup()
And pass in the name of the user permissions group you want to check on.
Sample
#region Check Permissions of current user private void CheckPermissions() { bool bCanDisplay = false; try { bCanDisplay = CheckIfCurrentUserInGroup("Visitor"); #region If Statement //Display button if the user is withing the group(s). if (bCanDisplay) { genPDFlnkButton.Visible = true; } else { genPDFlnkButton.Visible = false; } #endregion } catch (Exception e) { handleErrors(e.ToString()); } } #endregion #region Check Permissions of user protected bool CheckIfCurrentUserInGroup(string sGroups) { bool bSecure = false; string[] groups = sGroups.Split(','); foreach (string group in groups) { if (SPContext.Current.Web.SiteGroups[group].ContainsCurrentUser) { bSecure = true; break; } } return bSecure; } #endregion
3 thoughts on “SharePoint Check User Permissions”
ContainsCurrentUser
Hello Marion, I wonder if you have any solution to check user permissions via client object model. Unfortunately a SPGroup does not seem to have a “ContainsCurrentUser” method:
var group = clientContext.Web.SiteGroups[0]
bool isInGroup = group.ContainsCurrentUser <– missing this method in CSOM
Did you, or someone else reading this, figure it out?
Regards, Stan
Hey Stan.
I found this blog post here: http://www.novolocus.com/2012/02/29/check-permissions-in-javascript-client-object-model/
He talks about checking users permissions for an Item. You can check for permissions based on the Site, List, or Item level using “.get_effectiveBasePermissions()” with CSOM.
Here is a blog post that shows a pretty good example of checking List permission: http://blog.fidelityfactory.com/2011/11/29/sharepoint-client-ecma-script-check-user-permissions/