0

I am trying to develop custom alerts for a SharePoint Discussion List(in Community sub-site), I want to mail these alerts only to people in Members group of the Community (only those who have Joined the community ) how can I get these Members in Event receiver using Server side(C#) coding. I have managed to iterate the SiteGroups but how can i make sure that which one among the is Members and not Visitors/Owners Group.

1

In code you can

SPGroup spGroupItem = web.Groups["GroupName"];
SPRoleAssignment oRoleAssignment = web.RoleAssignments.GetAssignmentByPrincipal(spGroupItem);

foreach (SPRoleDefinition inRole in oRoleAssignment.RoleDefinitionBindings)
{
        //inRole.id //inRole.Name
        //1073741829 //limited access
}

Or you can find it using below function

bool IsMemberGroup(string groupName) 
{
    using (SPSite site = new SPSite("<your site collection>"))
    {
       using (SPWeb web = site.RootWeb)
       {
          foreach (SPRoleAssignment roleAssignment in web.RoleAssignments)
          {
              if (roleAssignment.RoleDefinitionBindings.Contains(web.RoleDefinitions.GetByType(SPRoleType.Contributor)))
              {
                 SPPrincipal principal = roleAssignment.Member;
                 if (principal is SPGroup) 
                 {
                    if(principal.Name.ToLower() == groupName.ToLower())
                        return true;
                 }
              }
          }
       }
    }       
    return false;
}

Another option is

SPWeb.AssociatedMemberGroup
  • I prefer SPWeb.AssociatedMemberGroup thanks...just an addition in case for future use to this There are properties on the SPWeb class: SPWeb.AssociatedVisitorGroup SPWeb.AssociatedMemberGroup SPWeb.AssociatedOwnerGroup – Akshay Randive Apr 17 '15 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.