pythonazuresharepointmicrosoft-graph-apionedrive

Trouble/How to get the Document Library of my SharePoint Site using GRAPH API


If I use the following code sample I am able to get my SharePoint Site entity which looks like this: enter image description here But the fields drive or drives which should contain the Drive entity of the Site entity (where there is the Document Library) are None. Also the lists field is None (AFAIK drives are a special type of lists with template "Document Library", so I expected to see everything here). On the other hand, using the dashboard of my site directly I am able to upload/download files from the Document Storage: enter image description here

import os
import jwt
from typing import Optional, Literal
from azure.identity.aio import ClientSecretCredential
from msgraph import GraphServiceClient
from msgraph.generated.models.drive_item import DriveItem
from msgraph.generated.models.folder import Folder
from msgraph.generated.drives.item.items.item.content.content_request_builder import ContentRequestBuilder
from kiota_abstractions.base_request_configuration import RequestConfiguration
from msgraph.generated.models.o_data_errors.o_data_error import ODataError
from src.config.loader import CONFIG
import aiofiles
from src.utils.logger import get_logger

logger = get_logger(CONFIG.logging.name, CONFIG.logging.level)

class GraphClientSingleton():
    _client = None
    _credentials = None

    @classmethod
    def get_client(cls):
        if cls._client is None:
            # cls._authority = 'login.microsoftonline.com'
            cls._credentials = ClientSecretCredential(
                tenant_id = CONFIG.msgraph.tenant_id_sharepoint,
                client_id = CONFIG.msgraph.client_id_sharepoint,
                client_secret = CONFIG.msgraph.client_secret_sharepoint,
            )
            cls._scopes = ['https://graph.microsoft.com/.default']

            cls._client = GraphServiceClient(credentials=cls._credentials, scopes=cls._scopes)
        return cls._client

if __name__ == "__main__":
    async def test_api():
        sp_dao = SharePointDAO()
        site_id = ... # my site id
        site = await sp_dao.client.sites.by_site_id(site_id).get()
        logger.info(site)
    import asyncio
    asyncio.run(test_api())

My questions are:

  1. How is it possible to see the Document Library I am using in the dashboard of the site if I don't see it in any fields on my Site object? Are they related somehow?
  2. If I wish to manage this Drive entity how can I do it given my snippet of code and starting from the site_id as information to crawl into the right resource?

Solution

  • If you have siteId, you can list all document libraries by calling

    result = await graph_client.sites.by_site_id('site-id').drives.get()
    

    or expand the drives relationship

    query_params = SiteItemRequestBuilder.SiteItemRequestBuilderGetQueryParameters(
            expand = ["drives"],
    )
    
    request_configuration = RequestConfiguration(
    query_parameters = query_params,
    )
    
    result = await graph_client.sites.by_site_id('site-id').get(request_configuration = request_configuration)