Tuesday, September 10, 2013

Add user to SharePoint security group using SharePoint client object model

In this post we will discuss how to add a user to a SharePoint security group using SharePoint client object model. Also you can check out my previous posts on:
- Add user to SharePoint group using SharePoint web service

- Submit data from Infopath form to SharePoint list using SharePoint object model

- Continuous Crawl in SharePoint 2013 search

To work with SharePoint 2013 client object model, we need to give reference to the below dlls:
- Microsoft.SharePoint.Client.Runtime.dll

- Microsoft.SharePoint.Client.dll

These dlls are located in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\ISAPI.

Below is the full code to add user to SharePoint security group using SharePoint client object model

using Microsoft.SharePoint.Client;

ClientContext clientContext = new ClientContex("http://SiteURL");

web = clientContext.Web;
         
GroupCollection collGroup = web.SiteGroups;

Group oGroup = collGroup.GetById(5);        

UserCreationInformation userCreationInfo = new UserCreationInformation();

userCreationInfo.Email = "contact@onlysharepoint2013.com";

userCreationInfo.LoginName = @"DomainName\UserName";

userCreationInfo.Title = "Display Name";

User oUser = oGroup.Users.Add(userCreationInfo);

clientContext.ExecuteQuery(); 


Twitter Delicious Facebook Digg Favorites More