Increasing the Maximum Message Size

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

When you make a large request through the client object model, you might receive an error for a bad request. If you do, you can use the server object model to increase the maximum message size that is allowed by the Windows Communication Foundation (WCF) service, Client.svc, which supports the Microsoft SharePoint Foundation client object model. Another option is to use Distributed Authoring and Versioning (DAV) to make a PUT request.

The server object model provides two properties for increasing the maximum message size. You can set either the MaxReceivedMessageSize property on the SPWcfServiceSettings class, or the MaxReceivedMessageSize property on the SPClientRequestServiceSettings class. You can access these properties through the current SPWebService object in one of the following ways: SPWebService.ContentService.ClientRequestServiceSettings or SPWebService.ContentService.WcfServiceSettings["Client.svc"]. Depending on the value of the property on the SPClientRequestServiceSettings object, SharePoint Foundation uses, or does not use, the property on the SPWcfServiceSettings object, as follows:

  • If SPWebService.ContentService.ClientRequestServiceSettings.MaxReceivedMessageSize is greater than zero, SharePoint Foundation uses this value as the property setting.

  • If SPWebService.ContentService.ClientRequestServiceSettings.MaxReceivedMessageSize equals zero, SharePoint Foundation uses the default value as the setting.

  • If SPWebService.ContentService.ClientRequestServiceSettings.MaxReceivedMessageSize equals -1, SharePoint Foundation uses the SPWcfServiceSettings value as the setting if it is defined, but if it is not defined, SharePoint Foundation uses 64KB as the setting.

Increasing the WCF MaxReceivedMessageSize

The following example shows how to use the MaxReceivedMessageSize property of the SPClientRequestServiceSettings object to modify the message size setting.

Public Shared Sub IncreaseMaxReceivedMessageSize()
    Dim contentService As SPWebService = SPWebService.ContentService
    contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = 10485760  ' 10MB
    contentService.Update()
End Sub
public static void IncreaseMaxReceivedMessageSize()
{        
    SPWebService contentService = SPWebService.ContentService;
    contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = 10485760;  // 10MB
    contentService.Update();
}

The next example shows how to use the MaxReceivedMessageSize property of the SPWcfServiceSettings object to modify the setting. The example must first set the property on SPClientRequestServiceSettings to -1.

Public Shared Sub IncreaseMaxReceivedMessageSize()
    Dim contentService As SPWebService = SPWebService.ContentService
    
    ' Must set this to -1, else, the MaxReceivedMessageSize value for
    ' SPWebService.ContentService.WcfServiceSettings["client.svc"] will not be used.
    contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = -1
    
    ' SPWcfServiceSettings has other Properties that you can set.
    Dim csomWcfSettings As New SPWcfServiceSettings()
    csomWcfSettings.MaxReceivedMessageSize = 10485760  ' 10MB
    contentService.WcfServiceSettings("client.svc") = csomWcfSettings
    
    contentService.Update()
End Sub
public static void IncreaseMaxReceivedMessageSize ()
{
    SPWebService contentService = SPWebService.ContentService;

    /* Must set this to -1, else, the MaxReceivedMessageSize value for
    SPWebService.ContentService.WcfServiceSettings["client.svc"] will not be used.*/
    contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = -1;

    // SPWcfServiceSettings has other Properties that you can set.
    SPWcfServiceSettings csomWcfSettings = new SPWcfServiceSettings();
    csomWcfSettings.MaxReceivedMessageSize = 10485760; // 10MB
    contentService.WcfServiceSettings["client.svc"] = csomWcfSettings;

    contentService.Update();
}

Using DAV to Make the Request

The following example illustrates how to use DAV to make the request.

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp)

Dim request As HttpWebRequest = DirectCast(WebRequestCreator.ClientHttp.Create(New Uri("http://Server/MyFile.txt")), HttpWebRequest)
request.Method = "PUT"

' Make an asynchronous call for the request stream. The callback method will be called on a background thread. 
Dim asyncResult As IAsyncResult = request.BeginGetRequestStream(New AsyncCallback(RequestStreamCallback), request)

Private Sub RequestStreamCallback(ByVal ar As IAsyncResult)
    Dim request As HttpWebRequest = TryCast(ar.AsyncState, HttpWebRequest)
    Dim requestStream As Stream = request.EndGetRequestStream(ar)
    Dim streamWriter As New StreamWriter(requestStream)
    
    ' Write your file here.
    streamWriter.Write("Hello World!")
    
    ' Close the stream.
    streamWriter.Close()
    
    ' Make an asynchronous call for the response. The callback method will be called on a background thread. 
    
    request.BeginGetResponse(New AsyncCallback(ResponseCallback), request)
End Sub

Private Sub ResponseCallback(ByVal ar As IAsyncResult)
    Dim request As HttpWebRequest = TryCast(ar.AsyncState, HttpWebRequest)
    Dim response As WebResponse = Nothing
    Try
        response = request.EndGetResponse(ar)
    Catch generatedExceptionName As WebException
    Catch generatedExceptionName As SecurityException
        
        ' You may need to analyze the response to see if it succeeded.
    End Try
End Sub
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

HttpWebRequest request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri("http://Server/MyFile.txt"));
request.Method = "PUT";

/* Make an asynchronous call for the request stream. The callback method will be called on a background thread. */ 
IAsyncResult asyncResult = request.BeginGetRequestStream(new AsyncCallback(RequestStreamCallback), request);

private void RequestStreamCallback(IAsyncResult ar)
{
    HttpWebRequest request = ar.AsyncState as HttpWebRequest;            
    Stream requestStream = request.EndGetRequestStream(ar);
    StreamWriter streamWriter = new StreamWriter(requestStream);

    // Write your file here.
    streamWriter.Write("Hello World!");            

    // Close the stream.
    streamWriter.Close();

    /* Make an asynchronous call for the response. The callback method will be called on a background thread. */
    request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
}       

private void ResponseCallback(IAsyncResult ar)
{
    HttpWebRequest request = ar.AsyncState as HttpWebRequest;
    WebResponse response = null;
    try
    {
        response = request.EndGetResponse(ar);
    }
    catch (WebException)
    {}
    catch (SecurityException)
    {}

    // You may need to analyze the response to see if it succeeded.
}

In a Microsoft Silverlight application, you might also have to set your client access policy XML file, as seen in the following example. You can put this file in %inetpub%\wwwroot\wss\VirtualDirectories\80.

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <!--Enables Silverlight 3 all methods functionality-->
    <policy>
      <allow-from http-methods="*">"
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
    <!--Enables Silverlight 2 clients to continue to work normally -->
    <policy>
      <allow-from >
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

For more information about Silverlight and HTTP communication, see HTTP Communication and Security with Silverlight.

See Also

Concepts

Data Retrieval Overview

SharePoint 2010 Client Object Model Guidelines

Common Programming Tasks in the Managed Client Object Model

Other Resources

Using the SharePoint Foundation 2010 Managed Client Object Model

Client Object Model Resource Center