Skip to content
June 13, 2012 / red1s

Finding out which SharePoint group a user belongs to using a .Net 3.5 Webpart in MOSS


Recently I had to build a webpart to pick up which SharePoint group a user belonged to

Using the following code within your webpart code will create a Boolean value and output whether or not the user is part of a certain group you query

private bool IsMember(string grpmembername)

{

SPSite site = new SPSite(“http://connecttest”);

SPWeb web = site.OpenWeb();

if (web.IsCurrentUserMemberOfGroup(web.Groups[grpmembername].ID))

{

//txtLabel.Text = “It is a member”;

web.Close();

site.Close();

return (true);

}

else

{

//txtLabel.Text = “It is not a member”;

web.Close();

site.Close();

return (false);

}

}

if (IsMember(“SP_grpCognosForest”))

txtgroupbox.Text = “You are part of the group”;

else

txtgroupbox.Text = “You are not part of the group”;

I admit its not the most elegant solution, but its simple enough to implement quickly

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: