Archive for CSOM

Sending large data with CSOM

As a follow up to my recent post showing how to create or overwrite a document using the CSOM, I’m now going to show how to upload a file containing large amounts of data. To see the original post, click here.

Previously, when saving the binary data, I demonstrated the functionality with the following code:

public void SaveFile(Microsoft.SharePoint.Client.ClientContext context, string folderRelativeUrl, string relativeItemUrl, byte[] fileData)
{
    var fci = new FileCreationInformation
    {
        Url = relativeItemUrl,
        Content = fileData,
        Overwrite = true
    };
 
    Microsoft.SharePoint.Client.Folder folder = context.Web.GetFolderByServerRelativeUrl(folderRelativeUrl);
    Microsoft.SharePoint.Client.FileCollection files = folder.Files;
    Microsoft.SharePoint.Client.File file = files.Add(fci);
 
    context.Load(files);
    context.Load(file);
    context.ExecuteQuery();
}

The above code, by default, will be restricts to transferring files that are no more than 2091752 bytes in size, or 2MB.
Read more

Cannot invoke HTTP DAV request

When using the Client-Side Object Model (CSOM) for SharePoint and you’re trying to create\overwrite a file using binary data, you’d naturally try and use the SaveBinaryDirect method of the File class:

public void SaveFile(Microsoft.SharePoint.Client.ClientContext context, Microsoft.SharePoint.Client.File file,
                     string relativeItemUrl, byte[] fileData)
{
    using (Stream ms = new MemoryStream(fileData))
    {
        File.SaveBinaryDirect(context, relativeItemUrl, ms, true);
    }
}

When using a MemoryStream the following error will more than likely be raised:

Cannot invoke HTTP DAV request. There is a pending query
Read more

Follow

Get every new post delivered to your Inbox

Join other followers: