Graph - Update folder name using ItemId

Stan Verhoeckx 20 Reputation points
2023-04-20T09:51:48.68+00:00

We've been using the MS Graph API for an implementation for Sharepoint with our software and now I'm trying to update the name of a folder using the ItemId of that folder but it doesn't work. This is an example folder structure we have:

ERPDocuments (this is the root folder)
-> Companies (this is a subfolder)
-->00011 - ITTER [7] (subfolder, the 7 between the brackets is the ID within our software)
So I want to target the subfolder with [7] in the name since this is our unique identifier to locate the folder. Getting the ItemId for this folder was already quite a hassle since a wildcard isn't allowed but I did manage a way to get the ItemId. Now for the actual update to rename this folder I use the PATCH function: https://graph.microsoft.com/v1.0/drives/b!289pD_30x0aEAv-RDCFNFZq3ZiwaYnBJg2FFJvroZPZRRKqSnuDsS4qAez4zkMI1/items/01NLWS7HMHQIN4PFS2NJG35S4VPVKTE6XP
Request body:

{ "name": "00011 - ITTER2 [7]" }

Now this request returns an error:

"error": { "code": "itemNotFound", "message": "The resource could not be found.", "innerError": { "date": "2023-04-20T09:46:58", "request-id": "4d20f430-6bee-403f-89b8-4ee1662eb5ab", "client-request-id": "dd0a39fa-747c-c55b-2389-7c0682ca6a61" } }

And it seems as if I can't update it cause the ItemId couldn't be found. If I get the ItemId of the Companies folder and then try to update the name, it does work. So does this mean I can only update a folder if it's within the root folder? How am I suppose to update a folder then if it's 3 levels deep?

According to the documentation(https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_update?view=odsp-graph-online) I should be able to do PATCH /drives/{drive-id}/items/{item-id}?

Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft 365 and Office | OneDrive | For business | Windows
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-04-21T02:42:49.7233333+00:00

    Hi @Stan Verhoeckx , Per my test, I can update the folder name by PATCH /drives/{drive-id}/items/{item-id}. Here is the test result User's image

    You will need to check the drive id and item id. First you need to get your site id by following api

    https://graph.microsoft.com/v1.0/sites/Contoso.sharepoint.com:/sites/testsite?$select=id
    
    

    And the highlight marked id is site idUser's image

    Then use the siteid check the drive id

    https://graph.microsoft.com/v1.0/sites/siteid/drives
    
    

    User's image

    Finally get the itemid by the api, and you can update folder name with drive id and item id now.

    https://graph.microsoft.com/v1.0/me/drives/driveid/root/children
    
    

    User's image


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.