Is it possible to hide a field in list when new item is created, but show it when the item is edited?
I figured out how to hide a field, but it will hide the field in the form no matter it is New/Edit.
thanks
Is it possible to hide a field in list when new item is created, but show it when the item is edited?
I figured out how to hide a field, but it will hide the field in the form no matter it is New/Edit.
thanks
Use SPField.ShowInNewForm and SPField.ShowInEditForm properties to control the visibility of fields in form pages.
PowerShell example
$web = Get-SPWeb http://intranet.contoso.com
$list = $web.Lists["Tasks"]
$field = $list.Fields["TaskStatus"]
$field.ShowInNewForm = $False
$field.ShowInEditForm = $True
$field.Update()
$list.Update()
function setFieldVisibility(listTitle,fieldTitle,showInNewForm,showInEditForm,showInDisplayForm, Success,Error) {
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle(listTitle);
var field = list.get_fields().getByTitle(fieldTitle);
field.setShowInNewForm(showInNewForm);
field.setShowInEditForm(showInEditForm);
field.setShowInDisplayForm(showInDisplayForm);
field.update();
clientContext.executeQueryAsync(function() {
Success();
},
Error
);
}
setFieldVisibility('Tasks','Start Date',false,false,true, function(){
console.log('Field visibility properties have been updated');
},
function(sender, args) {
console.log('Error:' + args.get_message());
}
);
I have lost few hours trying to do this with CSOM but I figure it out so if you want to use CSOM approach Sane.Amit answer will trow an error "Additional information: The node to be inserted is from a different document context."
The reason is in this line fld.Update();
If you use SetShowInEditForm()
, SetShowInNewForm()
or SetShowInDisplayForm()
the field XML definition gets updated by these methods so calling field Update()
trows an error because you are trying to update same XML twice for the same field instance, also you will get the same error if you use SetShowInEditForm()
and SetShowInNewForm()
and then you ExecuteQuery()
.
So the correct way to update some field is:
Field fld = fldColl.GetByTitle('name of the field');
fld.SetShowInEditForm(false);
cc.ExecuteQuery();
fld.SetShowInNewForm(false);
cc.ExecuteQuery();
fld.SetShowInDisplayForm(false);
cc.ExecuteQuery();
fld.Title = "Some New Title";
fld.Description = "Some New Description";
fld.Update();
cc.ExecuteQuery();
You may do something like this too.
ClientContext cc = new ClientContext(siteURL);
List li = cc.Web.Lists.GetByTitle('XYZ');
FieldCollection fldColl = li.Fields;
cc.Load(fldColl);
cc.ExecuteQuery();
Field fld = fldColl.GetByTitle('name of the field');
fld.SetShowInEditForm(false); //SetShowInNewForm(false) for new form
fld.Update();
cc.ExecuteQuery();