Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

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

0.00/5 (No votes)
17 Aug 2015CPOL 8.2K  
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)