1

How can I get MemberGroupId (Here 135) of particular SPGroup programmatically (Sharepoint 2010, C#) if I have SPGroup Name/Title? I can get a group by ID as given below.

SPGroup group = web.SiteGroups.GetByID(135);

I want to get same by title or SPgroup Name. Is there any alternative method or any how if I can get ID from Group name as I can't hard code group ID as in production ID will not remain same for the same group. Any help will be highly appreciated.

4

You can try it as :

SPGroup group = web.SiteGroups["groupName"];
var groupID = group.ID;

or

SPGroup group = web.SiteGroups.GetByName("groupName");
var groupID = group.ID;

Reference - SPGroupCollection.GetByName method

2

I am proposing to do something like this :

public SPGroup SearchGroupByName(string groupName)
 {
   SPGroup groupObject = null;
   foreach(SPGroup singleGroup in this.SPWebLocation.Groups)
   {
    if(groupName == singleGroup.Name)
    {
      groupObject = singleGroup;
    }
   }
   return groupObject;
 }

This method returns you the group by name.

after that you can retrieve from the group resultat the Id as a propriety.

Hope that will help you. Best regards.

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.