I'm trying to pull back the last x number of list items in all non-hidden lists for a given web.

My original code would go through all lists and run the following code:

SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name =\"Modified\" Ascending=\"FALSE\"/></OrderBy>";
query.RowLimit = (uint)this.ItemsToDisplay;

    case SPBaseType.DocumentLibrary:
        query.ViewAttributes = "Scope=\"Recursive\"";


SPListItemCollection listItems = list.GetItems(query);

effectively getting the last x items modified from each list and then I would put all of them into a List and sort, then taking the bottom x items.

This code was really slow (sometimes taking 20-30 seconds) so I decided to go down the route of using the ChangeLog.

Now i'm doing this code:

SPChangeQuery query = new SPChangeQuery(false, false);
query.Item = true;
query.Delete = false;
query.Add = true;
query.Update = true;
query.ChangeTokenStart = new SPChangeToken(SPChangeCollection.CollectionScope.Web, Web.ID, DateTime.Now.ToUniversalTime().AddDays(-14));
query.ChangeTokenEnd = new SPChangeToken(SPChangeCollection.CollectionScope.Web, Web.ID, DateTime.Now.ToUniversalTime());

SPChangeCollection changes = Web.GetChanges(query);

which works well in dev but has issues in prod. It turns out that in prod since we run MOSS and profiles are synced, I get 1000's of changes that are occuring in the user information list. So right now since the GetChanges method only pulls items in chunks of 1000, i have to use the lastchangetoken and call getchanges several times and then sort through 1000's of items just to get at the one's I want.

First question: Does anyone know how to exclude hidden lists or certain lists from a SPChangeQuery?

Second question: Is there a faster / better way of doing this? I thought about using Auditing to get the data since I have that turned on for all of our site collections but I haven't gone down that path yet.


  • 1
    Maybe the SPSiteDataQuery?? Has anyone tried that before? I assume that will security trim the data it returns? Mar 3, 2010 at 20:45
  • This guy just seems to be checking each list that is returned and determining whether to continue or not. blog.sharepoint2007.com.au/2009/06/23/… The downside to the changelog still seems to be the lack of security trimming. Dec 14, 2011 at 12:52

2 Answers 2


SPListItems is not included in auditlog (per design due to performance issues) so you cant use auditing if you are looking for list items.

I would probably go for SPSiteDataQuery (as you mention yourself) which usually performs best for site-wide queries.

Another option would be the GetListItems web service which often i surprisingly fast...

A third option would be ProcessBatchData using the Display method, if it can be tweaked to deliver the data you need.

  • Ok, thanks for the options. I'll try to give those a go. Thanks. Mar 4, 2010 at 12:50
  • I tried the SPSiteDataQuery and on paper it seemed like it would work. But unfortunately it is kind of buggy. If I specified the lists to return it would only return generic lists. If I specified to return document libraries, it would return ones that I didn't want. The GetListItems seemed to only be for a single list and that's not much different than my SPQuery original code. ProcessBatchData also seemed like I would have to aggregate data from multiple lists. I ended up going back to my original code and worked out a way to lazy load the data through ajax. Thanks. Mar 5, 2010 at 15:27

Could you do it at a more granular level using SPList.GetChanges()? Obviously you'd have to do some merging of resultsets, but you'd probably be working with fewer overall change items. I guess a big factor would be how many lists you have, as this would be tied to how many merge operations your code would have to do..

  • Yea, I thought about that but i'm not sure how much faster that is going to be over my original code that does the spquery. Thanks! Mar 3, 2010 at 23:28
  • I ended up having to provide an option in my webpart to switch to your suggestion because of the list view threshhold stuff in 2010. Dec 13, 2011 at 14:49
  • Your solution also ended up being faster in some cases but slower in others. I think because I couldn't figure out a good way to limit or sort the number of items returned. Dec 14, 2011 at 12:54

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.