Programmatically Hiding a Content Type Field

Here is my scenario,
I have 2 content types (CT1 and CT2). CT2 is created programmatically using PowerShell by the lines of code given below,
$CT1= $spWeb.ContentTypes["My Content Type 1"]
$CT2 = $spWeb.ContentTypes["My Content Type 2"]
if($CT2 -eq $null)
$CT2 =New-Object Microsoft.SharePoint.SPContentType -ArgumentList @($CT1, $spWeb.ContentTypes, "My Content Type 2");
$CT2.Group = "My Group";
$CT2.Description = "Some Description for CT2.";
$spWeb.ContentTypes.Add($CT2);
I want to hide some of the columns that I inherited from CT1 in CT2. When I use the below code for hiding columns, I get 'Exception setting "Hidden": "Cannot
change Hidden attribute for this field"'
<pre lang="x-powershell">$field = $CT2.Fields["MyField"] #MyField - I want to hide this field
$field.Hidden = $true;
$field.Update()
After I looked deeper into the problem, I found that, CanToggleHidden property for the field is set to 'False'. Hence, I decided to change the flag using reflection by using the lines below,
$field = $CT2.Fields["MyField"] #MyField - I want to hide this field
$bindingFlags = [Reflection.BindingFlags] "NonPublic,Instance"
[System.Type] $type = $field.GetType();
[Reflection.MethodInfo] $mdInfo = $type.GetMethod("SetFieldBoolValue",$bindingFlags);
$object = [System.Object] @("CanToggleHidden",$true);
$mdInfo.Invoke($field,$object);
$field.Hidden = $true;
$field.Update()
This time, I did not get error at $field.Hidden but at $field.Update() method call, I get 'Exception calling "Update" with "0" argument(s): "This functionality
is unavailable for fields not associated with a list."'
Suprisingly from UI (Site Settings -> Site Content Types), I can browse to 'CT2' content type and hide columns that I don't want!!!!
My questions is, Is there a way to hide unwanted columns programmatically?
SSK

You should reference the field you want to hide not from the Fields collection, but from the FieldLinks one.
Try to change powershell string:
$field = $CT2.Fields["MyField"]
to this one:
$field = $CT2.FieldLinks["MyField"]
After it, the script should work without exceptions.

