Tuesday, January 8, 2013

Creating Sharepoint Security Groups and Adding People to it via Code

Hi Readers

In this post I'll explain how to Create security groups using C# code and add users to the group.

//Get the specific site
using (SPSite site = new SPSite("http://mytestsite"))
{
    using (SPWeb web = site.OpenWeb())
    {
        //Create the user group with the name test group
        //The default user and the description will have to be given
        web.SiteGroups.Add("Test Group", web.Users["abc\\user"], null, "This is a test group");
        //Add a user to the group
        //The other parameters are given emplty strings because they are not required
        web.SiteGroups["Test Group"].AddUser("abc\\user2", "", "", "");
    }
}

This is really simple.

Leave a comment if you have any queries

-Guruparan-