Ads

Wednesday, 15 May 2013

C# code to Check whetehr User present in SharePoint group or not

To store and manage permissions we can create User Groups in SharePoint. In this article I’ll discuss how you can check whether the current user is in a particular group using C#. This method will return Boolean value "true" if the user in the given group. You can use this method in your custom webpart.

public bool isMemberInGroup(string groupName)
{
    string siteUrl = "Site URL Here";
    SPSite site = new SPSite(siteUrl);
    SPWeb web = site.OpenWeb();
    bool memberInGroup = web.IsCurrentUserMemberOfGroup(web.Groups[groupName].ID);
    return memberInGroup;
}

If you want to compare the current user in "Person or Group" column type, then you can also get the current user’s user name using  "web.CurrentUser.Name".

No comments:

Post a Comment

Ads