Search code examples
c#.netsharepointlarge-files

SharePoint Online, uploading files larger than 2mb using SharePointClient


While trying to upload files to SharePoint online, remotely via SharePointClient upload, I am encountering a file size limit of 2mb. From my searches it seems that people have overcome this limit using PowerShell, but is there a way to overcome this limit using the native SharePointClient package in .Net C#? Here is my existing code sample:

    using (var ctx = new Microsoft.SharePoint.Client.ClientContext(httpUrl))
    {
        ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, passWord);
        try
        {
            string uploadFilename = string.Format(@"{0}.{1}", string.IsNullOrWhiteSpace(filename) ? submissionId : filename, formatExtension);
            logger.Info(string.Format("SharePoint uploading: {0}", uploadFilename));
            new SharePointClient().Upload(ctx, sharePointDirectoryPath, uploadFilename, formatData);
        }
    }

I have read from the following site that you can use the ContentStream just not sure how that maps to SharePointClient (if at all):

https://msdn.microsoft.com/en-us/pnp_articles/upload-large-files-sample-app-for-sharepoint

UPDATE:

Per the suggested solution I now have:

public void UploadDocumentContentStream(ClientContext ctx, string libraryName, string filePath)
{

    Web web = ctx.Web;

    using (FileStream fs = new FileStream(filePath, FileMode.Open))
    {
        FileCreationInformation flciNewFile = new FileCreationInformation();

        // This is the key difference for the first case - using ContentStream property
        flciNewFile.ContentStream = fs;
        flciNewFile.Url = System.IO.Path.GetFileName(filePath);
        flciNewFile.Overwrite = true;

        List docs = web.Lists.GetByTitle(libraryName);
        Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(flciNewFile);

        ctx.Load(uploadFile);
        ctx.ExecuteQuery();
    }
}

Still not quite working, but will update again when it is successful. Current error is :

Could not find file 'F:approot12-09-2017.zip'.

FINALLY

I am using files from Amazon S3 so the solution was to take my byte data and to stream that to the call:

public void UploadDocumentContentStream(ClientContext ctx, string libraryName, string filename, byte[] data)
{

    Web web = ctx.Web;
    FileCreationInformation flciNewFile = new FileCreationInformation();
    flciNewFile.ContentStream = new MemoryStream(data); ;
    flciNewFile.Url = filename;
    flciNewFile.Overwrite = true;
    List docs = web.Lists.GetByTitle(libraryName);
    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(flciNewFile);
    ctx.Load(uploadFile);
    ctx.ExecuteQuery();

}

Solution

  • You can use FileCreationInformation to create a new file and provide the contents via a FileStream. You can then add the file to the destination library. This should help you get around 2mb limit you are encountering with upload method you are using. Example below:

    FileCreationInformation newFile = new FileCreationInformation
    {
        Url = fileName,
        Overwrite = false,
        ContentStream = new FileStream(fileSourcePath, FileMode.Open)
    };
    
    var createdFile = list.RootFolder.Files.Add(newFile);
    ctx.Load(createdFile);
    ctx.ExecuteQuery();
    

    In the example the destination library is list you will need to get reference to this first. I can show you how to do this if required.