0

Would appreciate some help here.

Using the following powershell code I can access all Sharepoint online web pages but for any page containing _layouts I get:

Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (401) Unauthorized."

I am using an admin account.

What setting do I need to change to access these pages via this method?

$webClient = New-Object System.Net.WebClient 
$webClient.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
$webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$HTML = $webClient.DownloadString($cloudUrl)
2
  • 1
    Pages in _layouts are pages that reside in the SharePoint server's hive. Why would you want to download these pages?
    – user6024
    Commented Feb 19, 2019 at 17:46
  • Hi, Trevor. I want to access the version history of a list item. I have not found any other method to get this data in CSOM, and so the idea is to get that page and parse the HTML to get the info I need. Commented Feb 19, 2019 at 18:31

1 Answer 1

0

If you want to get version history of list items in SharePoint Online, the following PowerShell script for your reference.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Export-VersionHistory()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $CSVFile
    )
    Try {

        #Delete the Output report file if exists
        if (Test-Path $CSVFile) { Remove-Item $CSVFile }

        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Get all items
        $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()

        #Array to hold result
        $VersionHistoryData = @()

        #Iterate throgh each item
        Foreach ($Item in $ListItems)
        {
            write-host "Processing Item:" $item.id -f Yellow

            #Get all versions of the list item
            $Versions = $Item.versions
            $ctx.Load($Versions)
            $Ctx.ExecuteQuery()

            If($Versions.count -gt 0)
            {
                #Iterate each version
                Foreach($Version in $Versions)
                {
                    #Get the Creator object of the version
                    $CreatedBy =  $Version.createdby
                    $Ctx.Load($CreatedBy)
                    $Ctx.ExecuteQuery()

                    #Send Data to object array
                    $VersionHistoryData += New-Object PSObject -Property @{
                    'Item ID' = $Item.ID
                    'Title' =  $Version.FieldValues["Title"]
                    'Version Label' = $Version.VersionLabel
                    'Version ID' = ($Version.VersionId/512)
                    'Created On' = (Get-Date ($Version.Created) -Format "yyyy-MM-dd/HH:mm:ss")
                    'Created By' = $CreatedBy.Email
                    }
                }
            }
        }

        #Export the data to CSV
        $VersionHistoryData | Export-Csv $CSVFile -Append -NoTypeInformation

        write-host -f Green "Version History Exported Successfully to:" $CSVFile
     }
    Catch {
        write-host -f Red "Error Exporting version History to CSV!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://tenant.sharepoint.com/sites/lz"
$ListName="CustomList"
$CSVFile="C:\Temp\VersionHistory.csv"

#Call the function to generate version History Report
Export-VersionHistory -SiteURL $SiteURL -ListName $ListName -CSVFile $CSVFile

Note: Download the latest CSOM library for SharePoint Online.

1
  • LZ_MFST. This is great. I realized that I actually had this code working before for SharePoint online but not for premises. (I'm ultimately trying to synchronize lists using only CSOM code) That is, I tried for on premises and failed, assumed it wouldn't work on the cloud, and so went the WebClient route. Assuming the difference in connecting but all else the same, any idea why I get "Field or property "Versions" does not exist." for an on premises? Commented Feb 20, 2019 at 16:44

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.