I am able to pick a document of my choosing from a SharePoint document library using web API calls and send it for rendering.
function Get-SpOosDocumentResponse {
[CmdletBinding()]
param (
[string]$SiteCollectionUrl,
[string]$DocLibraryName,
[string]$FolderPath = '.',
[string]$DocumentName
)
$RestApiUrl = "$SiteCollectionUrl/_api/web/GetFolderByServerRelativeUrl" +
"('$DocLibraryName/$FolderPath')/Files"
$ShpFolderResponse = Invoke-RestMethod `
-Uri $RestApiUrl `
-UseDefaultCredentials `
-Method Get `
-Headers @{
"Accept" = "application/json;odata=verbose"
}
$Files = $ShpFolderResponse.d.results
$DocId = $Files | where Name -eq $DocumentName | select UniqueId
if (!$DocId) {
Write-Error "$DocumentName not found." -ErrorAction Stop
}
$WopiUrl = "$SiteCollectionUrl/_layouts/WopiFrame2.aspx" +
"?sourcedoc={$DocId}&action=view"
$WopiResponse = Invoke-WebRequest -Uri $WopiUrl -UseDefaultCredentials
return $WopiResponse
}
But I can't find any good way to analyze the response if the rendering was successful, using PowerShell
, since the rendering seems to be taking place in the web browser with the use of javascript
.
I can't even detect if the WOPI-bindings are broken, using client-side access, since still no errors are generated that I'm able to detect.
The answer to this question is to use a web testing framework such as Selenium
.
Selenium
is supported by all major suppliers of web browsers and they release a Selenium
version (webdriver
) along with their regular releases of Chrome
, MS Edge
, Firefox
etc.
Selenium
is then accessed through libraries written in .Net
, Python
, Java
etc.
PowerShell actually do have a Selenium module that wraps .Net Selenium
(the project is looking for new maintainers).
To get this to work, you have to replace the webdriver
provided with the module with the version that fits your current web browser of choice.
All the selenium commands uses the Se
-prefix.
The commands I've explored so far are:
Start-SeDriver
- Starts a web browser controlled by Selenium
Set-SeUrl
- Navigates to the URL requestedSwitch-SeFrame
- Changes focus to specified iFrameGet-SeElement -By Id
- Fetches an element based on IdGet-SeElement -By XPath
- Filters out any elements based on the XPath
specifiedStop-SeDriver
- Stops the web browserTo get all elements available in the web page use
$elements = Get-SeElement -By XPath "//*"
To get the Id
of a specific element use
$elements[23].GetAttribute('id')
To help you out with finding out what elements is of interest in the web page, I highly recommend using a web browser extension such as Selenium IDE (abandoned project) or Katalon Recorder.
So a working example of checking if the OOS is loading could be.
$SiteCollectionUrl = 'https://my.site'
$DocLibraryName = 'Shared documents'
$FolderPath = '.'
$DocumentName = 'Document.docx'
$RestApiUrl = "$SiteCollectionUrl/_api/web/GetFolderByServerRelativeUrl" +
"('$DocLibraryName/$FolderPath')/Files"
$ShpFolderResponse = Invoke-RestMethod `
-Uri $RestApiUrl `
-UseDefaultCredentials `
-Method Get `
-Headers @{
"Accept" = "application/json;odata=verbose"
}
$Files = $ShpFolderResponse.d.results
$Doc = $Files | where Name -eq $DocumentName
$driver = Start-SeDriver `
-Browser Edge `
-StartUrl $SiteCollectionUrl `
-Size 1080x800 `
-State default `
-ImplicitWait 50
$WopiUrl = "$SiteCollectionUrl/_layouts/WopiFrame2.aspx?" +
"sourcedoc={$($Doc.UniqueId)}&action=view"
Set-SeUrl -Url $WopiUrl
Switch-SeFrame 0 # Enter the OOS realm
sleep 1
try {# getting OOS MainApp element
$MainApp = Get-SeElement -By Id 'MainApp' -Timeout 5 -EA Stop # Get OOS App info
[string[]]$MainAppText = $Mainapp.Text.Split("`r") # Copy Text to array
}
catch {# any terminting errors occurred such as not getting MainApp
return @('Error', 'OOS MainApp could not be found.')
}
To actually detect any errors with the rendering, you need to look for any error messages as well (not included in this answer as it adds to much to length and complexity).