Martin Muldoon
Martin Muldoon

Reputation: 3428

Issue with PnP PowerShell code syntax error

I have a function that iterates through a document library and creates a report that lists the metadata associated with each item.

It won't compile. At the function declaration at the first bracket, I'm getting an error:

Missing closing '}' in statement block or type definition

I have identified the lines of code that seem to be the source of the error. When I cut the code on the lines between # the error goes away.

I can't figure where the syntax is incorrect. I'm using:

SharePointPnPPowerShell2013 3.11.1907.0

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

                  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.KBAbstract
                                          "Content Author" = $libitem.KBContentAuthor.Email

                                          ############The Problem is somewhere in these Lines of code###########
                                          [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem.KBCategory
                                            $MMSFieldTerms = ""
                                            Foreach($MMSFieldValue in $MMSFieldValueColl)
                                            {
                                                if($MMSFieldValue.label -ne $null)
                                                {
                                                    $MMSFieldTerms+=$MMSFieldValue.label+"; "
                                                }
                                            }
                                            write-host $MMSFieldTerms
                                            ##########The Problem ends here############

                                          "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
                            }
                         }
                  }       
}

Upvotes: 0

Views: 189

Answers (2)

Theo
Theo

Reputation: 61068

You should move the whole block

############The Problem is somewhere in these Lines of code###########
[Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem.KBCategory
$MMSFieldTerms = ""
Foreach($MMSFieldValue in $MMSFieldValueColl) {
    if($MMSFieldValue.label -ne $null) {
        $MMSFieldTerms+=$MMSFieldValue.label+"; "
    }
}
write-host $MMSFieldTerms
##########The Problem ends here############

outside of the hashtable definition of $data. In there, only name-value pairs are expected. I'd move if higher up, just below if($libitem.FSObjType -eq "0"){ and before $data = @{, so you could add it to the hashtable if you want as property

"Field Terms" = $MMSFieldTerms -join ';'


Sidenote 1: instead of using string concatenation, why not build the $MMSFieldTerms as array and join the elements with ';' afterwards like:

[Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem.KBCategory
$MMSFieldTerms = foreach($MMSFieldValue in $MMSFieldValueColl) {
    if($MMSFieldValue.label) {
        $MMSFieldValue.label
    }
}

write-host ($MMSFieldTerms -join ';')

Sidenote 2: it is best to replace all curly 'smart-quotes' by straight ones: “DocumentLibrary” --> "DocumentLibrary"

Upvotes: 1

user847990
user847990

Reputation:

I don't have SharePoint to execute the code and test. The only thing I can spot is your Foreach is not being set to a property. Since you are using $data to try and build an hashtable the collection you are joining needs to be set into a key/value pair.

So this:

                    ############The Problem is somewhere in these Lines of code###########
                    [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem.KBCategory
                    $MMSFieldTerms = ""
                    Foreach($MMSFieldValue in $MMSFieldValueColl) {
                        if($MMSFieldValue.label -ne $null) {
                            $MMSFieldTerms+=$MMSFieldValue.label+"; "
                        }
                    }
                    write-host $MMSFieldTerms
                    ##########The Problem ends here############

Should be something like this: (added **** around it to show the difference only)

            ############The Problem is somewhere in these Lines of code###########
            [Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection]$MMSFieldValueColl = $libitem.KBCategory
            $MMSFieldTerms = ""
            "****Some Property****" = Foreach($MMSFieldValue in $MMSFieldValueColl) {
                if($MMSFieldValue.label -ne $null) {
                    $MMSFieldTerms+=$MMSFieldValue.label+"; "
                }
            }
            write-host $MMSFieldTerms
            ##########The Problem ends here############

Upvotes: 0

Related Questions