Uploading File to Sharepoint Subsite via Graph API

Braun, Ehren [CORP/US] 0 Reputation points
2023-03-30T15:32:20.4+00:00

What I am looking to do is upload a file to a Sharepoint subsite using the Graph API. I have been able to create a upload session and upload to a sharepoint site referencing https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online, however, when I try creating a session upload for a subsite on a sharepoint site I get an "itemNotFound" error code.

As an example, the url for creating an upload session for a file called example.txt is shown below

https://graph.microsoft.com/v1.0/sites/{hostname},{spsite-id},{spweb-id}/drive/items/{item-id}:/example.txt:/createUploadSession

where {hostname},{spsite-id},{spweb-id} is the ID of the sharepoint site (see https://learn.microsoft.com/en-us/graph/api/resources/sharepoint?view=graph-rest-1.0 for an overview of sharepoint sites in the Graph API) and {item-id} is the driveItem ID for the folder on sharepoint that we want to upload to. Before we use a POST request with the url, we need to include a request body in a json format (as shown in the first link) and below is what that should be at a minimum

{"item": {

    "name": "example.txt"

    }

}

Doing what I have described above, this works for creating an upload session on a sharepoint site (see the first link for using the upload session url). For a subsite on a sharepoint site, this is what I tried and it did not work. In order to get the ID of the subsite, I used the url below as a GET request

https://graph.microsoft.com/v1.0/sites/{hostname},{spsite-id},{spweb-id}/sites

where {hostname},{spsite-id},{spweb-id} is the parent Sharepoint site ID. This url gives the subsites of a Sharepoint site and I was able to obtain the ID of the subsite that I wanted to upload to. When I use this ID (which is in the form {hostname},{spsite-id},{spweb-id}) with the url for creating an upload session, it fails.

My question is what is the correct url to create an upload session on a subsite? The documentation does not specify how to do this other than what I have already shown for a regular Sharepoint site. If someone could please help, it would be appreciated

Thanks

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,476 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,537 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 30,666 Reputation points Microsoft Vendor
    2023-03-31T05:38:42.43+00:00

    Hi @Braun, Ehren [CORP/US]

    You can use following api to create session

    POST /users/{userId}/drive/items/{itemId}/createUploadSession
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.