0

I want to update a Column Name using a Calculated Column. In my attempts below, I'm trying to rename the Column name from Col2change to NewName. None of the code snipperts work.

I do not have access to Workflows, etc. the only method I have available is via Calculated Columns.

Can anyone see the problem with my code?

=" <img src=""http://Oursite/blank.gif"" onload=""{"
&"var clientContext=new SP.ClientContext.get_current();"
& "var list=clientContext.get_web().get_lists().getById (SP.ListOperation.Selection.getSelectedList());"
&" var field = list.get_fields().getByInternalNameOrTitle(""Col2Change"");" 
&" field.set_title(""NewName"");"
&" field.update();"
&" context.load(field);"
&" context.executeQueryAsync(); "
& "    }"">"


=" <img src=""http://Oursite/blank.gif"" onload=""{"
&"var ctx = SP.ClientContext.get_current();"              
&"  field = ctx.get_web()                              "
&"              .get_lists()                            "
&"               .getByTitle('My List')                  "
&"           .get_fields()                           "
&"           .getByInternalNameOrTitle(""Col2change"");      "
&"ctx.load(field, ""SchemaXml"");                        "
&"ctx.executeQueryAsync(function() {"
&"    var s = field.get_schemaXml(),"
&"        s1 = s.replace('Title=""Col2change""', "
&"                           'ShowInDisplayForm=""NEWNAME""');"
&"    field.set_schemaXml(s1);"
&"    field.update();"
&"    ctx.executeQueryAsync();"
& "    } ) }"">"

=" <img src=""http://Oursite/blank.gif"" onload=""{"
&"var ctx = SP.ClientContext.get_current();"              
&"  field = ctx.get_web()                              "
&"              .get_lists()                            "
&"               .getByTitle('My List')                  "
&"           .get_fields()                           "
&"           .getByInternalNameOrTitle(""Columnname"");      "
&"ctx.load(field, ""Title"");                         "
&"ctx.executeQueryAsync(function() {"
&"    field.set_title(""UpdatedColumnname"");"
&"    field.update();"
&"    ctx.executeQueryAsync();"
& "    } ) }"">"

=" <img src=""http://Oursite/blank.gif"" onload=""{"
&"var clientContext = SP.ClientContext.get_current();" 
&"var  List list = clientContext.Web.Lists.GetByTitle(""My List"");"
&" clientContext.Load(list);"
&" clientContext.ExecuteQuery();"
&"  var fields = list.Fields;"
&" clientContext.Load(fields);"
&" clientContext.ExecuteQuery();"
&" var field = fields.GetByInternalNameOrTitle(""ColumnName"");"
&" var field.Title = ""NewColumnName"";"
&" field.Update();"
&" clientContext.Load(field);"
&"clientContext.ExecuteQuery();"
& "    }"">"
7
  • 1
    Is your code block supposed to be the value for the calculated column? I really hope I never find anything like this in a farm I have to work on...
    – John-M
    Commented May 21, 2015 at 3:10
  • The code is trying to update the Column name: (When you go into the field Settings : Edit Column it is the first value "Type a name for this column.".) Control is indeed very tight on our farm, our tools are veeery limited. I'm not changing the value of what is in a column row, just the column name.
    – southskies
    Commented May 21, 2015 at 3:44
  • I'm not changing the value of what is in a item row. This above code goes into the calculated column field, which (when executed after an item update) will update the Column name of a different column. This is the intention.
    – southskies
    Commented May 21, 2015 at 3:59
  • Updated the code above with another version, but this doesn't work either
    – southskies
    Commented May 21, 2015 at 5:32
  • To me, this totally makes no sense at all. Update an internal field name based on a calculated column, what does that mean? What is the use case?
    – MdMazzotti
    Commented May 21, 2015 at 7:31

1 Answer 1

0

Using a calculated column isn't really a good way to solve the problem you're describing...

You suggested that you want to change or reformat the column header based on a value change in a list.

The primary (logical) problem with your idea of using calculated columns is that even if you were able to get the calculated column to set itself to the value of some script that would work to change the column heading, that script would still need a person (with the right permissions to change column headings) to view the page where the calculated column value was displayed. The update would never occur automatically.

A second and more immediate problem is that this system is quite complicated and would be very difficult to maintain over time.

If you cannot create an event reciever (farm solution or remote) to perform this logic for you, I would seriously reconsider your chosen solution for the higher level problem you're attempting to solve.

1
  • Hi John, thanks for that information - it does make sense. I was hoping that updating the column would also help me sort out my problem that I logged with [link] (stackoverflow.com/questions/30320478/e)
    – southskies
    Commented May 22, 2015 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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