none
PowerShell Won't Update Value in Managed Metadata Field in Document Library RRS feed

  • Question

  • Hello all,

    When using this script, I am able to tag items in a list, but it won't change those in a document library. Please help:

    $web = Get-SPWeb http://mycompanysite.com/test
    $list = $web.lists["Test"]
    $CSVcnt= 0
    $items = $list.getItems()
    foreach($i in Import-CSV C:\PowerShell\EditListItemValues.csv)
    {
    $listCnt = 0;
    Foreach($item in $items){
    If($item["Name"] -eq $i.AnchorAttribute) {
    $item["test1"] = $i.Attribute1
    $item["test2"] = $i.Attribute2
    $item["test3"] = $i.Attribute3
    $item["test4"] = $i.Attribute4
    $item.update()
    }
    }
    }


    Personal Blog: http://thebitsthatbyte.com


    • Edited by Kelly Rusk Tuesday, March 19, 2013 3:02 PM
    Monday, March 11, 2013 8:47 PM

Answers

  • Ok, got it. You need to get an empty field value for the taxonomy field, then pass that to the SetFieldValue method.

    E.g.

    $web = get-spweb http://my
    $list = $web.Lists["documents"] -as [Microsoft.SharePoint.SPDocumentLibrary]
    $url = [String]::Format("{0}{1}",$web.Url, $item.File.Url)
    $file = $web.GetFile($url)
    $file.CheckOut()
    $fi = $file.Item
    $tf = $fi.Fields["MetadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
    $fv = $tf.GetFieldValue("");
    $tf.SetFieldValue($fi,$fv)
    $fi.Update()
    $file.CheckIn("")


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    • Marked as answer by Kelly Rusk Tuesday, March 19, 2013 8:50 PM
    Tuesday, March 19, 2013 5:51 PM
  • I've update the example a little, as well as blogging about it here: http://matthewyarlett.blogspot.co.uk/2013/03/clear-delete-value-of-taxonomy-field.html

    #Lists
    $web = get-spweb http://my
    $list = $web.Lists["my list"]
    $item = $list.items[1] #Get an item that currently has a value set in the Taxonomy (Managed Metadata) field
    $taxField = $item.Fields["metadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField] 
    $taxFieldValue = $taxField.GetFieldValue("");
    $taxField.SetFieldValue($item,$taxFieldValue)
    $item.Update()
    
    
    #Document Libraries
    $web = get-spweb http://my
    $list = $web.Lists["document library"] -as [Microsoft.SharePoint.SPDocumentLibrary]
    $item = $list.items[1] #Get an item that currently has a value set in the Taxonomy (Managed Metadata) field
    $url = [String]::Format("{0}{1}",$web.Url, $item.File.Url)
    $file = $web.GetFile($url)
    if($file.CheckOutStatus -eq "None")
    {
    $file.CheckOut()
    $fileItem = $file.Item
    $taxField = $fileItem.Fields["metadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
    $taxFieldValue = $taxField.GetFieldValue("");
    $taxField.SetFieldValue($fileItem,$taxFieldValue)
    $fileItem.Update()
    $file.CheckIn("Updated Taxonomy Field Value")
    }
    else
    {
    $msg = [String]::Format("This file, {0}, is checked out and cannot be edited at the moment",$file.Name)
    Write-Host $msg -ForegroundColor DarkYellow
    }


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    • Marked as answer by Kelly Rusk Wednesday, March 20, 2013 4:22 PM
    Wednesday, March 20, 2013 10:56 AM

All replies

  • Side note: field "test4" is a managed metadata field. In the corresponding spreadsheet I use the id and term and it brings over the value without issue to an empty field. (ex. 91;#Department 2)


    Personal Blog: http://thebitsthatbyte.com

    Monday, March 11, 2013 8:56 PM
  • Hi Kelly,

    Have a try of following code to use SetFieldValue method to update the Managed metadata field value

    $web = Get-SPWeb http://portal.sharepoint.com

    $list = $web.Lists["ListName"]

    $listItems = $list.GetItems()

     foreach($item in $listItems)

     {

            #Cast to SPListItem to avoid ambiguous overload error

            $spItem = [Microsoft.SharePoint.SPListItem]$item;

           

            #Get the taxonomy field for the list item

            $taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields["Project"]

           

            #Set the field's value using the term

            $taxField.SetFieldValue($spItem,$term)

            $spItem.Update()

    }

    For more information, see

    http://blogs.msdn.com/b/kaevans/archive/2012/02/15/updating-sharepoint-managed-metadata-columns-with-powershell.aspx

    Best Regards.


    Kelly Chen
    TechNet Community Support

    Tuesday, March 12, 2013 8:32 AM
    Moderator
  • Thanks Kelly,

    I had seen that link as well, but when I run the script, I get the following:

    Multiple ambiguous overloads found for "SetFieldValue" and the argument count:
    "2".

    I have 2 lines of data.

    Regards,

    Kelly


    Personal Blog: http://thebitsthatbyte.com

    Thursday, March 14, 2013 10:19 PM
  • Hi Kelly,

    Based on this kind error, I assume that your managed metadata column is allowed to use multiple values. The method above is used to update the column that has one single value. When the column has multiple value, then it will throw that error.

    Please check the following article to update the managed metadata column with multiple values:

    http://www.3guysonsharepoint.com/?p=1052

    Best Regards.


    Kelly Chen
    TechNet Community Support

    Friday, March 15, 2013 1:42 AM
    Moderator
  • Thanks Kelly,

    I had found that one while searching as well, but since I am constrained to using PowerShell, I can't leverage C#. I wonder if it may be better to null the values, then add new ones instead.

    Regards,

    Kelly


    Personal Blog: http://thebitsthatbyte.com

    Friday, March 15, 2013 1:42 PM
  • Hi Kelly,

    I doubled checked and the fields are not using multiple values.

    Kelly


    Personal Blog: http://thebitsthatbyte.com

    Friday, March 15, 2013 9:04 PM
  • My revised script will process on the item, since it shows the modified field changing, but it doesn't update the item. What am I missing here? Managed Metadata can be so frustrating...

    $site = Get-SPSite http://intranet.yourcompany.com  
    $web = Get-SPWeb http://intranet.yourcompany.com/operations
    $list = $web.lists["Test"]
     
    $session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)   
    $termStore = $session.TermStores["Managed Metadata Service"]   
    $group = $termStore.Groups["Intranet.YourCompany.com"]   
    $termSet = $group.TermSets["Organizational Structure"]   
    $terms = $termSet.GetTerms(100)
    
    $locations = "Department 1"
    foreach($location in $locations) {  
    $term = $terms | ?{$_.Name -eq $location}  
    $termString += $term.Name + "|" + $term.Id + ";"  
    If($item["Name"] -eq "My Document.docx") { 
    $item["Department"] = $termString.Substring(0,$termString.Length-1)
    Write-host "Managed Metadata Set or Updated"
    $item.Update()
    $web.dispose()
    }
    }


    Personal Blog: http://thebitsthatbyte.com

    Monday, March 18, 2013 9:42 PM
  • As I dive deeper, my scripts are working on Lists, but not Document Libraries. How do I need to adjust my PowerShell to work on Document Libraries? Do the documents need to be checked out first?

    Personal Blog: http://thebitsthatbyte.com

    Tuesday, March 19, 2013 1:49 PM
  • Additionally, do Document Libraries require the SPFieldValue where lists do not?

    Personal Blog: http://thebitsthatbyte.com

    Tuesday, March 19, 2013 4:29 PM
  • Ok, got it. You need to get an empty field value for the taxonomy field, then pass that to the SetFieldValue method.

    E.g.

    $web = get-spweb http://my
    $list = $web.Lists["documents"] -as [Microsoft.SharePoint.SPDocumentLibrary]
    $url = [String]::Format("{0}{1}",$web.Url, $item.File.Url)
    $file = $web.GetFile($url)
    $file.CheckOut()
    $fi = $file.Item
    $tf = $fi.Fields["MetadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
    $fv = $tf.GetFieldValue("");
    $tf.SetFieldValue($fi,$fv)
    $fi.Update()
    $file.CheckIn("")


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    • Marked as answer by Kelly Rusk Tuesday, March 19, 2013 8:50 PM
    Tuesday, March 19, 2013 5:51 PM
  • Matthew, you have been so helpful! Thank you so much. I updated what you provided, as I needed to call to the item by the following: $item = $list.items[0] where 0 equals the list item as you mentioned here:

    http://social.msdn.microsoft.com/Forums/en-US/sharepointgeneralprevious/thread/d05f8d8e-7a28-4909-b8b4-9c8fc8e0d501

    It can also be swapped out with a foreach statement if required. Here is your PowerShell with the addition to prevent null values.

    $web = get-spweb http://my
    $list = $web.Lists["documents"] -as [Microsoft.SharePoint.SPDocumentLibrary]
    $item = $list.items[0]
    $url = [String]::Format("{0}{1}",$web.Url, $item.File.Url)
    $file = $web.GetFile($url)
    $file.CheckOut()
    $fi = $file.Item
    $tf = $fi.Fields["MetadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
    $fv = $tf.GetFieldValue("");
    $tf.SetFieldValue($fi,$fv)
    $fi.Update()
    $file.CheckIn("")

     

    Personal Blog: http://thebitsthatbyte.com

    Tuesday, March 19, 2013 8:50 PM
  • I've update the example a little, as well as blogging about it here: http://matthewyarlett.blogspot.co.uk/2013/03/clear-delete-value-of-taxonomy-field.html

    #Lists
    $web = get-spweb http://my
    $list = $web.Lists["my list"]
    $item = $list.items[1] #Get an item that currently has a value set in the Taxonomy (Managed Metadata) field
    $taxField = $item.Fields["metadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField] 
    $taxFieldValue = $taxField.GetFieldValue("");
    $taxField.SetFieldValue($item,$taxFieldValue)
    $item.Update()
    
    
    #Document Libraries
    $web = get-spweb http://my
    $list = $web.Lists["document library"] -as [Microsoft.SharePoint.SPDocumentLibrary]
    $item = $list.items[1] #Get an item that currently has a value set in the Taxonomy (Managed Metadata) field
    $url = [String]::Format("{0}{1}",$web.Url, $item.File.Url)
    $file = $web.GetFile($url)
    if($file.CheckOutStatus -eq "None")
    {
    $file.CheckOut()
    $fileItem = $file.Item
    $taxField = $fileItem.Fields["metadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
    $taxFieldValue = $taxField.GetFieldValue("");
    $taxField.SetFieldValue($fileItem,$taxFieldValue)
    $fileItem.Update()
    $file.CheckIn("Updated Taxonomy Field Value")
    }
    else
    {
    $msg = [String]::Format("This file, {0}, is checked out and cannot be edited at the moment",$file.Name)
    Write-Host $msg -ForegroundColor DarkYellow
    }


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    • Marked as answer by Kelly Rusk Wednesday, March 20, 2013 4:22 PM
    Wednesday, March 20, 2013 10:56 AM
  • Hi All,

    I encountered this thread when I was looking to update a managed metadata field to a specific value in the termstore. Therefore the answer provided ( i.e. set column value to blank ) was not a solution for me.

    the good news is that Matthew has already documented the solution for this on his blog.

    http://matthewyarlett.blogspot.co.uk/2012/08/bulk-updating-taxonomy-field-value-on.html

    Basically the 2nd paramater in the SetFieldValue method must be a SharePoint.Taxonomy.Term object and not just a string.

    You must therefore find the term that you want to update the column with in the termstore first ( which can be a little bit tricky if your terms are nested ) and then pass this term to SetFieldValue.

    Thanks Matthew

    Best regards,

    Counie


    SharePoint Farmer in training

    Monday, August 12, 2013 11:36 AM
  • Hi Matthew,

    Can you please share your code as i am finding bit tricky to find the 2nd part you've menitoned 

    "Basically the 2nd paramater in the SetFieldValue method must be a SharePoint.Taxonomy.Term object and not just a string.

    You must therefore find the term that you want to update the column with in the termstore first ( which can be a little bit tricky if your terms are nested ) and then pass this term to SetFieldValue."

    I want to update the metadata field with a different term in the termset and i am reading the value to be updated from a csv file  which is in this format. (FieldName,FieldType,WhereField,WhereValue,UpdateValue)

    FieldName,FieldType,WhereField,WhereValue,UpdateValue
    V1,Managed Metadata,V2,V3


    Friday, May 11, 2018 12:38 PM