I am trying to update a taxonomy field of a file in my document library.
The taxonomy group is "My Group"
The term that will be used for updating is "123 Test"
When I run the below script, I get an error: Cannot index into a null array. How can I update the taxonomy field of my document?
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy")
$site = Get-SPSite http://sharePointserver
$FieldName="MyField"
$site = Get-SPSite http://sharePointserver
$FieldName="MyField"
"MyField" is the internal name of the taxonomy field. Why am I getting
You cannot call a method on a null values expression?
$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores["Connection to: Managed Metadata Service"]
$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores[0]
$group = $termStore.Groups["My Term Sets"]
$termSet = $group.TermSets["Company"]
$terms = $termSet.GetAllTerms()
$term = $terms["123 Test]
$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores[0]
$group = $termStore.Groups["My Term Sets"]
$termSet = $group.TermSets["Company"]
$terms = $termSet.GetAllTerms()
$term = $terms["123 Test]
$webUrl = "http://sharePointserver/sites/85bcdccd9204"
$library = "My Document Library"
$s = new-object Microsoft.SharePoint.SPSite($webUrl)
$w = $s.OpenWeb()
$l = $w.Lists[$library]
Write-Host "Connecting To Site: " $webUrl
foreach ($listItem in $l.Items)
{
$spItem = [Microsoft.SharePoint.SPListItem]$listItem;
$currentItemID=$listItem.ID
Write-Host " Current Item ID: " $currentItemID -ForegroundColor DarkYellow
#Get the taxonomy field for the list item
if ($currentItemID -eq 130)
{
#Set MMS column Value
$MMSField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields[$FieldName]
foreach ($listItem in $l.Items)
{
$spItem = [Microsoft.SharePoint.SPListItem]$listItem;
$currentItemID=$listItem.ID
Write-Host " Current Item ID: " $currentItemID -ForegroundColor DarkYellow
#Get the taxonomy field for the list item
if ($currentItemID -eq 130)
{
#Set MMS column Value
$MMSField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields[$FieldName]
Write-host "Managed Metadata Field value updated!"
$MMSField.setFieldValue($spItem,"123 Test")
$listItem.Update();
}
if ($term.Name -eq "123 Test")
{
Write-Host "123 Test Found!"
}
}
faye fouladi