Monday, November 30, 2015

Update a Managed Metadata List Field the Easy Way with PowerShell

Posted in SharePoint, Technical By Kyle Wright @ 2/7/2013

I was working with PowerShell to update a managed metadata field that accepted multiple values in a publishing page library today and it was more difficult than it seemed like it should have been, so I’m posting here what actually worked for me. It turned out to be much simpler than I was trying to make it.  The larger project here was reading values in for publishing page properties from an XML file, but I have skipped all that for the purpose of this post. 

The first thing you need to do is connect to your site and your term store and term set.

   1:  $site = Get-SPSite
   2:  $session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
   3:  $termStore = $session.TermStores["Managed Metadata Service"]
   4:  $group = $termStore.Groups["Corporate"]
   5:  $termSet = $group.TermSets["Locations"]
   6:  $terms = $termSet.GetTerms()

These lines are loading all the terms in the term set named “Locations”.  Next, we need to specify what values we are trying to set.  In this example, we are manually setting them in the script, but these could easily be read in from XML, CSV or something else.

$locations = "Charlotte", "Chicago", "New York", "Boston"

So we have our terms set in an array, now we can loop through each element of the array, find the corresponding term in the term set (stored in $terms from line 6 above) and create a “Term | Term ID” string to insert into our managed metadata field.  Individual terms are separated by a semi-colon in this case since our field allows multiple values.

   1:  foreach($location in $locations) {
   2:       $term = $terms | ?{$_.Name -eq $location}
   3:       $termString += $term.Name + "|" + $term.Id + ";"
   4:  }    

Now we just need to apply this string to our managed metadata field.  It will translate it so that only the term names are shown.  It stores the term ID in a separate hidden field.

   1:  $newPage.ListItem["Location"] = $termString.Substring(0,$termString.Length-1) #fails with a trailing ; so, remove it

The managed metadata field will reject your string if it contains a trailing semi-colon, so when we set the value to our field, just strip off the last character.  This will need to be removed no matter how many terms there are.

So, lets put that all together with all the additional code we need to connect and work with the publishing page library:

   1:  $site = Get-SPSite   
   2:  $web = $site.rootweb
   3:  $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
   4:  $pages = $pubWeb.GetPublishingPages($pubWeb)
   6:  $newPageName = "Test-to-Set-Locations"
   7:  $pageLayout = $pubWeb.GetAvailablePageLayouts() | Where-Object {$_.Title -eq "Article Page"}
   8:  $pageurl = "Pages/" + $newPageName + ".aspx"
   9:  $newpage = $pages.Add($pageurl, $pagelayout)
  11:  $session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)   
  12:  $termStore = $session.TermStores["Managed Metadata Service"]   
  13:  $group = $termStore.Groups["Corporate"]   
  14:  $termSet = $group.TermSets["Locations"]   
  15:  $terms = $termSet.GetTerms()   
  17:  $locations = "Charlotte", "Chicago", "New York", "Boston"   
  18:  foreach($location in $locations) {  
  19:       $term = $terms | ?{$_.Name -eq $location}  
  20:       $termString += $term.Name + "|" + $term.Id + ";"  
  21:  }      
  22:  $newPage.ListItem["Location"] = $termString.Substring(0,$termString.Length-1)   
  23:  Write-host "Managed Metadata Set"
  24:  $newPage.Update()

, ,


Got something to say? Join the discussion »
    Posted by dchess on 5/15/2014
    Thanks for posting this. I've been trying to do something similar with a document library, but when I run a similar script I keep getting this error: Unable to index into an object of type Microsoft.SharePoint.SPListItem.

    Any ideas?

leave a reply

 [Quick Submit with Ctrl+Enter]

Remember my details
Notify me of followup comments via e-mail


About C5 Insight

We are a Microsoft Gold Certified partner focusing on SharePoint, Microsoft Dynamics CRM and  Learn more about us by visiting our website.

We Wrote the Bible on Microsoft SharePoint and Dynamics CRMBook-Microsoft Dynamics CRM 2011 and SharePoint 2010 Bible

Upcoming Events

Why Upgrade from SharePoint 2010?

12/08/15 @ 2:00 PM ET If your organization is considering a move to SharePoint 2013, then this webinar is for you. We know......

Comparing Salesforce and Dynamics CRM for Sales

12/15/15 @ 1:00 PM ET "Our CRM is so good that our sales team members WANT to use it," said no sales manager ever. Are yo... ...

Microsoft Social Engagement: Improving your Marketing LUCK

12/17/15 @ 1:00 PM ET Ready to get social? In this deminar (that's demo + webinar) session, we will take a look at the Mi... ...

Top Downloads

SharePoint 2013 Feedback Solution - collect feedback from users into a custom feedback list that captures their feedback and exactly where they were in SharePoint when they provided it.

Why CRM Fails - research into CRM failure, best practices, and how to recover from a failing project.

The 60 Second Collaboration Readiness Self-Evaluation - is your team really ready to collaborate?  If they're not, software isn't going to help.  Take the quick self-evaluation in each department to understand how well your culture collaborates.

Identify Your Support Gaps - collaboration and CRM projects often fall short when it comes to supporting users, because support for these projects is different from other projects.  



The information herein may be used solely at your own risk.  No warranty is made by the author or by C5 Insight, Inc.

The opinions expressed herein are those of the individual authors and do not necessarily represent C5 Insight, Inc in any way.

Copyright 2011-2013 by C5 Insight