Tell me more ×
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

3 Answers

up vote 4 down vote accepted
+50

I am assuming you provisioned the column as a site column. So, 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

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

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

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.