1

I have written a PowerShell script that queries a SharePoint list, but I am unable to get the previous versions of a 'Multiple lines of text' field.

I have append changes checked in the field settings, and can get the data I need if only one entry exists in that column.

I am able to get all other data I need from the list such as: Author, ID, Title, etc. So I think I am accessing the list correctly.

Is this possible? or will I need to find a different way to go about doing this.

I apologize if my code is hard to read or follow I'm very new to scripting.

#Import SP assembly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null

#Get creds for SP online
$credential = Get-Credential -Credential "[email protected]"

#Connect to SP
$credentials= New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName,$credential.Password)
$webURL="https://mine.sharepoint.com/sites/mysite"
$ctx= New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
$ctx.Credentials = $credentials

try{
    #Get the list
    $lists = $ctx.web.Lists
    $list = $lists.GetByTitle("mylist")
    #Get the list items
    $listItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
    $ctx.load($listItems)
    $ctx.executeQuery()
    #Loop through the list items
    foreach($listItem in $listItems){
        #get each of the column values     
        $Category = $listItem.FieldValues["Category"]  
        $Title = $listItem.FieldValues["Title"]
        $Origin = $listItem.FieldValues["Origin"]
        $Issues = $listItem.FieldValues["Issues"]
        $Site = $listItem.FieldValues["Site"]
        $IssueStatus = $listItem.FieldValues["Issue_x0020_Status"]
        $Priority = $listItem.FieldValues["Priority"]
        $StartDate = $listItem.FieldValues["Start_x0020_Date"]
        $DueDate = $listItem.FieldValues["Due_x0020_Date"]
        $CompletedDate = $listItem.FieldValues["Completed_x0020_Date"]
        $Modified = $listItem.FieldValues["Modified"]
        $Created = $listItem.FieldValues["Created"]
        #get the user object that created the item
        $Author = $listItem.FieldValues["Author"]
        #make sure the item is a user
        if($Author -is [Microsoft.SharePoint.Client.FieldUserValue]){
            $itemAuthor = $ctx.Web.GetUserById($Author.lookupId)
            $ctx.Load($itemAuthor)
            $ctx.ExecuteQuery()
        }else{
            $itemAuthor = ''
        }
        #get the user who last edited the object
        $Editor = $listItem.FieldValues["Editor"]
        #make sure the object is a person
        if($Editor -is [Microsoft.SharePoint.Client.FieldUserValue]){
            $itemEditor = $ctx.Web.GetUserById($Editor.lookupId)
            $ctx.Load($itemEditor)
            $ctx.ExecuteQuery()
        }else{
            $itemAuthor = ''
        }
        #Get the supervisor object
        $supervisorAssigned = $listItem.FieldValues["Supervisor_x002d_Assigned_x0020_"]
        #Make sure the item is a user
        if($supervisorAssigned -is [Microsoft.SharePoint.Client.FieldUserValue]){
            $supervisor = $ctx.Web.GetUserById($supervisorAssigned.lookupId)
            $ctx.Load($supervisor)
            $ctx.ExecuteQuery()
        }else{
            $supervisor = ''
        }
        #Try to get only text from field
        #$AdminComments = $listItem.FieldValues["Admin_x0020_Comments"] #This works, but only returns data if only 1 entry is present#
        $AdminComments = ''
        foreach($v in $listItem.Versions){
            $AdminComments+= $v["Admin_x0020_Comments"]
        }
        #Try to get only text from field
        #$OpsComments = $listItem.FieldValues["Ops_x0020_Comments"] #This works, but only returns data if only 1 entry is present#
        $OpsComments = ''
        foreach($v in $listItem.Versions){
            $OpsComments+= $v["Ops_x0020_Comments"]
        }
        #Write values to console
        #do something with the data later
        Write-Host "Title: " $Title "Category: " $Category "Origin: " $Origin "Issues: " $Issues "Site: " $Site "Admin Comments: " $AdminComments "Ops Comments: " $OpsComments "Supervisor: " $supervisor.LoginName "Status: " $IssueStatus "Priority: " $Priority "Start Date: " $StartDate "Due Date: " $DueDate "Completed Date: " $CompletedDate "Date Modified: " $Modified "Modified By: " $itemEditor.LoginName "Date Created: " $Created "Author: " $itemAuthor.loginName
    }
}
catch{
    write-host "$($_.Exception.Message)" -foregroundcolor red
}

1 Answer 1

1

You need to load and query for the Versions property. I'm surprised this didn't throw an exception on it.

$ctx.Load($listItem.Versions)
$ctx.ExecuteQuery()

 foreach($v in $listItem.Versions){
    $AdminComments+= $v["Admin_x0020_Comments"]
}

Also, is there any reason why you're not using PnP? You could do this whole script in under 20 lines.

2
  • That worked perfectly, thank you! There isn't a reason for doing it this way, I am looking a PnP now. I'm very new to SharePoint, and kinda new to PowerShell. Is there a way to do this where it returns text only? not the HTML.
    – fce_sp
    Commented Dec 16, 2019 at 20:12
  • I cannot overstate how useful the PnP PowerShell is. As far as the HTML goes, if the column is "rich text" you're stuck with it and have to strip it out. If you have the ability to change the field setting to "plain text" that will solve your problem. Commented Dec 16, 2019 at 22:40

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.