Question

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..

Was it helpful?

Solution

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

OTHER TIPS

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/

(Edit 2017-01-24: Looks like the blog is down..)

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

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

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!

you can do this with REST methode function UpdateList(){
$.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getbytitle('Pages')/Fields(guid'f941f8cf-65d5-4cdb-bc6b-6fab7ea92bc3')/setshowindisplayform(false)", type: "POST", contentType: "application/json;odata=verbose", headers: { "Accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "X-HTTP-Method": "MERGE", "If-Match": "*" }, success: function (data) { alert('Success'); window.location.href=window.location.href; }, error: function (data) { console.dir(data); } }); } UpdateList();
get the script

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top