Since SharePoint 2013 introduces a Representational State Transfer (REST) service that is comparable to the existing SharePoint client object models, it opens up a huge capabilities, in particular for administering and automating SharePoint Online when used with PowerShell.
My first idea was to utilize Invoke-RestMethod cmdlet, which was introduced in Windows PowerShell 3.0. Invoke-RestMethod cmdlet contains Credential parameter which could accept basic, digest, NTLM, and Kerberos authentication.
SharePoint Client Component SDK comes with a SharePointOnlineCredentials class which represents an object that provides credentials to access SharePoint Online resources. But unfortunately SharePoint Online credentials could not be passed in Invoke-RestMethod cmdlet, since claims based authentication is not supported by this cmdlet.
Below is demonstrated a simplified PowerShell script that to some extent mimics Invoke-RestMethod cmdlet. This script is intended for sending HTTPS request to a SharePoint Online REST service:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter(Mandatory=$True)] | |
[String]$Url, | |
[Parameter(Mandatory=$False)] | |
[Microsoft.PowerShell.Commands.WebRequestMethod]$Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get, | |
[Parameter(Mandatory=$True)] | |
[String]$UserName, | |
[Parameter(Mandatory=$False)] | |
[String]$Password | |
) | |
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" | |
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" | |
if([string]::IsNullOrEmpty($Password)) { | |
$SecurePassword = Read-Host –Prompt "Enter the password" –AsSecureString | |
} | |
else { | |
$SecurePassword = $Password | ConvertTo-SecureString –AsPlainText –Force | |
} | |
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword) | |
$request = [System.Net.WebRequest]::Create($Url) | |
$request.Credentials = $credentials | |
$request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f") | |
$request.Accept = "application/json;odata=verbose" | |
$request.Method=$Method | |
$response = $request.GetResponse() | |
$requestStream = $response.GetResponseStream() | |
$readStream = New-Object System.IO.StreamReader $requestStream | |
$data=$readStream.ReadToEnd() | |
$results = $data | ConvertFrom-Json | |
$results.d.results |
Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Example 1. Gets a value that specifies usage information about the site, including bandwidth, storage, and the number of visits to the site collection | |
$result = .\Invoke-RestSPO.ps1 –Url "https://contoso.sharepoint.com/_api/site/usage" –UserName "username@tenant.onmicrosoft.com" –Password "password" | |
write-host 'Total amount of disk space, currently being used by the site collection (Mb):' ([math]::round($result.Usage.Storage /1Mb)) | |
#Example 2. Get List Items | |
.\Invoke-RestSPO.ps1 –Url "https://contoso.sharepoint.com/_api/web/lists/getbytitle('Contacts')/title" –UserName "username@tenatnt.onmicrosoft.com" –Password "password" |
Pingback: Consuming the SharePoint 2013 REST API from PowerShell | Yet Another SharePoint Blog
Pingback: Invoke-SPORestMethod: Using the SharePoint Online REST API with PowerShell « SharePoint Automation
Pingback: Consuming the SharePoint Online REST API from PowerShell: Part 2 | YASP Blog
Thankyou! This was very helpful.
Hi Vadim, I tried to access “My site” but the script fails with
Exception lors de l’appel de « GetResponse » avec « 0 » argument(s) : « The request was aborted: The request was canceled. »
Au caractère C:\temp\sharepoint.ps1:23 : 1
+ $response = $request.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
I have to pass trough a proxy in my office, I tried
$request.proxy=New-Object system.net.webproxy(“http://10.24.18.18:8080″)
but it also failed.
What can I try next?
Thank you
Had the same issue, eventually resolved by setting the credentials with the domain.
$Credentials = New-Object System.Net.NetworkCredential($sUserName,$sPassword,$sDomain)
Please consult the example here
https://gallery.technet.microsoft.com/office/How-to-Get-all-the-Lists-a0af6259
I get a array dimensions exceed supported range, any ideas?