Is it possible to change a value on all libraries within a site. I have multiple libraries where the term name in the store has been changed. However in libraries I have documents tagged with the old term. The view the library is looking for this old term. Can I use powershell to change the filter value on mass for all libraries. Any examples?
1 Answer
There are a few things you need to do here.
Get a ref to the Term store and terms:
$site = Get-SPSite http://yoursite
$web = $site.OpenWeb("yourweb")
$session = Get-SPTaxonomySession -Site $site
$list = $web.lists["Documents"]
$field = $list.Fields["TargetField"]
$termStoreId = $field.SspId
$termsetId = $field.TermSetId.ToString()
$termstore = $session.TermStores[$termStoreId]
$termSet = $termstore.GetTermSet($termsetId)
$terms = $termset.GetAllTerms()
Once you have that you need to find the items to replace. You need to get the WSSId of the original term:
$term = $terms | where {$_.Name -eq 'YourOldTerm'}
$WSSId = [Microsoft.SharePoint.Taxonomy.TaxonomyField]::GetWssIdsOfTerm($site, $termstoreid, $termsetId,$term.Id, $false, 1)
Now that you have that you can issue a query to find your items:
$caml = "<Where>
<Eq>
<FieldRef LookupId='TRUE' Name='YourFieldName' />
<Value Type='Integer'>$WSSId</Value>
</Eq>
</Where>"
$query = New-Object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = "Scope='RecursiveAll'"
$query.Query = $caml
$items = $list.GetItems($query)
Loop through the $items get the taxonomy field for each $item:
$fld = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields["YourFieldName"]
Get your new value from the original $terms:
$newValue = $terms | where {$_.Name -eq 'New Value'}
And finally set it:
$fld.SetFieldValue($folder.Item, $newValue)
$item.Update()
Ideally you should wrap all this in a function. If you don't understand anything let me know and I can do just that when I get some spare time.