Add User To Site Group in SharePoint using JSOM

The example in this topic show how to use JSOM to Add User To Site Group in SharePoint

  • function AddUserToSiteGroup() {
  •     // You can optionally specify the Site URL here to get the context
  •     // If you don't specify the URL, the method will get the context of the current site
  •     // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
  •     clientContext = new SP.ClientContext();
  •  
  •     var oGroup = clientContext.get_web().get_siteGroups().getByName("New Group");
  •  
  •     // Get user using Logon name
  •     var oUser = clientContext.get_web().ensureUser('Logon Name');
  •  
  •     //Get all Users in SP Group
  •     var oUserCollection = oGroup.get_users();
  •  
  •     // Add user to the group
  •     oUserCollection.addUser(oUser);
  •  
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •     console.log('Success');
  • }
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }

Thank you for reading this article. This code was tested in SharePoint 2013


© 2019 Code SharePoint