Create a site group in SharePoint 2010 using Client Object Model


In this you will see how to create a new custom site group and how to add the owner and members for the newly created group.


 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint.Client;

 

namespace COM

{

    class Program

    {

        static void Main(string[] args)

        {

            // siteURL is the string that contains the site URL

            string siteUrl = "http://serverName:50000/sites/Testing";

            // ClientContext object is used to get the context for the SharePoint objects

            ClientContext clientContext = new ClientContext(siteUrl);

            Web web = clientContext.Web;  

       

            GroupCreationInformation groupCreationInfo = new GroupCreationInformation();

            groupCreationInfo.Title = "Custom Group";

            groupCreationInfo.Description="Custom group created using Client Object Model";

 

            User owner = web.EnsureUser(@"domainName\ownerName");

            User member = web.EnsureUser(@"domainName\userName");

 

            Group group = web.SiteGroups.Add(groupCreationInfo);

            group.Owner = owner;

            group.Users.AddUser(member);

            group.Update();      

           

            clientContext.ExecuteQuery();          

        }

    }

}