Similar Messages

  • Deleting sharepoint list content type Fields

    Hi
    Please tell me the code for deleting all the fields inside content type fields..This is my code i used,but its not deleting the fields.
     SPSite spsite = new SPSite("http://ghfjhhk18:8001");
                SPWeb web = spsite.OpenWeb();
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists["Test"];
                SPContentTypeCollection cts = list.ContentTypes;
                SPContentType ct = cts["Item"];
               // ct.FieldLinks.Delete("Expires");
                //ct.Update();
                SPFieldCollection contentTypeFieldCollection = ct.Fields;
                foreach (SPField spField in contentTypeFieldCollection)
                    if (ct.Fields.ContainsField(spField.Title))
                        //SPFieldLink fieldLink = new SPFieldLink(spField);
                        ct.FieldLinks.Delete(spField.Title);
                    //myContentType = null;
                ct.Update();
                web.AllowUnsafeUpdates = false;
            }but this code is not deleting the fields..
    pls help me as soon as possible.
    One friend told me to do for loop in reverse..but how can i accomplish that

    Hi,
    Please refer the below code snippet which may help you to fix your issue. If not please refer the below links also
    http://ptsharepoint2010.blogspot.in/2011/12/programmatically-remove-fields-from.html
    http://www.c-sharpcorner.com/uploadfile/54db21/delete-field-content-type-in-sharepoint-2010-programmaticall/
    using (SPWeb web = site.OpenWeb())
    SPList spList = web.Lists["<ListName>"];
    SPContentTypeCollection spCTS = spList.ContentTypes;
    SPContentType spCT = spCTS["YourContentType"];
    spCT.FieldLinks.Delete("YourField");
    spCT.Update();
    If its not helping you, please let us know
    Sekar - Our life is short, so help others to grow
    Whenever you see a reply and if you think is helpful, click "Vote As Helpful"! And whenever
    you see a reply being an answer to the question of the thread, click "Mark As Answer

  • Content Type field

    Is it possible to remove the hyphen between the Type and the Description in the Content Type field?
    Or maybe someone can recommend a good way to utilize it in it's current state. In most cases we currently have something like 'Flowcharts' without a description and it would be nice to be able to remove the hyphen.
    Thanks

    Hi,
    there is a very easy way to do this, without any component:
    Open the ConfigurationManager Applet and select the Views Tab.
    In the Views Tab search for the View docTypes and click Edit
    In the Edit-Dialog Replace the Default Display Expression with
    <$dDocType$> <$if dDescription$>-<$endif$> <$dDescription$>
    The hyphen will only be displayed if there is a Description.
    Greets Thorsten

  • Error while adding content type fields in the EditForm.aspx for an customlist item.

    This is office 365. I have content type which is attached to custom list.
    Added new site column "CustomerAction" which was added to Content type, now when I added this field in the EDITForm.aspx using the SharePoint designer, this form is called when the custom list item is being edited.
    But when I added this extra field to EDITFORM.aspx it is giving error,. the standard correlationid error.
    Can anyone suggest, what could be problem !
    Thanks
    Labhesh
    Labhesh Shrimali

    Hi Labhesh,
    When we add the site column to the content type, there is an option to update the all
    content types which inheriting from this type.
    If we set this to Yes, then the site column will be added to the list where the content type or its child content type has been used and this column will be added to the EDITFORM.aspx too.
    So we don’t need to re-add this site column to EDITFORM.aspx.
    Thanks,
    Victoria
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Victoria Xia
    TechNet Community Support

  • Assigning External content type field column value using Client Object Model

    I have a problem assinging External column value to ListItem object with client object model-based application I'm developing. To be precise, I am able to retrieve data related to external content type by reading external list created from this content type
    but I don't know how to properly use it to assign value to this field. By doing some research on my own I concluded that BDC ID column from external list is the way to go since it uniquely defines selected row from external list but that doesn't
    tell me much since I don't know what to do with it. Currently I ended up with partial solution - to assign plain string value of picker column but that makes this value visible only in "View Properties" option on Sharepoint and not in "Edit Properties"
    which pritty much makes sence since it isn't properly related to rest of the data in specific row. Does someone have a better solution for this?
    Igor S.

    I think I understand your problem.
    In my example I have an external data column "Beneficiary Name", using a Beneficiary external content type (accessing a table of beneficiaries in a SQL table).
    I want to set the "Beneficiary Name" property using the client object model. I know the name of the beneficiary but not the ID value.
    It is a fairly simple solution. You just need to identify the name of the property SharePoint assigns to the ID field, in my case it is called "Beneficiary_ID". Then set the two properties as follows:
    thisItem["Beneficiary_Name"] = "Charitable Trust";
    thisItem["Beneficiary_ID"] = -1;
    thisItem.Update();
    Setting the ID property to -1 causes the server to do an automatic lookup for the ID from the value assigned to the item.

  • List field vs. Content Type Field - Powershell

    Hello,
    We have a bunch of document libraries where we have a List Column that contains information.  We have recently added a content type to the libraries and would like to copy the information from the List Column to a field in the content type.
    How do I use powershell to refer to these fields?
    If I have the Document Library (List) as a variable $list then I assume that I refer to the field as $list.$column.  But how do I do the same for the document?   How do I iterate through each document in the library to accomplish the same thing
    with a for loop?
    Something like:
    $list = $web.GetList("My List Name")
    foreach ($document in $list)
    $document.Fields["Document Field"] = $list.Fields["List Field"]
    $document.update()
    Any help would be greatly appreciated,
    Matt

    your code should be similar to
    $web=Get-SPWeb http://your_site_url_here
    $list=$web.Lists["List_Name_here"]
    foreach($item in $list.Items)
    #your logic here
    Hope that helps|Amr Fouad|MCTS,MCPD sharePoint 2010

  • Filling a content type field from an existing list

    I have a question about populating a new document with data from an existing list.
    We have a list with customers containing the name, address and email for every customer.
    I created a document library for letters and invoices with a document type connected to it. The document type uses a site column with a lookup to the customer name.
    In the document template I created the field corresponding to the customer name, so if it is changed in the DIP the customer name shows in the document itself.
    My challenge is that I would like to also fill in the address and email automatically for the customer name that is chosen.
    When I created the site column I was able to select 'add a column for these extra fields' so I am able to put them in the template, but they will not auto fill.
    Is this even possible?

    Hi,
    I got the same result as yours, and I don’t think we can display the additional fields in the DIP (as I don’t support Office, I’m not sure it).
    They are all belong to the lookup column, we could not fill any values.
    Just as we upload a file into the library, it also not display the additional fields.
    What’s more, if it could display the additional fields in the DIP, we also could not add any values to the additional fields.
    But, although the additional fields not display in the DIP, but when we select a value from the lookup column, it would auto fill the value for the additional fields in the library when we save the file.
    Thanks & Regards,
    Jason
    Jason Guo
    TechNet Community Support

  • Upgrade SharePoint Content Types / Site Columns declaratively in SharePoint hosted apps

    Hi!
    I have a question where I was unable to find any official guidance for. I have created a SharePoint hosted APP where I have multiple site columns and content types (declared in XML). The APP installs and functions just fine. 
    Now I have a requirement to change a site column (Choice field, just add some more choice values). I was able to update the site column but the changes will not be reflected on the list column (I think because there no way to push the changes).
    So is there an "official" way of changing site columns?
    Any help is appreciated!
    Thx!

    msdn -
    How to: Update app web components in SharePoint 2013 :
    We do not support changing the data type of a list or content type field (column) after its initial deployment in any
    circumstance. In particular, do not change the data type of a field as part of an app update (not
    even programmatically). As an alternative, you can add a new field. If the app includes custom item create, edit,
    or view forms; be sure to make corresponding changes in these forms. For example, add UI for the new field and remove UI for the old one. (In a provider-hosted app, you can programmatically move data from the old field to the new one and then delete the old.
    How to: Update apps for SharePoint
    [custom.development]

  • I need help using multiple content types in a wiki page library

    I currently trying to used multiple custom content types based off of the wiki page content type in one wiki page library. 
    EDIT: "Allow management of content types" has been enabled with the designer
    The way i would like this library to work is:
    Click the "Files" tab in the ribbon
    Click the "New Document" drop down menu
    Select one of the content types (Client, Contact, Project)
    SharePoint Will Ask for the Page Name and content type fields
    Click create
    Then it will take you to the new created page.
    This issue I'm currently having is when i click on one of the options (Client, Contact, Project) it will ask for a page name then you have to check it in to get to the edit form. The edit form it then loads is the default content type and not the selected
    content type.
    So my questions are.
    Is there any way i can combine the page name and edit steps together into one step?
    How can I get the edit form to pull from the selected content type?
    Thanks in advanced
    James T.F

    Wiki library isn't really designed for customizations... it's a "special" library that overrides a lot of default behavior... similar evidence can be found if you're trying to add metadata columns to wiki pages... they're just not really designed to handle
    it very well.
    Scott Brickey
    MCTS, MCPD, MCITP
    www.sbrickey.com
    Strategic Data Systems - for all your SharePoint needs

  • Tomcat always generates charset in Content-Type http header !!!

    Dear All! I need urgent help with a problem I met with and any suggestion are very appreciated!
    The problem is:
    We are using Tomcat as a web server (and JSP engine), and we would like to use a user control written in C# 2.0 on one of our pages. I've developed such component and tested it using JBuilderX (in fact it resides inside a JSP page). Everything is working fine while debugging using JBuilder, but when I tried to deploy the solution - I got a problem, my dll was not downloaded properly. Let me illustrate this with a test example. I wrote a test html page:
    <html > <head> <title>Test</title> </head> <body> <object id="MyControl" height="300" width="550" classid="http://localhost:8080/MyDLL.dll#WindowsControlLibrary1.UserControl1"> </object> </body> </html>
    When I tried to open it I did not get my control loaded. I check the headers of the server http response and found that Tomcat added the following string as
    Content-Type: application/x-msdownload;charset=ISO-8859-1
    I'm positive that in this case Content-Type should look like
    Content-Type: application/x-msdownload
    (or application/octet-stream if I would choose to define mime type for dll in server's web.xml as
    <mime-mapping> <extension>dll</extension> <mime-type>application/octet-stream</mime-type> </mime-mapping>
    - it does not matter as I understand )
    so Content-Type should not contain any charset for DLL part of response. NO charset=ISO-8859-1 !!
    But Tomcat adds it. :-( And I don't know how to prevent him doing that.
    On the other hand when I tried to open this page using Tomcat started from within JBuilder (port number in this case is different) - then everything was going good and control was downloaded to "Temporary Internet Files" and shown on the page.
    Content-Type field in this case was:
    Content-Type: application/x-msdownload
    without any charset.
    So, my question is: How to suppress Tomcat wish to add the "charset=ISO-8859-1" string to the Content-Type http response header?
    What should I do to load my test html page succesfully?
    Thanks in advance for you time and patience!
    Sincerely yours, Igor

    Back to the top

  • Can't create a Content Type List Item on Visual Studio 2010

    Can't create a Content
    Type List Item on Visual Studio 2010

    Hi,
    According to your description, my understanding is that you want to select the specific content type in new item form.
    This is a default behavior. The "Add new item" link uses the default content type and does not display a dropdown list to change that value. This behavior is hard coded into the control and can't be changed.
    Here are some similar threads for your reference:
    https://social.technet.microsoft.com/Forums/en-US/de60f2a1-df91-4a67-a606-02a593c977b4/choose-a-content-type-when-creating-a-new-list-item?forum=sharepointcustomizationlegacy
    http://sharepoint.stackexchange.com/questions/13281/content-type-field-missing-from-new-form
    Best Regards
    Zhengyu Guo
    TechNet Community Support

  • Content Type choices

    Hi all,
    I have created some content types in Task list. To choose the content type, I need to click on the new item in ribbon and choose the one I need. It is inconvenient. The "new task" button just show the original one only. Could I click on it and
    then choose the content type like the "Edit item" support?
    Thanks,
    Andy Nguyen.

    Hi,
    Based on your description, my understanding is that when you click new task in tasks list, the content type choice field missing from new form.
    There is no OOTB method to make the content type choice field display in the new form.
    When we click the new task button we are by default selecting the default content type so the content type field is not available.  Users will need to click the dropdown in the ribbon and select the correct content type.
    Besides, here are similar posts, you can use as a reference:
    https://social.technet.microsoft.com/Forums/en-US/217eb758-9090-49c9-bb7f-b6f2ca334ce4/content-types-as-choice-field-on-newformaspx?forum=sharepointgeneralprevious
    https://social.technet.microsoft.com/Forums/en-US/c5e4bbe0-91c4-429c-9442-4bd229f5e773/content-type-choice-field-missing-from-new-form-and-missing-in-group-by-for-view?forum=sharepointgeneralprevious
    Best Regards,
    Lisa Chen
    Lisa Chen
    TechNet Community Support

  • Newform swich content types whilst entering data

    Hi
    I have a list with multiple content types.
    When a new item is ceated I want the ability to switch content types whilst entering the data.
    Is this possible with either InfoPath or SPD?
    Thanks

    Hi ,
    When we create a new item, the content type cannot be changed.
    The content type filed will show in EditForm when there two or more content types with the same based parent content type in the same list/library. For example, if you create a custom content type which content type is Item, then when you edit the item you
    create based on the custom Item content type, you will see the content type field.
    For your issue , as a workaround, you can create a new item based on a content type and save it. If you want to change the content type of the created item, you can edit it to change the content type filed.
    I hope this helps.
    Thanks,
    Wendy
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Wendy Li
    TechNet Community Support

  • Required field in custom content type not required in custom list instance

    I have an issue with a custom SharePoint 2013 solution. Among other components, it consists of:
    a feature with several custom fields with different types, some of which are taxonomy fields,
    a feature with three custom content types using different sets of the custom fields with partially different configuration, e.g. whether they are required or not,
    a feature with a custom document library template and instance using two of the custom content types and a default picture library that is programmatically customized when the feature is activated, e.g. it is assigned the third custom content type.
    When deploying the solution and activating the feature, everything is set up correctly except a single taxonomy field for one of the doc lib's content types. It is defined to be required in both content types but indeed it does not show up as required for
    the default content type whereas everything works fine for the other. And it does not matter which of the two custom content types is defined first (= default) in the list template's schema.xml, the issue always occurs for the same taxonomy field in the doc
    lib's default content type. When I use the built-in Document content type as default, the field is required for both custom content types. However, using the Document content type is not an option.
    If you think this behavior is not strange enough: When the list content type's field is set required through the SharePoint UI, it becomes optional again when the list column is updated, e.g. its display name is changed.
    Any ideas?

    Hi,
    Thank you for your question.
    We are currently looking into this issue and will give you an update as soon as possible.
    Thank you for your understanding and support.
    Linda Li                
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Linda Li
    TechNet Community Support

  • Custom content type and choice field

    Hi,
    I try to create costum content type in visual studio, and I want to add choice field to this content type, but I didn't found, how can I programmatically fill choice datasource
    Can anyone help me?
    Thanks,
    Mykie

    I've only done this one but I seem to recall packaging these up as XML as normal columns and sompl referenced them in the Content type definition.  Having had a quick look on the inertubes for a refresher, I think this might point you in the right direction.
    http://www.skylinetechnologies.com/Blog/Article/76/Creating-Lists-in-SharePoint-Using-Visual-Studio-2012.aspx
    Something like this should be your end product.
    Steven Andrews
    SharePoint Business Analyst: LiveNation Entertainment
    Blog: baron72.wordpress.com
    Twitter: Follow @backpackerd00d
    My Wiki Articles:
    CodePlex Corner Series
    Please remember to mark your question as "answered" if this solves (or helps) your problem.

Maybe you are looking for