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:

[sourcecode language=”csharp”]

//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();

[/sourcecode]

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.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

1 Comment

Leave a Reply

Your email address will not be published.


*