I am trying to create a new SharePoint group programmatically (using .Net), for this I am using the following C# code

            //greate group with no users:

            web.SiteGroups.Add(groupName, groupOwner, null, description);
            //Parameters are defined earlier in the code such as:
            //   string groupName
            //   SPUser groupOwner
            //   string description

            SPGroup newGroup = web.Groups.GetByName(groupName);

The creation works well but when I try to access the group an exception is thrown saying "Group cannot be found." What is the reason that the group can not be found and how can I fix this (I need to access the group for my further code)? If I check on the SharePoint under People and Groups the group is there.

Any help is appreciated, thanks.

  • I'm not 100% sure, but I'm guessing it's because you are using old SPWeb object which doesn't have new group in it's properties. Dispose old object and create new one. – StupidOne May 22 '14 at 9:45

Try this : SPGroup oGroup = web.SiteGroups["Original_Name"];

  • Thank you for the quick reply. That is what I had originally with the same result that an exception was thrown. – user3297993 May 22 '14 at 10:02
  • Please check waht following methods return :private bool GroupExistsInWebSite(SPWeb web, string name) { return web.Groups.OfType<SPGroup>().Count(g => g.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) > 0; } private bool GroupExistsInSiteCollection(SPWeb web, string name) { return web.SiteGroups.OfType<SPGroup>().Count(g => g.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) > 0; } – Aanchal May 22 '14 at 10:06
  • This web.SiteGroups[groupName]; works for me. web.Groups[groupName] was throwing exception Group cannot be found – Aanchal May 22 '14 at 10:36
  • Thanks that helped, I must have mist out on this part. It is working for me now as well. – user3297993 May 22 '14 at 11:05

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.