PnP PowerShell : Fetch SharePoint Site and List ID

 

Introduction

Many a time we would need to get the ID of SharePoint List or Site for use with some kind of development activities. Though we have a shortcut to get the list ID out of the box from the UI, the Site collection ID is something that is obfuscated from the UI. In this article, we will see how to use PnP PowerShell to fetch a specific Site or List Unique ID.

Use Case

In case we want to work with Graph API to create a SharePoint List Item, we will be issuing a POST request to the URL: https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items

with the body payload

{
“fields”: {
“Title”: “1”,
“Source”: “Created from Microsoft Graph”
}
}

This will create the list item in the SharePoint list. However, we need to know the Site ID and the List ID where we want to create the list item.

Fetch the Site ID

We will be using PnP PowerShell to connect to SharePoint Online using the Connect-PnPOnline cmdlet and then use the Get-PnPSite command to fetch the details of the site. Ensure to use the -Includes parameter and mention the ID attribute which will be returned with the site. In case we need to fetch any of the remaining properties listed here, add them as well.

Get-PnPSite Official Documentation

Script

#SiteURL Variable
$SiteURL = “https://office365journey.sharepoint.com/sites/2022CovidActions”

#Connect to SharePoint Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Use the Includes parameter to specify the ID attribute to be returned in the object
$Site = Get-PnPSite -Includes ID

#Get Site Collection ID
$Site.Id
Text

Description automatically generated

Fetch the List ID

Similar way, we can fetch the list id by using the Get-PnPList command.

Get-PnPList Official Documentation

Script

#SiteURL Variable
$SiteURL=”https://office365journey.sharepoint.com/sites/2022CovidActions”

#List Name Variable
$ListName = “GraphTest”

#Connect to SharePoint Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get List Object
$List = Get-PnPList -Identity $ListName

#Get the List ID
$List.Id
Text

Description automatically generated

Easier way to get List ID

However we do have an easier way to fetch the list id. Head over to the List Settings page of the corresponding list and right click the Audience targeting settings and copy the link.

Graphical user interface, text, application

Description automatically generated

Pasting it over to a notepad will reveal the List ID located towards the end of the URL

Summary

Thus we saw how to fetch the Site and List ID using PnP PowerShell which will help us while working on operations related to SharePoint Site or List

Related Articles

Author

Author

Priyaranjan KS is a Modern Workplace Architect primarily focused on developing and architecting solutions around Office 365,Power Platform and Azure.He is also a Microsoft Most Valuable Professional(MVP) and a Microsoft Certified Trainer(MCT)

Latest Articles