I am creating a site definition by code and I have a requirement to create the security groups in the site definition.
How can I create the user security groups in the site definition using the Visual Studio code?
Please guide.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityYou can staple a feature to do this programmatically.
Add a Feature and associate it an event receiver. Then, in the public override void FeatureActivated(SPFeatureReceiverProperties properties)
method add the following code:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
web.SiteGroups.Add("NameOfTheGroup", web.CurrentUser,
web.CurrentUser, "DescriptionOfTheGroup");
web.Update();
//If you want to add users to the group
web.SiteGroups[NameOfTheGroup].AddUser("UserLoginName", string.Empty, "UserLoginName", string.Empty);
}
In your onet.xml to staple the feature you have to reference in like this:
<Configurations>
<Configuration ID="0" Name="Your-Name-Space" MasterUrl="_catalogs/masterpage/Your-Master(If-you-use-it).master">
<SiteFeatures>
<Feature ID="Add-Here-The-ID-of-Your-Feature" />
</Configuration>
</Configurations>