SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is ClientContext thread-safe? I'm using Parallel framework to parallelize some work that creates new items on the list. Example:

using(var ctx = new ClientContext(siteUrl))
    var list = ctx.Web.GetList('/List/somelist')
    var subList = ctx.Web.GetList('/List/sublist')
    var myExternalItems = collection of items

    Parallel.ForEach(myExternalItems , new ParallelOptions() { MaxDegreeOfParallelism = 4 }, myitem =>
        var item = list.AddItem(new ListItemCretionInformation());

        item["Title"] = myitem.Title; //just an example


        foreach(var sitem in myitem.SubItems)
            var subitem = subList.AddItem(new ListItemCretionInformation());

            subitem["Title"] = sitem.Title;


It fails with:

An unhandled exception of type 'System.StackOverflowException' occurred in Microsoft.SharePoint.Client.Runtime.dll

And in Call Stack:

The maximum number of stack frames supported by Visual Studio has been exceeded.

It works if I replace Parallel.ForEach with a normal sequent foreach loop. It's not an error in field values or names, but something related to ClientContext's web request I guess. So is Client Object Model thread safe?

share|improve this question
up vote 5 down vote accepted

As the MSDN documentation states, instance members of ClientContext are not guaranteed to be thread-safe:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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