question

10031103-4153 avatar image
0 Votes"
10031103-4153 asked StefanCochrane-6251 commented

Alternative Save/OpenBinaryDirect methods for CSOM for SharePoint Online

Based on the doc from MS
https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/using-csom-for-dotnet-standard
12469-csom.png



So for .NET core app, what is the alternative way to read/write files stored in SharePoint online? what is the regular file API? does anyone done this? any example code/documentation?

office-sharepoint-online
csom.png (39.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MichaelHan-MSFT avatar image
2 Votes"
MichaelHan-MSFT answered StefanCochrane-6251 commented

You could use the file.OpenBinaryStream() to read/write files stored in SharePoint, below is my demo code for your reference:

         Microsoft.SharePoint.Client.File file = context.Web.GetFileByUrl("https://tenant.sharepoint.com/sites/michael/Shared%20Documents/aa.txt");
         context.Load(file);
         context.ExecuteQuery();       
     
         Microsoft.SharePoint.Client.ClientResult<Stream> mstream = file.OpenBinaryStream();
         context.ExecuteQuery();
            
         using (System.IO.StreamReader sr = new System.IO.StreamReader(mstream.Value))
         {
           String line = sr.ReadToEnd();
           Console.WriteLine(line);
         }



· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Very useful! Thank you!

But I would like to know how to upload a (local) file to a Folder.

The key is before the BinaryWriter, but I couldn't figure out.

Can you help me?

0 Votes 0 ·

Hi @FabioBrandao ,

For your new question, I would suggest you ask a new question in the forum.

0 Votes 0 ·

Hello! Since this original post helped, I thought I'd "pay it forward" (if you're still looking for help). Here's how:

At line 7, add the following snippet of code:

using (var fileStream = new System.IO.FileStream(destFilepath, System.IO.FileMode.Create))
{
mstream.Value.CopyTo(fileStream);
}

The "destFilepath" string is where you want to have the file name and folder structure locally (or in OneDrive). For example, it can be "C:\tmp\DownloadedFiles\folder1\filename.pdf" or similar, anywhere on your local computer. If a folder doesn't exist (e.g., "folder1"), the script will add it.

Good luck!

Steve in Spain

0 Votes 0 ·
Grmacjon-MSFT avatar image
0 Votes"
Grmacjon-MSFT answered

Hi @10031103-4153,

Welcome to Microsoft Q&A! Currently, Microsoft Q&A supports the products listed over here (more to be added later on).

Your question about Alternative Save/OpenBinaryDirect methods for CSOM for SharePoint Online is not yet supported on this platform. You could ask about this in the SharePoint Developers forums:https://techcommunity.microsoft.com/t5/sharepoint-developer/bd-p/SharePointDev


Thanks,

Grace


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

trevorseward avatar image
0 Votes"
trevorseward answered 10031103-4153 commented

.NET Standard CSOM is now available as of June 23rd on NuGet. You should be able to use this CSOM binary with your .NET Core app.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Unfortunately, MS drop some features, including this one, you can check this video: https://youtu.be/mb6OEDOhVW4, @ 2:35

0 Votes 0 ·