Download file from sharepoint online document site by microsoft graph

Copper Contributor

Hello everybody.

I state that I am new in programming with microsoft graph and sharepoint.

 

I need to download a file.
I performed these steps:
1) Find the Ids of the files in the site:

var lists = client

.Sites ["ateikonnet.sharepoint.com, xxxxxxxx-2bf3-4b3b-85d4-xxxxxxxx, c12ae315-da0e-4695-81c6-4f62629953f9"]

.Lists ["42e26619-xxxxx-xxxx-b7f1-e4b2efc46d7e"]

.Items

.Request ();
var result = lists.GetAsync (). Result;
foreach (var file in result)
{Console.WriteLine (file.Id + ":" + file.Name + ":" + file.WebUrl);}

The result is:
4:: https://ateikonnet.sharepoint.com/sites/Documentale/Documenti/cartella1 
6:: https://ateikonnet.sharepoint.com/sites/Documentale/Documenti/Cartella2 
8:: https://ateikonnet.sharepoint.com/sites/Documentale/Documenti/setdocumenti1 
9:: https://ateikonnet.sharepoint.com/sites/Documentale/Documenti/setdocumenti1/Cartella%20lavoro.xlsx 
10:: https://ateikonnet.sharepoint.com/sites/Documentale/Documenti/cartella1/Documento.docx 
11:: https://ateikonnet.sharepoint.com/sites/Documentale/Documenti/Documento.docx 

(I notice that Id is strange, it is not a Uid. Correct ?)

 

2) i am trying this code, but it doesn't work. Any suggestions ?

var fileId3 = "11";

var request3 = client
.Sites["ateikonnet.sharepoint.com, xxxxxxxx-2bf3-4b3b-85d4-xxxxxxxx, c12ae315-da0e-4695-81c6-4f62629953f9"]
.Drive
.Items[fileId3]
.Content
.Request();

var stream = request3.GetAsync().Result;
var driveItemPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "driveItem_" + fileId3 + ".docx");
var driveItemFile = System.IO.File.Create(driveItemPath);
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(driveItemFile);
Console.WriteLine("Saved file to: " + driveItemPath);

 

3) The error is:
ServiceException: Code: itemNotFound
Message: The resource could not be found.
Inner error:
AdditionalData:
date: 2022-03-15T08:04:51
request-id: 6c97bd8d-d628-4700-bcf7-2f2fb02f727f
client-request-id: 6c97bd8d-d628-4700-bcf7-2f2fb02f727f
ClientRequestId: 6c97bd8d-d628-4700-bcf7-2f2fb02f727f

 

 

Can you help me, thanks in advance for the reply
Gimmy


P.s.
if I download from the Onedrive folder it works. This is the code:

var fileId3 = "01V66FJ6YRL46JPHGK45CZ2PWTHBG7WX4I";
var request3 = client.Me.Drive.Items[fileId3].Content.Request();

var stream = request3.GetAsync().Result;
var driveItemPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "driveItem_" + fileId3 + ".pdf");
var driveItemFile = System.IO.File.Create(driveItemPath);
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(driveItemFile);
Console.WriteLine("Saved file to: " + driveItemPath);

0 Replies