2

Using SharePoint Online (2013) we are encountering this issue occasionally when creating folders in batch.

Exception calling "ExecuteQuery" with "0" argument(s): "The request message is too big. The server does not allow messages larger than 2097152 bytes."

Is there a way to determine the message size prior to doing $Context.ExecuteQuery() to prevent this?

1 Answer 1

4

Here's a example for JS - if you are using C# I can't promise that you can access the same functionality to measure the request.

JS

I found the piece that is creating the request - you can get the string via ctx.get_pendingRequest().$2x_0().toString(). Afterwards you simply try to get the bytelength via JS (reference).

Working example:

var stringToByte = function(str){
  return encodeURI(str).split(/%(?:u[0-9A-F]{2})?[0-9A-F]{2}|./).length - 1;
}

var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var webs = web.get_webs();

ctx.load(webs);

alert('Current size is ' + stringToByte(ctx.get_pendingRequest().$2x_0().toString()) + 'bytes');

ctx.load(webs, 'Include(AllProperties, Title)');

alert('Current size is ' + stringToByte(ctx.get_pendingRequest().$2x_0().toString()) + 'bytes');

C#

I managed to get at least some vague Information via the GarbageCollector. Here's the full example:

using Microsoft.SharePoint.Client;
using System;
using System.Linq;

namespace Tester
{
    internal class Program
    {   
        private static void Main(string[] args)
        {
            try
            {
                var measure = new GcMeasurer();
                ClientContext ctx = new ClientContext("https://sharepoint");

                for (var i = 0; i < 1500; i++)
                {
                    measure.StartMonitor();
                    ctx.Web.AllProperties["_Tester" + i] = RandomString(1000);
                    measure.EndMonitor();
                    Console.WriteLine(BytesToString(measure.GetSize()));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error:");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            Console.Read();
        }

        private static readonly Random Random = new Random();

        public static string RandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[Random.Next(s.Length)]).ToArray());
        }

        private static String BytesToString(long byteCount)
        {
            string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
            if (byteCount == 0)
                return "0" + suf[0];
            long bytes = Math.Abs(byteCount);
            int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
            double num = Math.Round(bytes / Math.Pow(1024, place), 1);
            return (Math.Sign(byteCount) * num).ToString() + suf[place];
        }
    }

    internal class GcMeasurer
    {
        private long CurrentSize { get; set; }
        private long MonitoredSize { get; set; }

        internal GcMeasurer()
        {
            CurrentSize = 0;
        }

        internal void StartMonitor()
        {
            MonitoredSize = GC.GetTotalMemory(true);
        }

        internal void EndMonitor()
        {
            CurrentSize += GC.GetTotalMemory(true) - MonitoredSize;
        }

        internal long GetSize()
        {
            return CurrentSize;
        }
    }
}
4
  • It looks like that isn't exposed in the CSOM for .NET/PowerShell. I'll just need to limit them further upstream and hope I don't hit it.
    – Taylor
    Commented Sep 7, 2016 at 17:40
  • That's a long way to go to get it. I'll see if I can incorporate something similar into my PowerShell code, but as far as I can tell nothing is exposed in .NET for this so the GC route may be needed. I'll see if my TAM can help, but this is the most useful information I've gotten on this. Thanks
    – Taylor
    Commented Sep 26, 2016 at 18:31
  • What is the solution for powershell if we face same error?? Commented Mar 19, 2019 at 14:19
  • Other than the one already written? Just keep your batches small i.e. don't write 1000 Elements, instead write 10 times 100. If that still causes the error change the ratio even more.
    – Mx.
    Commented Mar 20, 2019 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.