How to Get Site Id of a SharePoint Site using MS Graph API

get-site-id-from-graph-explorer
Liked the Article? Share it on Social media!

There was a requirement I had back then to create a Java Application to work with SharePoint. In the beginning, I had no idea what SharePoint was and what the use of it. You can use SharePoint as a secure place to store, organize, manage, share and access information. It’s like a central place where organizations employees can access the content. A site is addressed by a unique identifier which is a composite ID. It composes of hostname, collection unique id, and the unique site id. Let’s see how we can use Microsoft Graph Explorer to find the Site Id of a SharePoint Site.

What is a SharePoint Site?

So, I had a requirement to upload some video and document files to a specific SharePoint site. A Site is a kind of website. In a SharePoint account, we can create more than one sites. So we call it site collection.

There are various things we can configure when we are creating a site.

  • Templates
  • Language
  • Web pages
  • Content types
  • Apps

I’m not going to go into details about every feature. If you’re interested, read the Microsoft official documentation. You will get all the information from there. They have excellent documentation.

What is Graph Explorer?

Graph Explorer is a web-based tool that you can use to test API requests using Microsoft Graph APIs in a very convenient manner. You can play around with demo data in there. But if you need data about your tenant, then you have to sign in.

You can access Graph Explorer at: https://developer.microsoft.com/graph/graph-explorer

How to use Graph Explorer?

  1. Sign in to the graph explorer
  2. Select the HTTP method which you want to perform
  3. Select the version of API that you want to use, either v1.0 or beta
  4. Type the query in the request box
  5. Hit the Run Query

Then it will show the response of your query as JSON data. Below image shows the all the steps above.

graph-explorer-all-steps

How to get a specific SharePoint Site id?

You might be wondering, why the heck I need site id? If you’ve come across accessing SharePoint data programmatically, then on the line, you will need a site id. For various things. Such as uploading content, deleting them, moving them etc.

Below is the query for getting site id for your SharePoint account.

https://graph.microsoft.com/v1.0/sites/<hostname>.sharepoint.com:/sites/<sitename>

Replace <hostname> and <sitename> with your hostname and sitename.

After executing the above query, it will give you the id of your specific site. In the JSON response, you will get a key as id, then extract the value of id.

That value contains three-piece of data,

  1. Hostname
  2. SharePoint site id
  3. SharePoint web id – you don’t need this one

You only need the SharePoint Site id. Below is my query request response.

site-id-response

Conclusion

In this short tutorial, you learn about the Graph Explorer web tool. You can play around it. There are many graph APIs to play. On the left side of the graph explorer, you can see all the services it provides. Read this tutorial to get the practical usage of SharePoint Site Id.


Liked the Article? Share it on Social media!

Leave a Comment

Your email address will not be published.

Scroll to Top