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)
  • 1
    Pages in _layouts are pages that reside in the SharePoint server's hive. Why would you want to download these pages?
    – user6024
    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. Feb 19, 2019 at 18:31

1 Answer 1


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()
        [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)

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

        #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

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

                    #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

#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.

  • 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? 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.