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();
}
 

zondag 7 september 2014

Trigger click event in your aspx page


How to trigger easely an event on your Sharepoint ASPX page by using JQuery. 
Well, it doesn't need very mutch to trigger automatically by example the save event
by using your scripting language.For example if somebody fills all required fields in 
and you detected that is was filled in correctly... you can trigger the save event without asking
the user to click on the save button. And this saves time for your customer. 

A simple line of code that you can put in your javascript 
Don't forget that you have to import your JQuery library ! 
  

      $("input[value='Save']").trigger("click");