How to get all the files in OneDrive account using the graph SDK?

I’m trying to use a service account to pull all files from a OneDrive business account using the MS Graph Python SDK.

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

microsoft_tenant_id = '123abc'
client_id = '123abc'
client_secret = '123abc'

SCOPES = ['https://graph.microsoft.com/.default']

credential = ClientSecretCredential(microsoft_tenant_id, client_id, client_secret)
graph_client = GraphServiceClient(credential, SCOPES)

user_id = '[email protected]'


async def get_drive_count():

# What do I use after .drives?

    response = await graph_client.users.by_user_id(user_id).drives... # not sure what to use next
    

asyncio.run(get_drive_count())

I can’t find any examples on how to use the graph client to pull one drive files.

I’ve tried using .root.children.get() but the SDK doesn’t have any of those methods.

Does anyone know how to pull all OneDrives files using their SDK?

You need to get user’s drive id and then recursively iterate through the all folders in that drive and count number of items.

I’m not familiar with python, but something like the code below should work.

Be aware that if a folder has more than 200 items, you need to use paging to retrieve all items.

import asyncio
from typing import List
from msgraph import GraphServiceClient
from azure.identity import ClientSecretCredential

microsoft_tenant_id = '123abc'
client_id = '123abc'
client_secret = '123abc'

SCOPES = ['https://graph.microsoft.com/.default']

credential = ClientSecretCredential(microsoft_tenant_id, client_id, client_secret)
graph_client = GraphServiceClient(credential, SCOPES)

user_id = '[email protected]'

items_count = 0

async def get_child_items_count(driveId: str, driveItemId: str) -> None:
    all_drive_item_folders_ids: List[str] = []
    # process first page
    childItems = await graph_client.drives.by_drive_id(driveId).items.by_drive_item_id(driveItemId).children.get()
    if childItems:
        # increment global counter
        items_count+=len(childItems.value)
        # store folders ids
        for i in range(len(childItems.value)):
            child_item = childItems.value[i]
            if child_item.folder is not None and child_item.folder.child_count > 0:
                all_drive_item_folders_ids.append(child_item.id)
    
    # process other pages
    while childItems is not None and childItems.odata_next_link is not None:
        childItems = await graph_client.drives.by_drive_id(driveId).items.by_drive_item_id(driveItemId).children.with_url(childItems.odata_next_link).get()
        if childItems:
            # increment global counter
            items_count+=len(childItems.value)
            # store folders ids
            for i in range(len(childItems.value)):
                child_item = childItems.value[i]
                if child_item.folder is not None and child_item.folder.child_count > 0:
                    all_drive_item_folders_ids.append(child_item.id)

    # counts items in subfolders
    for i in range(len(all_drive_item_folders_ids)):
        await get_child_items_count(driveId, all_drive_item_folders_ids[i])  

async def get_drive_count():
    # get drive id
    response = await graph_client.users.by_user_id(user_id).drive.get()
    driveId = response.id
    # start with root item
    await get_child_items_count(driveId, 'root')
    
    print(f"count: {items_count}")

asyncio.run(get_drive_count())

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật