Thursday, August 13, 2015

Read & Update Display Names of List Columns through Javascript in Sharepoint

We can Read Columns (Internal Name, Display Name / Title, Type etc) & also Update Columns (Display Name / Title) of a List in Sharepoint ( 2010 & above ) using Client Object Model.

Below is the sample code in javascript to read Columns for a List.
 function retrieveAllFieldsOfList(listName) {  
   var ctx = SP.ClientContext.get_current();  
      var web = ctx.get_web();  
      var list = web.get_lists().getByTitle(listName);  
      var fields = list.get_fields();  
      ctx.load(fields, 'Include(Title,InternalName)');  
      ctx.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failure));  
 }  
 function Success()  
 {            
      var _fields = '';  
      var lEnum = fields.getEnumerator();  
      while(lEnum.moveNext())  
      {  
           var internalName = lEnum.get_current().get_internalName();   
           var title = lEnum.get_current().get_title();  
           _fields = _fields + 'Internal Name:' +internalName+ ',Title:' +title+ ';';  
      }  
      console.log(_fields);  
 }  
 function Failure(sender, args)  
 {  
      console.log("Failed" + args.get_message());  
 }  

Below is the Sample Code in javascript to Update Title / Display Name of a Field in a Sharepoint List
 function updateMonths(listName){  
      var ctx = SP.ClientContext.get_current();  
      var web = ctx.get_web();  
      var list = web.get_lists().getByTitle(listName);  
      //Below field section to be repeated for multiple field updates  
      var field = list.get_fields().getByInternalNameOrTitle('<Internal Name / Existing Title>');  
      field.set_title('<New Title>');  
      field.update();  
      ctx.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failure));  
 }  
 function Success()  
 {            
      console.log('Updated Successfully.');  
 }  
 function Failure(sender, args)  
 {  
      console.log("Failed" + args.get_message());  
 }  

Tuesday, August 11, 2015

Set Current User as default value in People Picker Sharepoint

We can't directly set default values for People picker fields in Sharepoint. We can achieve it using SPServices.

Below is the sample code to set logged in user name as default value for a People picker field

 <script src="../../Style Library/scripts/jquery.min.js" type="text/javascript"></script>  
 <script src="../../Style Library/scripts/jquery.SPServices-0.7.2.min.js" type="text/javascript"></script>  
 <script type="text/javascript">  
   $( document ).ready(function() {  
        //To set logged in user name as default value in a people picker field with display name, "Requested by"  
           //People Picker Field Display Name  
           var fieldName = 'Requested By';  
           //Check if the field is empty. As, this script gets executed each time the page loads(Could be during validations)  
           if(($().SPFindPeoplePicker({peoplePickerDisplayName: fieldName}).currentValue).trim() == '')  
           {  
                //Get current Logged in User  
                var currentUser = $().SPServices.SPGetCurrentUser({  
                                         fieldName: "Name",  
                                         debug: false  
                                    });  
                //Set logged in user name to the people picker   
                $().SPFindPeoplePicker({  
                      peoplePickerDisplayName: fieldName,  
                     valueToSet: currentUser,  
                     checkNames: true  
                });  
           }  
   });  
 </script>  

Access this link to know more about the SPServices method 'SPFindPeoplePicker' used to set people picker

Don't forget to check if the field value is empty before you set, as the end user might have changed the name and when the user clicks on Submit, if there are any validation errors, page reloads and the script gets executed.