Click here to Skip to main content
15,890,897 members
Articles / Programming Languages / Javascript

Read & Update Display Names of List Columns through JavaScript in SharePoint

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 Aug 2015CPOL 8.1K   1  
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.

JavaScript
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.

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --