2

I want to add one more value from the termset in the existing column value using Powershell script.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy")
$site = Get-SPSite http://invi28apps9c
$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores["Managed Metadata Service"]
$group = $termStore.Groups["KB"]
$termSet = $group.TermSets["IPS Document type"]     
$terms = $termSet.GetTerms(100)
$term = $terms | ?{$_.Name -eq "Docset"}
$web = Get-SPWeb http://invi28apps9c/DemoTeamSite
$list = $web.Lists["KBDocument"]
$query = New-Object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = "Scope='Recursive'";
$query.RowLimit = 2000
$caml = "<Where> 
        <Eq>
         <FieldRef Name='DocIcon'/>
         <Value Type='Computed'>.zip</Value>
        </Eq>
        </Where>"
$query.Query = $caml 

do
{
    $listItems = $list.GetItems($query)
    $spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
    foreach($item in $listItems)
    {
        $spItem = [Microsoft.SharePoint.SPListItem]$item;
        $taxField =     [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields["Document Type"]
        $taxField.SetFieldValue($spItem,$term)
        $spItem.Update()
    }
}
while ($spQuery.ListItemCollectionPosition -ne $null)
1

In order to use Taxonomy terms to a taxonomy field using PowerShell, we need to involve SharePoint Object Model from PowerShell, and then use the Object Model to add terms in a termstore to the field.

Below is the same code for your reference. Please add the logic in the For-Each method:

Site Collection URL - Give your site collection url in quotation marks

 $TaxonomySiteUrl = "http://server"

Access the termstore

$site = Get-SPSite $TaxonomySiteUrl
$web = $site.OpenWeb()

$list = $web.Lists["listName"]

$newItem = $list.Items.Add()

$targetField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem.Fields["Name of Taxonomy Field"]

$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
 $termstore = $session.TermStores[$targetField.SspId]
 $termSet = $termstore.GetTermSet($targetField.TermSetId)

$taxCollection = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection($targetField)

$termSet.GetAllTerms() | ForEach-Object {
 $taxonomyFieldValue = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue($targetField)
 $taxonomyFieldValue.TermGuid = $_.Id
 $taxonomyFieldValue.Label = $_.Name
 $taxCollection.Add($taxonomyFieldValue)
 }

$targetField.SetFieldValue($newItem, $taxCollection)

$newItem["Title"] = "Item1" 
$newItem.Update()

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.