2

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

1
  • Your using content type in content type?
    – Kamlesh
    Commented Apr 2, 2014 at 5:36

3 Answers 3

5

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()

CSOM (JavaScript) example

 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());
    }
);
4
  • You were faster :) Commented Apr 2, 2014 at 5:41
  • Oh, sorry about that ;) Commented Apr 2, 2014 at 5:42
  • thanks. is there a way to do this from site content (web UI)?
    – urlreader
    Commented Apr 2, 2014 at 14:08
  • There is no way to update those properties via UI as far as i know, but you could perform it using JavaScript if you will (see an updated answer) Commented Apr 2, 2014 at 14:29
6

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();
2
  • Oh boy, this is something. Can I buy you a coffee for this finding, it literally saved the day.
    – user656
    Commented Apr 8, 2015 at 17:05
  • In Client Side Object Model libraries version 16.1.3912.1204 this issue is fixed, so you don't need to do ExecuteQuery() after every SetShowIn...
    – CalESharE
    Commented Oct 15, 2015 at 9:11
4

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();
1
  • 1
    +1 for the CSOM approach :) Commented Apr 2, 2014 at 6:16

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.