How to Download/Upload Files From/To Sharepoint 2013 Using Csom

Download a specific file from SharePoint (CSOM) using C#

For getting specific file, no need to get ListItem by Id, instead, directly pass server relativer url like this:

  ClientContext clientContext = new ClientContext("http://sp/sites/dev");
Web web = clientContext.Web;
Microsoft.SharePoint.Client.File filetoDownload = web.GetFileByServerRelativeUrl("/sites/dev/shared%20documents/mytest.txt");
clientContext.Load(filetoDownload);
clientContext.ExecuteQuery();
var fileRef = filetoDownload.ServerRelativeUrl;
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
var fileName = Path.Combine("C:\\", (string)filetoDownload.Name);

using (var fileStream = System.IO.File.Create(fileName))
{
fileInfo.Stream.CopyTo(fileStream);
}

For other usage, checkout,update,checkin, I would suggest you can still use CSOM, as there are buit-in functions support:

File methods

How to upload (multiple) files to SharePoint Online using CSOM?

I tested the below code in my local environment; it works fine.

<div>
<asp:FileUpload ID="upldGradeReport" runat="server" />
<asp:FileUpload ID="upldExpenseReceipt" runat="server" />

<asp:Button ID="btnSubmitForm" OnClick="SubmitButton_Click" runat="server" Text="Submit" />
</div>

protected void SubmitButton_Click(object sender, EventArgs e)
{
sendToSharePoint();
Response.BufferOutput = true;
Response.Redirect("Submission.aspx");
}

protected void sendToSharePoint()
{

try
{
string siteUrl = "https://tenant.sharepoint.com/sites/lee";

ClientContext clientContext = new ClientContext(siteUrl);
SecureString securePassword = new SecureString();
foreach (char c in "password".ToCharArray()) securePassword.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("lee@tenant.onmicrosoft.com", securePassword);
string sDocName = string.Empty;
string sDocName1 = string.Empty;
Uri uri = new Uri(siteUrl);
string sSPSiteRelativeURL = uri.AbsolutePath;
sDocName = UploadFile(clientContext,upldGradeReport.FileContent, upldGradeReport.FileName, sSPSiteRelativeURL, "MyDoc");
sDocName1 = UploadFile(clientContext,upldExpenseReceipt.FileContent, upldExpenseReceipt.FileName, sSPSiteRelativeURL, "MyDoc");

//prior CSOM code to insert values into a new List Item exists here

//clientContext.ExecuteQuery();
}
catch (Exception ex)
{
String ThisError = ex.Message;
}
}

public String UploadFile(ClientContext clientContext,Stream fs, string sFileName, string sSPSiteRelativeURL, string sLibraryName)
{
string sDocName = string.Empty;
try
{
var sFileURL = String.Format("{0}/{1}/{2}", sSPSiteRelativeURL, sLibraryName, sFileName);

Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, sFileURL, fs, true);
sDocName = sFileName;
}
catch (Exception ex)
{
sDocName = string.Empty;
//SPErrorMsg = ex.Message;
}
return sDocName;
}

Upload 5 MB files to sharepoint 2013 programmatically

The solution I went with:

MemoryStream destStream;

using (System.IO.FileStream fInfo = new FileStream(fileInfo.FullName, FileMode.Open))
{

byte[] buffer = new byte[16 * 1024];
byte[] byteArr;

using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = fInfo.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
byteArr = ms.ToArray();
}

destStream = new MemoryStream(byteArr);

Microsoft.SharePoint.Client.File.SaveBinaryDirect(
context,
serverRelativeURL + directory + fileInfo.Name,
destStream,
true);

context.ExecuteQuery();
results = "File Uploaded";
return true;
}

Uploading file using Managed client object model in sharepoint 2013

you can define max request length in SharePoint like this:
https://blogs.technet.microsoft.com/sammykailini/2013/11/06/how-to-increase-the-maximum-upload-size-in-sharepoint-2013/

In Central Admin>Application Management>Manage Web Applications> Select the desired web app and click General Settings on the ribbon

In web.config edit httpRuntime Node like this (for 2GB files and unlimited execution):



Related Topics



Leave a reply



Submit