Sharepoint overwrite default groups by code behind

If you created a custom site definition or site template and everything works fine, one of the last steps is thinking of the security groups or the SharePoint groups which are created by default.

Those are visitors, owner and members. The name is normally “owner of + web title”. But what if you would like to rename it or just give this user group a different role definition?

Well, i first thought, no problem, delete them and create your own permission level and groups in an event receiver – like in this post. But i was wrong, i tried it with an event receiver, with a custom SPWebProvisioningProvider – but nothing helped. The default groups are always created after i the web is provisioned.

The Trick

I wouldn’t write about it, if there is no solution. So finally i got a clue from sharepointcommunity.de which helped me. In the event receiver you simply call the function to create the default groups and the rename each one and also assign another role definition. So SharePoint needs the default groups, but it provides to change them.

So let me show you how to do:



//first create default groups
rootWeb.CreateDefaultAssociatedGroups(rootWeb.Site.Owner.LoginName, rootWeb.Site.Owner.LoginName, rootWeb.Title);

//Get the Member Group
SPGroup dMember = rootWeb.Groups[rootWeb.AssociatedMemberGroup.Name];
//Change name
dMemberDel.Name = web.Title + " Member with Delete Rights";
//Get Role assignment
SPRoleAssignment roleAssignMember = web.RoleAssignments.GetAssignmentByPrincipal(dMember);
//Remove Binding if you like
roleAssignMemberNoDel.RoleDefinitionBindings.RemoveAll();
//Add your own binding
roleAssignMemberNoDel.RoleDefinitionBindings.Add(roleDefContribute);
//Update Roleassignment
roleAssignMemberNoDel.Update();
//Group Update
dMemberDel.Update();


After the site is created, it works really fine. If you saw the web.associatedgroups property – that’s the quicklaunch of the group settings page. You can add your custom group to it. Really cool.

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

1 comment on “Sharepoint overwrite default groups by code behind

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.