Call REST API Methods in SharePoint Online from PowerShell

Requirement: Call SharePoint Online REST API from PowerShell.

PowerShell to Call a REST API method in SharePoint Online

For those unfamiliar with the concept, REST stands for Representational State Transfer and is an architecture that can provide interoperability between computer systems over a network such as the Internet. The REST API is a web service that provides SharePoint data and functionality access. It can be used for many different purposes, such as building custom applications, creating one-off scripts for automating tasks, or integrating with other systems. You can use any programming language that supports HTTP requests and responses. This blog post will show you how to call the SharePoint Online REST API from PowerShell and return results in JSON format.

Here is how to call SharePoint Online REST API using PowerShell:

#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 Get-SPOUserProfileProperty ($SiteURL, $UserID)
{
    #Setup Credentials to connect
    $Cred = Get-Credential

    #Connect to Site
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    $Context.ExecuteQuery()    

    #Frame REST API URL
    $RequestUrl = "$($SiteUrl)/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor([email protected])[email protected]='i:0%23.f|membership|$($UserID)'"
    $AuthenticationCookie = $Context.Credentials.GetAuthenticationCookie($SiteUrl, $true)
    $WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $WebSession.Credentials = $Context.Credentials
    $WebSession.Cookies.SetCookies($SiteUrl, $AuthenticationCookie)
    $WebSession.Headers.Add("Accept", "application/json;odata=verbose")

    #Invoke Rest Method
    $Result = Invoke-RestMethod -Method Get -WebSession $WebSession -Uri $RequestURL
    Return ($Result.d.userprofileproperties.results)
}

#Set Parameters
$SiteURL="https://Crescent.sharepoint.com" 
$UserID="[email protected]"

#Call the function to get user profile properties
Get-SPOUserProfileProperty -SiteUrl $SiteUrl -UserID $UserID | Select Key, Value

This retrieves all user profile properties of a given user

call sharepoint online rest api from powershell

SharePoint Online: PnP PowerShell to Call REST API

We can invoke a REST API method from PnP PowerShell with Invoke-PnPSPRestMethod:

#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"

#Connect to the Site
Connect-PnPOnline -Url $SiteURL -Interactive 

#Define the Rest Method
$RestMethodURL = $SiteURL+'/_api/web/lists?$select=Title' 

#Invoke Rest Call to Get All Lists
$Lists = Invoke-PnPSPRestMethod -Url $RestMethodURL
$Lists.value

Please note, REST API methods are case-sensitive! ($Lists.value and $Lists.Value is not the same.)

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

3 thoughts on “Call REST API Methods in SharePoint Online from PowerShell

  • Unfotunately no. Link you shared is conencting using Pnp Powershell.

    We need way to call Sharepoint rest API using APp ID and Secret.

    Reply
  • IS there way to make this Unattended i.e. instead of hardcoding with username password , use AppID and Secrete to make calls? (I tried and credential object is null)

    Reply

Leave a Reply

Your email address will not be published.