none
Unable to add new column to all content types in Asset Library via PowerShell RRS feed

  • Question

  • I have a site where I need to add a column to several lists within my site. Due to the nature of how I need the new column to work, it cannot be a Site Column.  I'm creating my custom list to be used as the lookup for the new field.  My script works but fails to add the new column to all of the content types within the Asset Library.    Any ideas?

    Here's the code:

    Start-SPAssignment -Global
    $spweb = Get-SPWeb $NewSiteURL
    $listTemplate = $spweb.ListTemplates["Custom List"]
    $spweb.Lists.Add($listUrl, "", $listTemplate)
    $list = $spweb.Lists[$listUrl]
    $list.Title = $listTitle
    $list.Update()
    
    #Create Standard Lists & Libraries
    $lists = @(
    ("Discussions","Discussion Board"),
    ("Events","Calendar"),
    ("Videos","Asset Library")
    )
    
    foreach ($list in $lists){
        $listUrl = $list[0]
        $listTemplate = $spweb.ListTemplates[$list[1]]
        $spweb.Lists.Add($list[0], "", $listTemplate)
        $list = $spweb.Lists[$listUrl]
        $list.Title = $list[0]
        $list.OnQuickLaunch =$true
        $list.Update()
        write-host ($listUrl + " Created")
    }
    
    #Add Categories column to new lists
    $lists = "Discussions", "Videos", "Documents"
    $lookupList = $spweb.Lists.Item("Site Categories")
    
    
    foreach ($list in $lists){
        $childList = $spweb.Lists[$list]
        $childList.Fields.Add("Categories", "Text", $false)
        $childList.Fields.AddLookup("Categories", $lookupList.ID, $false)
        $spChildListLookupField = $childList.Fields["Categories"]
        $spChildListLookupField.AllowMultipleValues = $true
        $spChildListLookupField.LookupField = $lookupList.Fields["Title"]
        $spChildListLookupField.Update()
    }
    $spweb.Dispose()
    Stop-SPAssignment -Global

    • Moved by Bill_Stewart Thursday, March 16, 2017 2:06 PM Move to more appropriate forum
    Thursday, March 16, 2017 6:39 AM

Answers

  • Hi Tuff,

    This is the script I use to add a Lookup column into the Asset library:

    $SPWeb = Get-SPWeb "http://sp"
    $lookupList = $SPweb.Lists["list1"]
    $childList = $SPweb.Lists["asset"]
    
    $childList.Fields.AddLookup("LKTest1",$lookupList.id, $true)
    $spChildListLookupField = $childList.Fields["LKTest1"]
    $spChildListLookupField.AllowMultipleValues = $true
    $spChildListLookupField.LookupField = $lookupList.Fields["Title"]
    $spChildListLookupField.Update()

    By default, the new column will only be added to the default content type(in my case, it is “Audio”) of the Asset library:

    Then I execute the script below to add this column into another content type:

    #add this field to other content type
    $field = $childList.Fields["LKTest1"]
    $listCT = $childList.ContentTypes["Video"];
    
    $link = new-object Microsoft.SharePoint.SPFieldLink $field
    $listCT.FieldLinks.Add($link)
    $listCT.Update()
    $childList.Update()

    The outcome:

    Best regards,

    Patrick

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

    • Marked as answer by Tuff Moyrong Sunday, March 19, 2017 2:58 PM
    Saturday, March 18, 2017 8:54 AM
    Moderator
  • Hi Patrick,

    I actually figured this out later that day while going through the assembly documentation from Microsoft.  Here's the code that I ended up with.

    #Add Categories column to new lists
    $lists = "Documents", "Discussions", "Videos"
    $lookupList = $spweb.Lists.Item("Site Categories")
    
    
    foreach ($list in $lists){
        $childList = $spweb.Lists[$list]
        $childList.Fields.AddLookup("Categories", $lookupList.ID, $false)
        $lookupField = $childList.Fields["Categories"]
        $lookupField.AllowMultipleValues = $true
        $lookupField.Update()
    }
    
    #Add FieldLinks to all content types for Video repository
    $link = new-object Microsoft.SharePoint.SPFieldLink $lookupField
    $childList = $spweb.Lists["Videos"]
    $cts = $childList.ContentTypes
    foreach ($ct in $cts){
        if ($ct.Name -ne "Folder"){
            $ct.FieldLinks.Add($link)
            $ct.Update($false)
        }
    }
    $spweb.Dispose()
    Stop-SPAssignment -Global

    • Marked as answer by Tuff Moyrong Sunday, March 19, 2017 2:58 PM
    Sunday, March 19, 2017 2:57 PM

All replies

  • Hi Tuff,

    This is the script I use to add a Lookup column into the Asset library:

    $SPWeb = Get-SPWeb "http://sp"
    $lookupList = $SPweb.Lists["list1"]
    $childList = $SPweb.Lists["asset"]
    
    $childList.Fields.AddLookup("LKTest1",$lookupList.id, $true)
    $spChildListLookupField = $childList.Fields["LKTest1"]
    $spChildListLookupField.AllowMultipleValues = $true
    $spChildListLookupField.LookupField = $lookupList.Fields["Title"]
    $spChildListLookupField.Update()

    By default, the new column will only be added to the default content type(in my case, it is “Audio”) of the Asset library:

    Then I execute the script below to add this column into another content type:

    #add this field to other content type
    $field = $childList.Fields["LKTest1"]
    $listCT = $childList.ContentTypes["Video"];
    
    $link = new-object Microsoft.SharePoint.SPFieldLink $field
    $listCT.FieldLinks.Add($link)
    $listCT.Update()
    $childList.Update()

    The outcome:

    Best regards,

    Patrick

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

    • Marked as answer by Tuff Moyrong Sunday, March 19, 2017 2:58 PM
    Saturday, March 18, 2017 8:54 AM
    Moderator
  • Hi Patrick,

    I actually figured this out later that day while going through the assembly documentation from Microsoft.  Here's the code that I ended up with.

    #Add Categories column to new lists
    $lists = "Documents", "Discussions", "Videos"
    $lookupList = $spweb.Lists.Item("Site Categories")
    
    
    foreach ($list in $lists){
        $childList = $spweb.Lists[$list]
        $childList.Fields.AddLookup("Categories", $lookupList.ID, $false)
        $lookupField = $childList.Fields["Categories"]
        $lookupField.AllowMultipleValues = $true
        $lookupField.Update()
    }
    
    #Add FieldLinks to all content types for Video repository
    $link = new-object Microsoft.SharePoint.SPFieldLink $lookupField
    $childList = $spweb.Lists["Videos"]
    $cts = $childList.ContentTypes
    foreach ($ct in $cts){
        if ($ct.Name -ne "Folder"){
            $ct.FieldLinks.Add($link)
            $ct.Update($false)
        }
    }
    $spweb.Dispose()
    Stop-SPAssignment -Global

    • Marked as answer by Tuff Moyrong Sunday, March 19, 2017 2:58 PM
    Sunday, March 19, 2017 2:57 PM