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;

First we create a new SPWcfServiceSettings object and configure the reader properties to contain the maximum value possible. This is just an example and of course you could set this to any value, even reduce it so the default data sizes are prohibited. By setting these values we’re letting SharePoint know that this web service can process very large amounts of data.

Next up, we get the active content web service via the SPWebService object.

Using this object, access the indexer using the key value matching the name of your web service service host file and set its value to the new SPWcfServiceSettings object. If the item does not exist in the collection, it will be created.

Finally, call the Update method to persist the change.

Failing to do any of the above, or manually configure the bindings in the web.config files, will result in a 400 bad request error if you try to send over the default amount of 64KB through your web service.

This entry was posted in SharePoint and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve the maths problem shown below before posting: *