Search code examples
sharepoint-2010

Current user belongs to a specific group in sharepoint?


I have created a code to find out the current logged in user belongs to a particular group or not but I am getting an error saying that

IsCurrentUserMemberOfGroup(gName1); contains some invalid arguments.

Please help ?

Here is my code

SPSite oSite = (SPSite)properties.OpenWeb().Site;
SPWeb oWeb = oSite.OpenWeb();
SPGroupCollection myGroup = oWeb.Groups;
foreach (SPGroup gName in myGroup)
{
    //string gname1 = gName.ToString();
    SPGroup gName1 = gName;
    if (gName1.Name == "Final Approval Group")
    {
        //SPGroup lGroup = oWeb.Groups["Final Approval Group"];
        bool answer = oWeb.IsCurrentUserMemberOfGroup(gName1);
        fValue = 1;
        break;
    }
}

Solution

  • Try using gName1.ID since IsCurrentUserMemberOfGroup takes an int argument.

    SPGroupCollection myGroup = oWeb.Groups; 
      foreach (SPGroup gName in myGroup) { 
        //string gname1 = gName.ToString(); 
        SPGroup gName1 = gName; 
        if (gName1.Name == "Final Approval Group") { 
          //SPGroup lGroup = oWeb.Groups["Final Approval Group"]; 
          bool answer = oWeb.IsCurrentUserMemberOfGroup(gName1.ID); 
          fValue = 1; break; 
      } 
    }