Add Users to Group in SharePoint 2013 using CSOM

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. namespace GowthamSamples  
  8. {  
  9.     class Program   
  10.     {  
  11.         static void Main(string[] args)  
  12.       {  
  13.             // ClientContext - Get the context for the SharePoint Site  
  14.             ClientContext clientContext = new ClientContext("http://gauti.sharepoint.com/sites/sp1/");  
  15.             GroupCollection Groups = context.Web.SiteGroups;  
  16.             // Assume that there is a "Owners" group, and the ID=7.  
  17.             Group ownersGroup = Groups.GetById(7);  
  18.             // Let's set up the new user info.  
  19.             UserCreationInformation userCreationInfo = new UserCreationInformation();  
  20.             userCreationInfo.Email = "[email protected]";  
  21.             userCreationInfo.LoginName = "sharepointdev\\gowtham";  
  22.             userCreationInfo.Title = "User";  
  23.             // Let's add the user to the group.  
  24.             User newUser = ownersGroup.Users.Add(userCreationInfo);  
  25.             context.ExecuteQuery();  
  26.         }  
  27.     }  
  28. }  
Thanks for reading my Blogs!