locked
Adding field into a list content type : RRS feed

  • Question

  • Hi,

    I have requirement to add some fields in a list content type instead of site content type.  can anyone tell me how to add a field in list content type when that field is not present in site or web ?

    I need to do this through powershell. Please provide a solution with respect to powershell development in sharepoint 2010,

    Advance thanks for a quick reply.

    Regards:

    Sanjay Joshi

    Friday, June 3, 2016 3:23 PM

Answers

  • Hi Sanjay,

    Per my knowledge, to add a field to a list content type, then the field needs to be added from existing site column or list column.

    So to add a new field which is not from the existing site columns to list content type, we need to add a new field to the list first and then add the field to the list content type that you need.

    Please refer to the demo below:

    $web = get-spweb "http://webURL/" 
    $list = $web.Lists["ListName"] 
    #add new field to list
    $newColumn = "<Field Type='Text' DisplayName='NewColumn1' Required='TRUE' MaxLength='20'  Name='newColumn1' Description='New column added from powershell'/>"  
    $list.Fields.AddFieldAsXml($newColumn, $true, [Microsoft.SharePoint.SPAddFieldOptions]::AddToNoContentType) 
    #get the newly created field in list
    $field=$list.Fields["NewColumn1"]
    $fieldlink=New-Object Microsoft.SharePoint.SPFieldLink $field 
    #add the new field to content type you need
    foreach($ctype in $list.ContentTypes)
    {
      if($ctype.Name -eq "MyContentType")
      { 
         $ctype.FieldLinks.Add($fieldlink)
         $ctype.Update()
         $list.Update()
      }
    }
    

    Best Regards,

    Victoria 


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Monday, June 6, 2016 6:00 AM

All replies

  • Here is the solution for you

    http://sharepoint.stackexchange.com/questions/57166/add-field-to-list-content-type-using-powershell


    Please remember to click &#39;Mark as Answer&#39; on the answer if it helps you

    Friday, June 3, 2016 3:24 PM
  • hi,

    Thanks for sharing the article. i have already gone through this article once and found that we can add a field in list content type if that field is already available in site or declared as a field within deployed list definition.

    If you see this below line as this is one of the line in shared article 

    $spFieldLink = New-Object Microsoft.SharePoint.SPFieldLink ($rootWeb.Fields[[System.Guid]$field])

    This line refer one of the field from site column and converts it into SPFieldLink so that we can use that fieldLink to add in a list content type.

    But in my case i do not have fields available in site or web level. Can you please let me know whether it is possible or not to add a field based on schema into a list content type through powershell.

    Regards:

    Sanjay

    Friday, June 3, 2016 6:17 PM
  • Hi Sanjay,

    Per my knowledge, to add a field to a list content type, then the field needs to be added from existing site column or list column.

    So to add a new field which is not from the existing site columns to list content type, we need to add a new field to the list first and then add the field to the list content type that you need.

    Please refer to the demo below:

    $web = get-spweb "http://webURL/" 
    $list = $web.Lists["ListName"] 
    #add new field to list
    $newColumn = "<Field Type='Text' DisplayName='NewColumn1' Required='TRUE' MaxLength='20'  Name='newColumn1' Description='New column added from powershell'/>"  
    $list.Fields.AddFieldAsXml($newColumn, $true, [Microsoft.SharePoint.SPAddFieldOptions]::AddToNoContentType) 
    #get the newly created field in list
    $field=$list.Fields["NewColumn1"]
    $fieldlink=New-Object Microsoft.SharePoint.SPFieldLink $field 
    #add the new field to content type you need
    foreach($ctype in $list.ContentTypes)
    {
      if($ctype.Name -eq "MyContentType")
      { 
         $ctype.FieldLinks.Add($fieldlink)
         $ctype.Update()
         $list.Update()
      }
    }
    

    Best Regards,

    Victoria 


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Monday, June 6, 2016 6:00 AM
  • Hi Sanjay,

    How is everything going?

    Is there any update about this issue?

    Best Regards,

    Victoria 


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Monday, June 13, 2016 1:36 AM