2

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

3

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)); 
     }
  • Thanks so much, its working now. Im gonna take some hours to learn more of the csom in sharepoint – Anders Mar 26 '14 at 11:59
2

Change this line

context.load(field,"testar");

to this:

context.load(field);
  • i tryied it but nothing happens and no error.. – Anders Mar 26 '14 at 10:52
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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