Wednesday, May 31, 2006

Detecting a user's group in SharePoint 2003

Listed below is a C# example of programmatically querying the group(s) a user belongs to on a SharePoint 2003 site:

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

private boolean IsContributor()
{
boolean bContributor = false;
SPWeb currentWeb = SPControl.GetContextWeb(this.Context);
SPRoleCollection currentRoles = currentWeb.CurrentUser.Roles;

foreach(SPRole myRole in currentRoles)
{
strRoles = myRole.Name.ToString();

if (strRoles == "Contributor")
{
bContributor = true;
break;
}
}

return bContributor;

}

No comments: