[Python] Msgraph-sdk-python get sharepoint site id from name

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I want to work with SharePoint sites using Python. For now, I just want to ls on the site. I immediately ran into an issue where I need site id to do anything with it.

    How do I get site id from the site name? I did retrieve it using powershell:

    $uri = "https://graph.microsoft.com/v1.0/sites/"+$SharePointTenant+":/sites/"+$SiteName+"?$select=id"
    $RestData1 = Invoke-MgGraphRequest -Method GET -Uri $uri -ContentType "application/json"

    # Display Site ID
    $RestData1.displayName
    $RestData1.id


    But now I want to perform the same operation as above but using msgraph library. The problem is I am not sure how to do it. So far I have:

    import asyncio

    from azure.identity.aio import ClientSecretCredential
    from msgraph import GraphServiceClient

    tenant_id="..."
    client_id="..."
    client_secret="..."

    credential = ClientSecretCredential(tenant_id, client_id, client_secret)
    scopes = ['https://graph.microsoft.com/.default']

    client = GraphServiceClient(credentials=credential, scopes=scopes)

    sharepoint_tenant = "foobar365.sharepoint.com"
    site_name = "someTestSPsite"


    site_id = "f482d14d-6bd1-1234-gega-3121183eb87a" # how do I get this using GraphServiceClient from the site_name ?

    # so that I can do this
    # this works with the id retrieved from PS script
    drives = (asyncio.run(client.sites.by_site_id(site_id).drives.get()))
    print(drives)

    Continue reading...

Compartilhe esta Página