ContentIterator – Curious Behaviour, and Infinite looping

I like the ContentIterator control – it’s a nice way of being able to process a lot of items in SharePoint. However, sometimes it’s behaviour is … strange.

I had been using it to loop over some (but not all) the items in a list. Some changes meant that I was now going to loop over all the items. I had a list with 500 items that I was testing with. My code was:

ContentIterator ci = new ContentIterator("example");
SPQuery qry = new SPQuery();
qry.Query = ContentIterator.ItemEnumerationOrderByNVPField;
ci.ProcessListItems(list,qry, ProcessItem, ProcessItemError);

This would process the first 200 items, over and over. It never reached an end. Hmm.

I was puzzled by this, so I put my code in a console app, and then tried querying the list (to check the number of items I was getting):

ContentIterator ci = new ContentIterator("Replicate");
SPQuery qry = new SPQuery();
qry.Query = ContentIterator.ItemEnumerationOrderByNVPField;
Console.WriteLine(qry.Query);
SPListItemCollection items = list.GetItems(qry);
Console.WriteLine("Item: {0}", items.Count);
ci.ProcessListItems(list,qry, ProcessItem, ProcessItemError);

Interestingly, this worked. I can’t see why though. I presume that using the SPQuery changes the query, or somehow affects the list. I’m really not sure.

Now, the observant might well note that actually, a better overload to use on the ContentIterator.ProcessListItems would be the one that doesn’t need a query:

ci.ProcessListItems(list, ProcessItem, ProcessItemError);

This made me wonder – what was different about this? Well, I used Reflector and looked within the class, and found that it was using a different OrderBy clause. When I changed my code to…

ContentIterator ci = new ContentIterator("example");
SPQuery qry = new SPQuery();
qry.Query = ContentIterator.ItemEnumerationOrderByID;
ci.ProcessListItems(list,qry, ProcessItem, ProcessItemError);

… it worked.

So, if your ContentIterator starts to run forever:

  • Check you’re using the best overload for what you want. If you don’t need to use an SPQuery, don’t!
  • Check your Order By clause – you may need to use a different one.
Advertisements
ContentIterator – Curious Behaviour, and Infinite looping

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s