JSOM: Alter a column's ShowInDisplayForm property
By Anatoly Mironov
When you create a content type, you can define if your fields will be shown in DisplayForm, EditForm, NewForm. You can hide or show them, just as Yaroslav Pentsarsky says. If your list is already provisioned, you can change them with Server Object Model, why not in PowerShell: Technet: Setting ShowInDisplayForm, ShowInEditForm, ShowInNewForm properties with powershell. If you don’t have access to the server, then the same can be done with JSOM. Here is the code:
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('ShowInDisplayForm="FALSE"',
'ShowInDisplayForm="TRUE"');
field.set\_schemaXml(s1);
field.update();
ctx.executeQueryAsync();
});
Next: JSOM: Alter a column’s DisplayName.
Comments from Wordpress.com
Anatoly Mironov - May 2, 2013
What do you try to do? Why is the guid empty first? Have you tried some other change, like ShowInDisplayForm (from my example)? Just to verify that everything else works.
Hey, Thanks for quick reply. When I pasted my code here this editor is actually hiding some xml markup. Anyhow I am trying to update the SspId, TermSetId property values for the field. I was able to change all the attribute values of the SP.field that is anything from below but could not change anything inside I am trying to use set_schemaXml method
Allright, if you can update all other properties, but not SspId, TermSetId, then it is something else that prevent updating it. I have not tried to update those properties with set_schemaXml. Maybe in the future, I’ll find time to investigate why it is so. If you find something, please leave a comment here.
Hi, Please find the below code var ctx = SP.ClientContext.get_current(), field = ctx.get_site() .get_rootWeb() .get_fields() .getByInternalNameOrTitle(“Product Type”); ctx.load(field, “SchemaXml”); ctx.executeQueryAsync(function () { var s = field.get_schemaXml(); $(’#resultpanel’).append(’****************************** original: ’ + s); s1 = s.replace(‘00000000-0000-0000-0000-000000000000’, ‘{655ba3db-79a3-4982-a295-54b4a4e51924}’); s2 = s1.replace(‘00000000-0000-0000-0000-000000000000’, ‘{070aef21-9b04-4f49-3595-9ffca54ef999}’); $(’#resultpanel’).append(’****************************** updating with: ’ + s2); field.set_schemaXml(s2); field.update(); ctx.executeQueryAsync(Function.createDelegate(this, function () { $(’#resultpanel’).append(’****************************** after Update from get_schemaXml: ’ + field.get_schemaXml()); }), Function.createDelegate(this, function (sender, args) { $(’#resultpanel’).append(’ Error - from OnLoadSuccess: ’ + args.get_message()); })); }); I am not finding any change in the field after i run this code. You see any mistake in my code. In the success delegate, from field.get_schemaXml() i can see the updated schema. But still its not getting reflected on the field. Any help is appreciated…
I can’t get this working in my Office365 dev environment. Is it possible to set the XML schema in office 365?
Hi, it should work in Office 365, too. I haven’t tried it out, though. When you try to do in, check the response you get from the ajax call. You can see it in the Developer tools of your browser. Often you’ll get a good error message there.
I’ve made a mistake in my provisiong scripts and all of my custom get_web().get_fields() are with incorrect WebId values, so are all of my custom content types that contain them. I’ve already updated the Fields in my web FieldCollection, but my ContentTypes remains incorrect. Problem is that get_web().get_contentTypes() just exposes a setter for schemaXmlWithResourceTokens method. Any idea on how can replace Contettype SchemaXml? Thank u in advance!
Thanks for sharing. Good post.