0

can anyone please help me out with the PowerShell or any-other solution here.

I have a SharePoint library with 4000 documents and it has a choice column called "test". and each document is set with at least one choice using this column. We want to convert a choice column into a managed metadata filed. I created a column called "test-metadata" and mapped with Managed metadata which has all the term sets similar to choice filed names.

What I was thinking is that using PowerShell can we do something like, based upon choice column set to each file, tag the document with the term set value of the same name.

ex: document A has a choice column(test) with a choice called "Montreal" then a new managed metadata column(test-metadata) should be set with the "Montreal" term set.

1 Answer 1

0

Test Demo for you reference:

Install PnP PowerShell from here.

$SiteURL="https://xxxxx.sharepoint.com/sites/dev"
$ListName= "Doc"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#sharepoint online pnp powershell get list items
$ListItems = Get-PnPListItem -List $ListName -Fields "Choice"

#Loop through each Item
foreach($ListItem in $ListItems)
{ 
if(!($ListItem["Choice"] -eq $null)){
    Write-Host "Title:" $ListItem["Choice"]
      #'testMetaData|test|' is your metadata tree structure, TermGroup|TermSet|Term
      $metadatapath = -join("testMetaData|test|", $ListItem["Choice"]);

      Set-PnPTaxonomyFieldValue -ListItem $ListItem -InternalFieldName 'test_x002d_metadata' -TermPath $metadatapath
      }
}

Here is the thread for you reference:

https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/set-pnptaxonomyfieldvalue?view=sharepoint-ps

Update:

Try below script:

$SiteURL="https://xxx.sharepoint.com/sites/dev"
$ListName= "Doc"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#sharepoint online pnp powershell get list items
$ListItems = Get-PnPListItem -List $ListName -Fields "Choice"
$Array = New-Object System.Collections.ArrayList
#Loop through each Item
foreach($ListItem in $ListItems)
{ 
$results = @{}
if(!($ListItem["Choice"] -eq $null)){
    #Write-Host "Title:" $ListItem["Choice"]
      $option = [System.StringSplitOptions]::RemoveEmptyEntries

      $($ListItem["Choice"].split(' ',$option)).foreach{
      $metadatas = '';
      $value = ""+$_+"";
      $term= Get-PnPTerm -Identity $value -TermSet "test" -TermGroup "testMetaData";
      $Id = $term.Id;
      #$metadatas ='"' + $Id +'"'+ "=" +'"'+ $value + '"' ;
      #Write-Host $metadatas 
      $results.Add( $Id.ToString(),$value);

      }


      Set-PnPTaxonomyFieldValue -ListItem $ListItem  -InternalFieldName 'test_x002d_metadata'  -Terms  $results
      }
}
2
  • thanks for above powershell. I am bit confused at the term set step. We hacve 10 terms under a group called "Axis". So i need to just mention as "Axis" or do I need to mention all the 10 terms in the script so that it cn map. Because based up on choice column text it should pickup term from that group
    – raj
    Nov 20, 2019 at 21:28
  • Check my update.
    – Lee
    Nov 22, 2019 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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