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.

To get around this, we’ll set the content for the FileCreationInformation object using the ContentStream property instead of the Content property.

Continue reading
Posted in CSOM, SharePoint | Tagged , | 8 Comments

Large Data and REST Services

When working with web services within SharePoint you sometimes need to be able to post and get large amounts of data through it. Instead of adding binding information to the web.config file(s), always a pain with SharePoint on deployment, depending on the context that the code is running under. It’s never a guarantee that you’ll have write permission to the necessary config files as you can never be certain the configured security will allow your write request.

A simpler method is to configure and add a SPWcfServiceSettings method for the web service that requires large amounts of data. The following few lines of code achieve this:

var spWcfServiceSettings = new SPWcfServiceSettings
{
    ReaderQuotasMaxStringContentLength = Int32.MaxValue,
    ReaderQuotasMaxArrayLength = Int32.MaxValue,
    ReaderQuotasMaxBytesPerRead = Int32.MaxValue,
    MaxReceivedMessageSize = Int32.MaxValue
};
 
SPWebService contentService = SPWebService.ContentService;
contentService.WcfServiceSettings["name_of_service_file.svc"] = spWcfServiceSettings;
contentService.Update(true);

Continue reading

Posted in SharePoint | Tagged , , | Leave a comment

Set Title Field on Document Upload

When uploading a file to a SharePoint library through code, you sometimes want to set item properties at the same time the document is created. To achieve this you simply populate a Hashtable object and pass this into the Add method of the SPFileCollection object:

public SPFile UploadDocument(SPDocumentLibrary library, string filename, string docTitle, byte[] fileBinary)
{
    SPFileCollection files = library.RootFolder.Files;
    System.Collections.Hashtable hash = new System.Collections.Hashtable();
    hash.Add("Title", docTitle);
 
    SPFile file = files.Add(filename, fileBinary, hash, false);
    return file;
}

There is an issue with the above method when trying to set the value for the Title field. After adding the file, you’ll find the Title column is left blank and SharePoint has not set its value. To have the value set, you have a couple of options.
Continue reading

Posted in SharePoint | Tagged | 3 Comments

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
Continue reading

Posted in CSOM, SharePoint | Tagged , | 2 Comments

Creating Entities from Dynamic SQL

When working with a LINQ to SQL model (dbml) and you try to add a custom stored procedure that uses dynamic SQL, you will likely see an error similar to the following:

The return type for the following stored procedure could not be detected. Set the return type for each stored procedure in the Properties window.

Visual Studio will detect dynamic SQL as the return type of a stored procedure when you return columns from temporary tables, for example.

How to resolve? Relatively easy actually. At the start of your stored procedure, add the following line:

SET FMTONLY OFF

Update the procedure to include the above command and back in Visual Studio, refresh your database connection before trying to add the procedure to the model. This time, Visual Studio is able to work out the return columns and their type. Once you’ve added it to the model, remove the command from the stored procedure in SQL.

What does setting FMTONLY to off do? Well, while this set to off, running the procedure only returns the metadata, no rows are returned. By doing this Visual Studio is able to determine the columns. Simple! 🙂

Posted in SQL, Visual Studio | Tagged , | Leave a comment