zondag 11 januari 2015

Check or Get People Picker value.


New function to check if a people picker field is empty or to get the value of a people picker field.
Functions that may be usefull to use in presave actions when editing or creating new list items. 

The function IsPeoplePickerEmpty must be called with a parameter that contains the title of your field. Or with the value of the text that stands on the left side of your people picker field. 

The function  GetPeoplePickerValue must be called with a parameter that contains the title of your field. Or with the value of the text that stands on the left side of your people picker field. It returns a string containing the key of your people picker field. This is the domain and userid of the inserted value. 

Remark. The GetPeoploePickerValue only works with a single user People Picker field. 

 
function IsPeoplePickerEmpty( titel )
{
  if (( $('nobr:contains("'+ titel +'")').closest("td").next("td").find('input')[1].value  == " ") || ( $('nobr:contains("'+ titel +'")').closest("td").next("td").find('input')[1].value  == "") )
         return true;
  else
         return false;
}
function GetPeoplePickerValue(titel)
{
    var result = "";
        if (!IsPeoplePickerEmpty(titel))
        {
                result = $('nobr:contains("'+ titel +'")').closest("td").next("td").find('input')[1].value;
                result = result.substring(result.indexOf("key=") + 5 );
                result = result.substring(0,result.indexOf("'"));
        }
    return result;
}

Set a PeoplePicker Field without a GUID !


A new function that lot's of people may help if they want to set a value of a People Picker field. 
There are examples to find on the internet how you may do it by using the GUID of the field. This because the People Picker field is made of different html parts. You can't set a value in the field directly by calling his Title property like most of the other SharePoint Fields.
But next function by calling the label of the field (the text you have giving on the screen), and a value (Full name of a person or the userid of a person)


Enjoy the new function !! 

Remark, the function only works for setting one value to the people picker field. 
test
// call the new function Set PeoplePicker.
//
SetPeoplePicker (title, userid);

function SetPeoplePicker(My_Title, userid)
{
        try{
                 var ppID = $($('nobr:contains("' + My_Title + '")').closest("td").next("td").find('div')[0]).attr('id');
                 var ppID_userfield = ppID.substring(0,ppID.indexOf("_upLevelDiv"));
                 var a_ppID = ppID_userfield.split('_');
                 var a_ppID_Length = a_ppID.length;
                 var ppID_userfield2 = a_ppID[0] + '$' +  a_ppID[1] + '$' +  a_ppID[2] ;
                 for ( i = 3; i < a_ppID_Length - 4; i++)
                 {
                       ppID_userfield2 = ppID_userfield2 + '_' + a_ppID[i];
                 }
                 ppID_userfield2 = ppID_userfield2 + '$' +   a_ppID[a_ppID_Length - 4] + '$' +   a_ppID[a_ppID_Length - 3] + '$' +   a_ppID[a_ppID_Length - 2] + '$' +   a_ppID[a_ppID_Length - 1] ;
        
                $('#' + ppID ).text(userid);
                //recognize the people picker driver name
                if(!ValidatePickerControl(ppID_userfield )){
                        ShowValidationError();
                        return false;
                }
                var arg=getUplevel(ppID_userfield );
                var ctx = ppID_userfield ;
                EntityEditorSetWaitCursor(ctx);
                WebForm_DoCallback(ppID_userfield2 ,arg,EntityEditorHandleCheckNameResult,ctx,EntityEditorHandleCheckNameError,true);
        }catch(err){
                $('#' + ppID).text("");
        }
}


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");
 

donderdag 28 november 2013

Set Focus on a sharepoint Field in scripting

To setting the focus of an input field in jquery, it was simple. But when you have a peoplepicker field
you think that you also are dealing with an input field, but less is true.
A sharepoint People Picker field isn't that easy made.
But we won't be a developer, if we could make a function that put's the focus also 
on a people picker field.
Below you find my function. It can be extended with other fields if necessary. 
You call it not by using the GUID, but by using the display name. Which is more flexible and easier, 
because you don't need to search for the GUID in the source code. 
 
You can call it by the next function. You only need the Field Display Name and the Type of field. 
The type can be at this moment, an Input field, a People Picker, or a  Select field. 

This you can use by example when you want to check values in the PreSaveAction function and 
you want to put the focus on a field that has to be changed.  
 
Have fun with it... and if you need an extention, don't hesitate to ask... 

 
function FirstfocusExtended(veldnaam, type)
{
                switch (type.toLowerCase())
                {
                               case "input" :
                                               $(":input[title='" + veldnaam + "']").focus();
                                               break;
                               case "peoplepicker" :
                                               $("nobr:contains('" + veldnaam + "')").closest("td").next("td").find("div").focus();
                                               break;
                               case "select" :
                                               $(":select[title='" + veldnaam + "']").focus();
                                               break;
                               default :
                                               break;
                }
}

dinsdag 26 november 2013

Set a check or more checkboxes of a choice field selected.


A nice new function is created today for my colleague
If you want to check a checkbox of a checkbox field or a multi choice field, 
you can use the next function. 
You can call it by not knowing the GUID of the checkbox you want to select. But you can call it with 
the Display Name (Title) of your field. 
If it only one checkbox, you call it like this :
SetChoiceFieldValue ("YOUR TITLE", 1, true);
 
If you have a choice field with more options, you can select specific entries 
by setting the index to the checkbox you want to select. 
You can use the same function also to uncheck the field. You only have to set the last param to false. 

It's a nice jquery function that makes your scripting more flexible. 


 
function SetChoiceFieldValue(titel, index, b_checked)
{
        var TD = $('nobr:contains("'+ titel + '")').closest("td").next("td").find('input');
        if (index <= TD.length + 1)
        {
                TD[index-1].checked = b_checked;
        }
}