Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Get/Set Managed Metadata Column Values using PowerShell

$
0
0
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!"

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>