0

There seem to be many examples of this, but none in PnP PowerShell. I'm trying to access all of the values from a column that is using the Term Store. I'm on SharePoint 2013

Here is my code this far:

 $libs = Get-PnPList -Web $web | Where{($_.BaseType -eq “DocumentLibrary”) }

          foreach($lib in $libs){
             $libitems = (Get-PnPListItem -Web $web -List $lib -Fields "FileLeafRef","Name","Title","Author","Modified","Created","KBAbstract","KBContentAuthor","KBCategory","Publish","KBPublishDate").FieldValues

               foreach($libitem in $libitems)
                 {
                     if($libitem.FSObjType -eq "0"){
                      $data = @{
                                  ...                          
                                   [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem["KBCategory"] 
                                    #Concatenate each term in the value collection
                                    $MMSFieldTerms = ""
                                    Foreach ($MMSFieldValue in $MMSFieldValueColl)
                                    {
                                        if($MMSFieldValue.label -ne $null)
                                        {
                                            $MMSFieldTerms+=$MMSFieldValue.label+"; "
                                        }
                                    }
....

I'm getting an error on my Foreach() statement:

Missing '=' operator after key in hash literal

1 Answer 1

0

[Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection] typecasting is incorrect and not needed because PnP PowerShell does that for you.

You can simply iterate over $MMSFieldValueColl and fetch the values as below:

$MMSFieldValueColl = $libitem["KBCategory"] 

#Concatenate each term in the value collection

$MMSFieldTerms = ""
Foreach ($MMSFieldValue in $MMSFieldValueColl)
{
    if($MMSFieldValue.Label -ne $null)
    {
        $MMSFieldTerms+=$MMSFieldValue.Label+"; "
    }
}
Write-host $MMSFieldTerms

UPDATE _________________________

function PopulateData($web, $incldeFileSize){
        Write-Host "Current Site " $web.url -ForegroundColor Cyan 
                  $libs = Get-PnPList -Web $web | Where {($_.BaseType -eq “DocumentLibrary”) }

                  foreach($lib in $libs){
                     $libitems = (Get-PnPListItem -Web $web -List $lib -Fields "FileLeafRef","Name","Title","Author","Modified","Created","KBAbstract","KBContentAuthor","KBCategory","Publish","KBPublishDate").FieldValues

                       foreach($libitem in $libitems)
                         {
                             if($libitem.FSObjType -eq "0"){
                              $data = @{
                                          "Web Name" = $web.Title

                                          "Library Name" = $lib.Title
                                          "File Name" = $libitem.FileLeafRef
                                          "Abstract" = $libitem.KBAbstractMicrosoft.SharePoint.Client.FieldUserValue
                                            $value = $libitem["KBContentAuthor"]
                                            $user = Get-PnPUser -Identity $value.LookupId
                                           "Content Author" = $user.Title
                                           $MMSFieldValueColl = $libitem["KBCategory"] 
                                            #Concatenate each term in the value collection
                                            $MMSFieldTerms = ""
                                            Foreach ($MMSFieldValue in $MMSFieldValueColl) {

                                                if($MMSFieldValue.label -ne $null)
                                                {
                                                    $MMSFieldTerms+=$MMSFieldValue.label+"; "
                                                }
                                            }
                                            Write-Host $MMSFieldTerms

                                         # "Knowledge Area" = $libitem.KBCategory
                                          "Publish" = $libitem.Publish
                                          "Published Date" = $libitem.KBPublishedDate.LookupValue
                                          "File URL" = $libitem.FileRef
                                          "Modified Date" = $libitem.Modified
                                          "Created By" = $libitem.Author.LookupValue
                                          "Created Date" = $libitem.Created
                                          "Modified By" = $libitem.Editor.LookupValue
                                          "File Size (KB)" = $null
                                     }
                             if($incldeFileSize -eq $true){
                                    $file = Get-PnPFile -Url $libitem.FileRef
                                    $data["File Size (KB)"] = $file.Length / 1KB
                              }
                              New-Object PSObject -Property $data
                            }
                         }
                  }       
}
5
  • Hi Gautam. I'm removed the cast yet I'm still gettiing the same error at the beginning of the Foreach statement. All of the code was working until I added the lines above. Jul 10, 2019 at 12:42
  • Hmm, not sure why its not working. Formatting seems to be correct. I tried a similar code with different column names and it worked. Can you try it for a single item as $listItem = (Get-PnPListItem -List "Documents" -Id 30).FieldValues and test it and then try it inside the loop. Do note, i have SP Online and not 2013, so probably something could be different :( Jul 10, 2019 at 16:07
  • Guatam... Can you check the update to the question? I've posted the entire function. Thanks! Jul 10, 2019 at 21:40
  • code seems fine, i tested with different column names and it works fine ! Did you try the above code in the comment above ? Jul 11, 2019 at 8:45
  • Hi Gautam, I resolved this issue by moving the foreach block that collects the $MMSFieldValues out of the Hash Table $data. Apparently, it only accepts name-value pairs. Thanks! Jul 12, 2019 at 17:23

Your Answer

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

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