22

I have web service that point to sharepoint 2013 Office 365. I use the client object model. I am trying to update the xml file which stores 4 attachments in it. When I do this when I have large binary data in the xml file I get the following error :

Message

The request message is too big. The server does not allow messages larger than 2097152 bytes.

I realize I am probably going to have to seperate the attachments from the xml file but currently my infopath form stores them there. Is there a way I can increase the request length or maybe chunk up saving or something. I really just modifying one node and it won't work unless I update the xml. Thanks . Code Below.

My Code:

ListItem docReq = GetDocRequestLight(docRequestID, businessID);
string fPath = (string)docReq["FileRef"];
using (FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fPath))
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(fInfo.Stream);
    XmlNamespaceManager xmlNameSpaceMgr = new XmlNamespaceManager(xmlDoc.NameTable);
    xmlNameSpaceMgr.AddNamespace("my", DocReqXmlNameSpace);

    // Get Parent Node
    XmlNode node = xmlDoc.SelectSingleNode(GetXPathFromItemKey(velmaKey), xmlNameSpaceMgr);

    DateTime outDate;
    bool outBool;
    if (DateTime.TryParse(newValue, out outDate))
        node.InnerText = outDate.ToString("yyyy-MM-dd");
    if (Boolean.TryParse(newValue, out outBool))
        node.InnerText = newValue;

    // Update Statuses
    XmlNode statusIDNode = xmlDoc.SelectSingleNode(DocReqStatusIDFieldXPath, xmlNameSpaceMgr);
    statusIDNode.InnerText = updatedStatus.ID.ToString();
    XmlNode statusNode = xmlDoc.SelectSingleNode(DocReqStatusFieldXPath, xmlNameSpaceMgr);
    statusNode.InnerText = updatedStatus.Name.ToString();

    // Save File
    docReq.File.SaveBinary(new FileSaveBinaryInformation()
    {
        Content =   Encoding.UTF8.GetBytes(xmlDoc.OuterXml),
    });

    ctx.ExecuteQuery();
helb
  • 7,609
  • 8
  • 36
  • 58
Carter
  • 231
  • 1
  • 2
  • 6
  • I avoided the size issue by using the [File Save Binary Direct](https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/upload-large-files-sample-app-for-sharepoint), here is a [good example](https://stackoverflow.com/questions/17057074/how-to-download-upload-files-from-to-sharepoint-2013-using-csom) of it being used successfully. – Roskow Jan 22 '19 at 15:53

4 Answers4

28

SharePoint has its own limits for CSOM. Unfortunately, these limits cannot be configured in Central Administration and also cannot be set using CSOM for obvious reasons.

When googling for the issue, mostly a solution is given by setting the ClientRequestServiceSettings.MaxReceivedMessageSize property to the desired size.

Call the following PowerShell script from SharePoint Management Shell :

$ws = [Microsoft.SharePoint.Administration.SPWebService]::ContentService 
$ws.ClientRequestServiceSettings.MaxReceivedMessageSize = 209715200 
$ws.Update()

This will set the limit to 200 MB.

However, in SharePoint 2013 Microsoft apparently added another configuration setting to also limit the amount of data which the server shall process from a CSOM request (Why anyone would configure this one differently is beyond me...). After reading a very, very long SharePoint Log file and crawling through some disassembled SharePoint server code, I found that this parameter can be set via the property ClientRequestServiceSettings.MaxParseMessageSize.

We are now using the following script with SharePoint 2013 and it works great:

$ws = [Microsoft.SharePoint.Administration.SPWebService]::ContentService 
$ws.ClientRequestServiceSettings.MaxReceivedMessageSize = 209715200 
$ws.ClientRequestServiceSettings.MaxParseMessageSize = 209715200 
$ws.Update()  

Hope that saves some people a headache!

helb
  • 7,609
  • 8
  • 36
  • 58
  • I needed to stop the Sharepoint timer service, enter the above script, and start the service again, to get this working. – Erwin Feb 27 '15 at 13:14
  • @Erwin I can't recall having to do that. But I guess this is just the seemingly random beavior one has to accept when working with SharePoint. Anyways, thanks for the feedback. – helb Feb 27 '15 at 13:17
  • Thanks! You saved me hours of digging! :) – Shahab May 27 '15 at 15:28
  • Thank you!!! not sure why the MaxParseMessageSize is not widely documented ... hats off to you for finding this fix.... it worked perfectly for me... – nivensookharan Oct 22 '15 at 10:13
  • Just for anyone who needs to revert this change, in our case, default values for both properties in SPS2013 is 0. – Dani Rodríguez Dec 11 '20 at 08:40
9

I have just encountered this issue in SharePoint 2013 and after looking through the docs it states:

Content property on the FileCreationInformation class. Maximum file size that can be uploaded is 2 MB. Time-out occurs after 30 minutes. Use to upload files that are less than 2 MB only.

However if you set the ContentStream property, you don't hit the same document size limit. The documentation states:

ContentStream property on the FileCreationInformation class. No file size limits. Time-out occurs after 30 minutes. Recommended for: - SharePoint Server 2013. - SharePoint Online when the file is smaller than 10 MB.

There are some other options detailed within the documentation, but hopefully this will help other people that have encountered this very same problem.

Corporalis
  • 1,032
  • 1
  • 9
  • 17
6

earlier i was using this approach and getting issues for 2MB file

var fileCreationInfo = new FileCreationInformation
                    {

                        Content = System.IO.File.ReadAllBytes(fileName),
                        Overwrite = true,
                        Url = Path.GetFileName(fileName)
                   };

I tried to use powershell script, as mentioned on many post, to increase the limit of file size but no luck, then i followed below approach and able to upload file size 100MB.

FileStream fs = new FileStream(fileName, FileMode.Open);
                FileCreationInformation fileCreationInfo = new FileCreationInformation();
                fileCreationInfo.ContentStream = fs;
                fileCreationInfo.Url = Path.GetFileName(fileName);
                fileCreationInfo.Overwrite = true;
Pradip Tilala
  • 1,715
  • 16
  • 24
5

use .ContentStream instead of .Content

 //newFile.Content = System.IO.File.ReadAllBytes(file);


newFile.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(file));
Xtremexploit
  • 319
  • 4
  • 7