Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a column that we will call C. This Column is associated to a Content Type (CT).

If I go to the properties of the CT -> Column C I can set the column to be Facoltative or Hidden:

If I put it to Hidden I cannot see it at All, if I set it to Facoltative I see it in the New Form.

The problem I'm facing is I want that field displayed only in Display Mode (neither in Edit or New).

I tried code like this:

        var ct = web.ContentTypes["CT"];
        ct.Fields["C"].ShowInEditForm = false;
        ct.Fields["C"].ShowInNewForm = false;
        ct.Fields["C"].ShowInDisplayForm = true;
        ct.Fields["C"].ShowInViewForms = true;
        ct.Update(true);

        SPField rag = web.Fields.GetFieldByInternalName("C");
        rag.ShowInEditForm = false;
        rag.ShowInNewForm = false;
        rag.ShowInDisplayForm = true;
        rag.ShowInViewForms = true;
        rag.Update(true);

but even if this works for other fields, for this it isn't working. It displays me the field in View, it doesn't in Edit but in New the column "C" is showed!

Can anybody help me? I'm trying and retrying but I cannot understand WHY it continues to show me in the new form this damned column!

Thank you very much

EDIT: the code posted above works for new list but NOT for the old one. How can I update the old one? I tried to fix every single list acting on the SPList.Fields and SPList.ContentType but doesn't works as expected! P.S.: I configure some parameters from the UI for the visibility of the fields..

share|improve this question
    
What about if i just want to check if the SP.Field does show in Display form or not ? I do not want to set the value of it ? any thoughts on that. setShowInDisplayForm() is a property i can set true of false using JSOM. but i want to show only fields in the Display Form without changing anything or property. thanks, –  moss Feb 28 at 21:03

4 Answers 4

up vote 6 down vote accepted
+50

I'm assuming you provisioned the column as a site column. The next thing is to make sure to push the changes to the down to lists and content types where this site column has been referred (which apparently you haven't done it).

So, I would recommend to delete the old definition and provision the column again!

See an example below where I am using the script to modify the ShowInEditForm and other properties of a site column called Test Column, which then can be added to document libraries and lists directly, or through a content type.

Note that the new ShowInEditForm and other properties settings will apply to all document libraries, lists and content types where the site column has been added:

#Get the web and site column objects
$web = Get-SPWeb http://portal
$column = $web.Fields["Test Column"]

#Set the PushChangesToLists property for the changes to be applied
#to lists where the column has already been added
$column.PushChangesToLists = $true

#Change the ShowInEditForm and other properties and update objects
$column.ShowInEditForm = $false
$column.ShowInNewForm = $false
$column.ShowInDisplayForm = $true
$column.ShowInViewForms = $true

$column.Update()
$web.Update()
$web.Dispose()
share|improve this answer
    
Can't tell you how many times the PushChangesToLists property have helped me! –  George Norberg Oct 9 at 8:57

I think you might be missing the fact that you need to update the CT after you have updated the field, also I normally don't declare the C as var but rather a CT.

See the code below:

(Also make sure the internal name "C" is unique to that field and not mixed with a required field in another CT.)

Best of luck

SPContentType ct = web.ContentTypes["CT"];
ct.Fields["C"].ShowInEditForm = false;
ct.Fields["C"].ShowInNewForm = false;
ct.Fields["C"].ShowInDisplayForm = true;
ct.Fields["C"].ShowInViewForms = true;
ct.Update();

SPField rag = web.Fields.GetFieldByInternalName("C");
rag.ShowInEditForm = false;
rag.ShowInNewForm = false;
rag.ShowInDisplayForm = true;
rag.ShowInViewForms = true;
rag.Update(true);

ct.Update(true);
share|improve this answer
    
Doesn't work.. I can't face why! Any other suggestion? –  Ziba Leah Nov 29 '12 at 10:38

The code you posted should work also for old lists, as it uses the Update(true) method. There is only one case I can think of, when pushing down changes from the parent Content Type to the child Content Type does not work: when the content type is either read-only, or sealed. Please read this site for more info.

Please let me know if that helped you.

Cheers!

share|improve this answer

Stefan Bauer wrote a post about this that covered it quite nicely!

I know the question have been answered but this post, in combination with this question, helped me to solve my task!

http://www.n8d.at/blog/hide-fields-from-lists-and-content-types/

The Essentials:

# First load SharePoint Core Assembly
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

$url = "http://myserver:Port";
$list = "List";
$fieldname = "NewColumn";

#Setting up context
$contextSite = New-Object Microsoft.SharePoint.SPSite($url);
$contextWeb = $contextSite.OpenWeb();

$list = $contextWeb.Lists.TryGetList($list);
$field = $list.Fields[$fieldname];

# Controls Field in Edit Form
$field.ShowInEditForm = 1;

# Controls Field in New Form
$field.ShowInNewForm = 0;

# Controls Field in New Form
$field.ShowInDisplayForm = 1;

# Hides fields from list settings
$field.ShowInListSettings = 1;

# Hides fields from version history
$field.ShowInVersionHistory = 1;

# Hides fields form selection in views
$field.ShowInViewForms = 1;

# Don't forget to update this field
$field.Update();

# And finally dispose everything.
$contextWeb.Dispose();
$contextSite.Dispose();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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