What is the best way to get the Site id for a sharepoint document using the docuemnt_url

GSH-1402 105 Reputation points
2025-02-10T10:30:10.7833333+00:00

I have developed a Microsoft add-in and I am hoping to enable support for the word documents stored in the Sharepoint.

In order to do that what is the best approach to get the document metadata (site_id, docuemnt_id) provided that I can get the document_url.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,309 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,448 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ling Zhou_MSFT 23,035 Reputation points Microsoft External Staff
    2025-02-11T02:36:31.25+00:00

    Hi @GSH-1402,

    Thanks for reaching out to us. We are very pleased to support you.

    According to the information you provided and the research I have done, I may understand that you need to use MS Graph to get the site id and document id. If there is anything wrong, please correct me.

    If there is not anything wrong, it is suggested that you please try the following solution:

    1. Get site id Intercept your domain name and site name from your document url and use the following request:
         GET https://graph.microsoft.com/v1.0/sites/{Your Tenant Name}.sharepoint.com:/sites/{Your Site Name}?$select=ID
      
      The id property is the site id: User's image
    2. Get document id
      • Before getting the document id we need to get the drive id of the document library, please use the following request to get the name and drive id of all the document libraries in the site and then filter by repository name in your application. (Because the request does not support filter, so we can only query the drive id of all the document libraries)
             GET https://graph.microsoft.com/v1.0/sites/{your site id}/drives?$select=id,name
        
        This is the returned response, the id property is the drive id: User's image
      • Use the following request to get information about the file. Note that the relative file path here starts with the folder containing the file and ends with the file name, or directly with the file name if there is no folder.
             GET https://graph.microsoft.com/v1.0/sites/{site id}/drives/{drive id}/root:/{relative file path}
             
             Example:
             https://graph.microsoft.com/v1.0/sites/{site id}/drives/{drive id}/root:/TestFolder/test_file.txt
        
        The etag property in the returned response contains the document id: User's image

    If you have any questions, please do not hesitate to contact me.

    Moreover, if the issue can be fixed successfully, please click "Accept Answer" so that we can better archive the case and the other community members who are suffering the same issue can benefit from it.

    Your kind contribution is much appreciated.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.