Oct 17, 2013

Create groups with JavaScript in SharePoint 2010

All credit goes to Yesh Agrawal for this: http://yeshagrawal.blogspot.com.es/2013/05/create-sp-group-and-assign-permission.html

Here's the code (edited to only create the group, not assign permissions):
<script >
function createSPGroups()
{
var grupos = new Array("group1",
"group2");

//Load new Site
   var currentCTX = new SP.ClientContext();
   var currentWEB  = currentCTX.get_web();

   //Get all groups in site
   var groupCollection = currentWEB.get_siteGroups();

  for(var i = 0; i < grupos.length; i++) { 
   var grupo_nombre = grupos[i];
   var grupo_desc = "Description for "+grupos[i]; 
   // Create Group information for Group
var membersGRP = new SP.GroupCreationInformation();
membersGRP.set_title(grupo_nombre);
membersGRP.set_description(grupo_desc);


//add group
oMembersGRP = currentWEB.get_siteGroups().add(membersGRP);



currentCTX.load(oMembersGRP);
}

//Execute Query
currentCTX.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
            alert("Done");
}

function onQueryFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
  
<button onclick="createSPGroups(); return false;">Crear grupo</button>

No comments:

Post a Comment