Posts tonen met het label Client Object Model. Alle posts tonen
Posts tonen met het label Client Object Model. Alle posts tonen

zaterdag 13 september 2014

Create a Sharepoint Group and add a PermissionLevel to that new Sharepoint Group


In a previous blogpost you saw how to break the inheritance of a site. Well by completing the request
of my customer, I wrote some code for also creating a own Sharepoint Group and give it 
a specific permission level. 

Enjoy my next custom code for creating a group and adding a permission level to it. 

  

      
 

 

function addGroup( )
{
   var groupsname = "FILL IN HERE THE NAME OF YOUR SHAREPOINT GROUP";
   var groupsOwner = "DOMAIN/USERID";
   var groupsDesc = "FILL IN HERE THE DESCRIPTION FOR YOUR SHAREPOINT GROUP"; 
   var soapEnv ="";
   soapEnv =            '';
   soapEnv = soapEnv  + '';
   soapEnv = soapEnv  + '';
   soapEnv = soapEnv  + '';
   soapEnv = soapEnv  + ''+ groupsname + '';
   soapEnv = soapEnv  + '' + groupsOwner + '';
   soapEnv = soapEnv  + 'user';
   soapEnv = soapEnv  + '' + groupsOwner + '';
   soapEnv = soapEnv  + '' + groupsDesc + '';
   soapEnv = soapEnv  + '  ';
   soapEnv = soapEnv  + '';
 
  $.ajax({
        url: "URL OF YOUR SITE BY STARTING HTTP /_vti_bin/UserGroup.asmx",
        type: "POST",
 async : false,
        dataType: "xml",
        beforeSend: function(xhr) {
           xhr.setRequestHeader("SOAPAction",
               "http://schemas.microsoft.com/sharepoint/soap/directory/AddGroup");
           },
        data: soapEnv,
        error: processMyError,
        complete: processResultAddGroep,
        contentType: "text/xml; charset=\"utf-8\""
  }); 

}


function processMyError(xhr, error)
{
  alert(xhr);
  alert("dd "+error);
}


function processResultAddGroep (xData, status)
{ 
// adding the Permissionlevel to your group. If you don't do this, the Sharepoint Group will be created, 
// but will not be visible in the list of Groups. 
  callAddRole();
}


function callAddRole ()
{
  var soapEnv ="";
  var groupsName = "FILL IN HERE THE NAME OF YOUR SHAREPOINT GROUP";
  var roleName = "FILL IN HERE THE NAME OF THE PERMISSION LEVEL, EX Contribute";

  soapEnv =            '';
  soapEnv = soapEnv  + '';
  soapEnv = soapEnv  + '';
  soapEnv = soapEnv  + '';
  soapEnv = soapEnv  + '' + groupsName  + '';
  soapEnv = soapEnv  + '' + roleName +  '';
  soapEnv = soapEnv  + '  ';
  soapEnv = soapEnv  + '';
  $.ajax({
         url: "FILL IN HERE THE URL OF YOUR SITE /_vti_bin/UserGroup.asmx",
         type: "POST",
         async: false, 
         dataType: "xml",
         beforeSend: function(xhr) {
                          xhr.setRequestHeader("SOAPAction",
                          "http://schemas.microsoft.com/sharepoint/soap/directory/AddGroupToRole");
                     },
         data: soapEnv,
         error: processMyError,
         complete: processResultAddRole,
         contentType: "text/xml; charset=\"utf-8\""
  });

}

function processResultAddRole(xData, status)
{ 
  alert("status: " + status);
  $('#error').val(status);
  alert("response: "+ xData.responseText);
  $('#errormessage').val(xData.responseText);

} 

woensdag 10 september 2014

Break site permission inheritance in code


Break site permission inheritance in code

Sometimes your want to allow your customer to create a site or a subsite with a simple click. 
And you want that same click to break the permission inheritance of its parent site.

Well, it has taken me some research but I have found a solution to do exactly that in code. 
Just put this code on a Sharepoint page. 
Or include the Sharepoint client object model into your custom page.

Enjoy the power of scripting ! 

  

      
 
function BreakRole()
{
   var copyRoleAssignments = true;
   var clearSubscopes = true;
   var clientContext = new SP.ClientContext.get_current();
   // or if your want to break another sites inheritance
   // var clientContext = new SP.ClientContext("/sites/YourSitecollection/yourSite");
   var oWebsite = clientContext.get_web();
   var $v_0 = new SP.ClientActionInvokeMethod(oWebsite, 'BreakRoleInheritance', [ copyRoleAssignments, clearSubscopes ]);
   clientContext.addQuery($v_0);
   clientContext.executeQueryAsync();
}