Forum Discussion
Do you know any example to upload file to a specific folder (identified by folderId)? with Graph v5.
Hello.
I'm just starting out with SharePoint and Microsoft 365.
I need to upload a file to a specific folder in a SharePoint library using Graph v5.61.
I've managed to upload it to the root of the library:
public async Task<string> UploadFile2Root(string listId, string fileName, byte[] fileBytes) { string fileId = ""; Drive? drive = await _graphClient.Sites[SITE_ID].Lists[listId].Drive.GetAsync(); GraphSDK.Drives.Item.Root.RootRequestBuilder targetFolder = _graphClient.Drives[drive?.Id].Root; using (MemoryStream stream = new MemoryStream(fileBytes)) { await targetFolder .ItemWithPath(fileName) .Content .PutAsync(stream); DriveItem? uploadedItem = await targetFolder.ItemWithPath(fileName).GetAsync(); fileId = uploadedItem?.Id ?? ""; } return fileId; }
The problem is when I try to use a specific folder (folderId has the correct value):
var targetFolder = _graphClient.Drives[drive?.Id].Items[folderId];
I always get the error: "Microsoft.Graph.Models.ODataErrors.ODataError: 'The resource could not be found.'"
Could you tell me how to solve the problem or an example that does this task?
I have also tried: https://learn.microsoft.com/en-us/answers/questions/1517434/how-to-upload-a-file-to-a-sharepoint-driveitem-fol and other examples, but without success.
I'm also trying to retrieve something similar to an ItemRequestBuilder.
Thank you very much in advance.
- mariscos25Copper Contributor
Hello.
I've already found the problem: the MsGraph driveId is not the SharePoint Id, this was unknown to me (I need to study the principles of Microsoft 365 and Graph to have a solid foundation). This are bad ids:Good ids look like (34 chars) 01FEXZ******************BDPP.
Good morning, sorry for the inconvenience.