Friday, October 2, 2009

SharePoint User Groups

Check User Group in SharePoint

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 = "http://mysite:5050/";
            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".

0 comments:

Post a Comment