Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to change the list column name. Here is my code, it isn't working. I added a content editor web part with this code:

<script type="text/javascript">
//js ref
</script>
<script type="text/javascript">

 ExecuteOrDelayUntilScriptLoaded(initialize, "sp.js");  

 //Get our objects
 function initialize(){
  var context = new SP.ClientContext.get_current();        
  var web = context.get_web();                       
  var list = web.get_lists().getByTitle("News");  
  var field = list.get_fields().getByInternalNameOrTitle("testar");

  context.load(field,"testar");

   context.executeQueryAsync(Function.createDelegate(this,onListDataSucceeded),Function.createDelegate(this, onListDataFailed)); 

function onListDataSucceeded(sender, args){
field.set_title("NewName");
field.update();
};

function onListDataFailed(sender,args){
alert('List Data fetch failed. ' + args.get_message() + 'n' + args.get_stackTrace());
};
};
</script>

the error i get is:

List Data fetch failed. The field or the proeprty testar does not exist.nundefined

share|improve this question

3 Answers 3

up vote 2 down vote accepted

First, your load is wrong like MdMazzotti pointed out. But you don't update your field, you'll have to load it into your context again and executeQueryAsync again inside your success function. But you could just load everything up in initialize():

     function initialize(){
      var context = new SP.ClientContext.get_current();        
      var web = context.get_web();                       
      var list = web.get_lists().getByTitle("News");  
      var field = list.get_fields().getByInternalNameOrTitle("testar");

      field.set_title("NewName");
      field.update();


      context.load(field);

       context.executeQueryAsync(Function.createDelegate(this,onListDataSucceeded),Function.createDelegate(this, onListDataFailed)); 
     }
share|improve this answer
    
Thanks so much, its working now. Im gonna take some hours to learn more of the csom in sharepoint –  Jarule Mar 26 at 11:59

Change this line

context.load(field,"testar");

to this:

context.load(field);
share|improve this answer
    
i tryied it but nothing happens and no error.. –  Jarule Mar 26 at 10:52

Try getting the Field Schema and updating the field name in it. Example:

var ctx = SP.ClientContext.get_current(),              //SP.ClientContext
    field = ctx.get_web()                              //SP.Web
               .get_lists()                            //SP.ListCollection
               .getByTitle('MyList')                   //SP.List
           .get_fields()                           //SP.FieldCollection
           .getByInternalNameOrTitle("Site");      //SP.Field
ctx.load(field, "SchemaXml");                          //load only SchemaXml
ctx.executeQueryAsync(function() {
    var s = field.get_schemaXml(),
        s1 = s.replace('Title="PREVIOUSTITLE"', 
                           'ShowInDisplayForm="NEWTITLE"');
    field.set_schemaXml(s1);
    field.update();
    ctx.executeQueryAsync();
});

See more at http://chuvash.eu/2012/09/24/jsom-alter-a-columns-showindisplayform-property/

See this for Filed properties http://msdn.microsoft.com/en-us/library/office/ms437580(v=office.15).aspx

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.