Monday, December 14, 2015

Get/Set Managed Metadata Column Values using PowerShell

Here is my collection of PowerShell scripts to read and write Managed Metadata field values in SharePoint:

Read Managed Metadata column Value:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$WebURL="https://portal.crescent.com/"
$ListName="Deals"
$FieldName="Region"
$ItemID=1

#Get Objects 
$Web = Get-SPWeb $WebURL     
$List= $Web.Lists[$listName]
$Item = $List.GetItembyID($ItemID)

#Get MMS column Value
[Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue]$MMSFieldValue = $item[$FieldName] 

write-host $MMSFieldValue.Label

Get Multiple Value MMS field values using PowerShell:
When "Allow Multiple Values" Selected, here is how we can retrieve the value of it:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$WebURL="https://portal.crescent.com/"
$ListName="Deals"
$FieldName="Region"
$ItemID=2

#Get Objects 
$Web = Get-SPWeb $WebURL     
$List= $Web.Lists[$listName]
$Item = $List.GetItembyID($ItemID)

#Get MMS column Value
[Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $item[$FieldName] 

#Concatenate each term in the value collection
$MMSFieldTerms=""
Foreach ($MMSFieldValue in $MMSFieldValueColl)
{
    if($MMSFieldValue.label -ne $null)
    {
        $MMSFieldTerms+=$MMSFieldValue.label+"; "
    }
}

write-host $MMSFieldTerms                   

Set Managed Metadata field Value:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#Variables
$WebURL="https://portal.crescent.com"
$ListName="Deals"
$FieldName="Region"
$ItemID=1

#Get the web
$Web = Get-SPWeb $WebURL     

#Get the Term from Term store
$TaxonomySession = Get-SPTaxonomySession -Site $web.Site
$TermStore = $TaxonomySession.TermStores["Managed Metadata Service"]
$TermGroup = $TermStore.Groups["Knowledge Portal"]
$TermSet = $TermGroup.TermSets["Regions"]
$Term = $Termset.Terms["America"]

#You can also get the term directly as: $Term = "15;#America|1c58d657-9bd1-4bff-b1b0-74e52eb717dd"
#Use SharePoint Manager

#Get the List and List Item
$List= $Web.Lists[$listName]
$Item = $List.GetItembyID($ItemID)

#Set MMS column Value
$MMSField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$Item.Fields[$FieldName]
$MMSField.setFieldValue($Item,$Term)
$Item.Update()

Write-host "Managed Metadata Field value updated!"

Update Managed Metadata Column value for Multiple Values
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#Variables
$WebURL="https://portal.crescent.com"
$ListName="Deals"
$FieldName="Region"
$ItemID=2

#Get the web
$Web = Get-SPWeb $WebURL     

#Get the Term store, Group and Term Set
$TaxonomySession = Get-SPTaxonomySession -Site $web.Site
$TermStore = $TaxonomySession.TermStores["Managed Metadata Service"]
$TermGroup = $TermStore.Groups["Knowledge Portal"]
$TermSet = $TermGroup.TermSets["Regions"]

#Get the List, List Item and Field
$List= $Web.Lists[$listName]
$Item = $List.GetItembyID($ItemID)
$MMSField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$Item.Fields[$FieldName]

#Actual Term Values to update
$TermValuesColl = @("Africa","Asia","Europe")

#Create a Term field value collection
$MMSValueCollection = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection($MMSField)
#Form each Term
foreach($TermValue in $TermValuesColl)
{
        $Term = $Termset.Terms[$TermValue]
        $MMSFieldValue = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue($MMSField)
        $MMSFieldValue.TermGuid = $Term.Id
        $MMSFieldValue.Label = $Term.Name
        $MMSValueCollection.Add($MMSFieldValue)
 }

#Set Multi-value MMS column Value
$MMSField.setFieldValue($Item,$MMSValueCollection)
$Item.Update()

Write-host "Managed Metadata Field value updated!"



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...