Tuesday, October 10, 2017

SharePoint Online, PowerShell and Rest API : Introduction


In this blog i will show you how to execute Rest API calls via PowerShell for SharePoint Online(Office 365 Tenant).

I am using SharePoint Online PnP MSI here.

You must have already heard about PowerShell MSI for SharePoint Online developed by Erwin van Hunen and included in SharePoint PnP, i will use the same for connecting to SharePoint online via Powershell, you can download the same using this url:-

https://github.com/SharePoint/PnP-PowerShell/releases

Note:- You will need to have Powershell 3.0 to use SharePoint Online msi of SharePoint PnP.

to View all Commands in PnP, type :-

Get-Command -Module *PnP*

Here is the URL for help documentation for commands:-

https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/readme.md


Once you have installed it, lets see how to execute Rest API calls via Powershell.

You can use Windows Powershell ISE, SharePoint Online Management Shell, CMDER

I am using CMDER to demo this:-

Open CMDER and type Powershell, and then load modules:-

If you are using cmder first time and would like to work using it with PowerShell Online, then import the module like this:-












Now we are ready to write our Command to talk to Rest API.

Follow these simple steps:-

  1. Connect to SP Online:-

     
    $email = Read-Host -Prompt "Please enter your tenant account email"
    $pass = Read-host -AsSecureString "Please enter tenant admin password"
    $credentials = New-Object –TypeName "System.Management.Automation.PSCredential"
     –ArgumentList $email, $pass
    $targetSite = "https://.sharepoint.com/sites//"
    $targetSiteUri = [System.Uri]$targetSite
    Connect-PnPOnline $targetSiteUri -Credentials $credentials
    
    
    
  2. Next retrieve the context from the connection:-

     
     $context = (Get-PnPWeb).Context
    
  3. Grab related authentication cookies

     
    #this is different object then other credentials used earlier
    $spcredentials = $context.Credentials
    $authenticationCookies = $spcredentials.GetAuthenticationCookie($targetSiteUri,
    $true)
    
    
  4. Initiate a web session and set Cookies and accept headers to it

    $webSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $webSession.Cookies.SetCookies($targetSiteUri, $authenticationCookies)
    $webSession.Headers.Add("Accept", "application/json;odata=verbose")
    
    
  5. Rest call constructs, here we are just retrieving data of a library with a normal get request.

    $targetLibrary = "Documents"
    $apiUrl = "$targetSite" + "/_api/web/lists/getByTitle('$targetLibrary')"
    
  6. Invoke the request

    $webRequest = Invoke-WebRequest -Uri $apiUrl -Method Get -WebSession $webSession
    
  7. Get the results

    $jsonLibrary = $webRequest.Content | ConvertFrom-Json
    
This is how it will appear in the shell:-







we have successfully executed our Rest Call to SharePoint online.

To view the results you can consider d as an object and execute Select:-



Entire PS Script:-

$email = Read-Host -Prompt "Please enter your tenant account email"
$pass = Read-host -AsSecureString "Please enter tenant admin password"
$credentials = New-Object –TypeName "System.Management.Automation.PSCredential"
 –ArgumentList $email, $pass

$targetSite = "https://.sharepoint.com/sites//"
$targetSiteUri = [System.Uri]$targetSite
Connect-PnPOnline $targetSiteUri -Credentials $credentials
$context = (Get-PnPWeb).Context
#this is different object then other credentials used earlier
$spcredentials = $context.Credentials
$authenticationCookies = $spcredentials.GetAuthenticationCookie($targetSiteUri,
$true)

$webSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$webSession.Cookies.SetCookies($targetSiteUri, $authenticationCookies)
$webSession.Headers.Add("Accept", "application/json;odata=verbose")
$targetLibrary = "Documents"
$apiUrl = "$targetSite" + "/_api/web/lists/getByTitle('$targetLibrary')"
$webRequest = Invoke-WebRequest -Uri $apiUrl -Method Get -WebSession $webSession
$jsonLibrary = $webRequest.Content | ConvertFrom-Json
$jsonLibrary.d | Select *

No comments:

Post a Comment