How to get the last x number of splistitems changed for a SPWeb

Steve Lineberry
  • How to get the last x number of splistitems changed for a SPWeb Steve Lineberry

    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;
    
    switch(list.BaseType)
    {
        case SPBaseType.DocumentLibrary:
            query.ViewAttributes = "Scope=\"Recursive\"";
            break;
        default:
    
            break;
    }
    
    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.

    Thanks.

  • 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..

  • 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.

Tags
caml-query spquery
Related questions and answers
  • = list; SPQuery query = new SPQuery(view); System.Text.StringBuilder sbViewFields = new System.Text.StringBuilder(); // all fields from view foreach (string field in view.ViewFields... using ListViewByQuery control. Here is how an SPQuery looks: &lt;Query> &lt;Where> &lt;Eq> &lt;FieldRef Name='Relaterat_x0020_till_x0020_avtal' LookupId='TRUE...So I did create on an SPWeb 20 custom lists and then a tasks list. For each of those 20 lists I've created 20 site lookup columns. I then created 20 Content Types based on Task content type

  • are the questions in the survey. The other fields contain other information such as author, last changed etc. How can I pick out the questions? I had thought that all non-questions would be hidden but this is not the case. Here is my code as it is at the moment. It returns about 16 items. The survey has 6 questions... // read question definitions string[] HandleTypes = new string[] { "Number", "DateTime", "Text", "Choice", "GridChoice", "Boolean" }; var query = from n in node.Descendants(ns+"Field") where (n.Attribute("Hidden") == null || n.Attribute("Hidden").Value.ToLower() == "true

  • as expected. When I try to add a new item to the list, the list form only shows the Title field from Item. Now, here's the weird part. If I add a second content type to the list (through the UI), the list form suddenly works correctly. This makes me think there's something wrong with my content type. Here's what I'm working with. Some names have been changed to protect the innocent. &lt;ContentType... xmlns:ows="Microsoft SharePoint" Title="Withdrawals" FolderCreation="FALSE" Direction="LTR" Url="Lists/Withdrawals" EnableContentTypes="TRUE" BaseType="0"> &lt;MetaData> &lt;ContentTypes>

  • I have created a recursive query that contains 2 multi user fields. After much thrashing, where I would add the and had my query return no results, I found that the FieldRef needed to include Nullable="TRUE" and Type="UserMulti". This works, in the query now returns the correct number of records, however the value in my 2 multi user fields is empty. &lt;View> &lt;Webs Scope="Recursive...; &lt;OrderBy> &lt;ProjectProperty Name="Title" Ascending="TRUE" /> &lt;FieldRef Name="Title" Ascending="TRUE" /> &lt;/OrderBy> &lt;/Query> &lt;/View> I'm trying

  • ) { SPList list = web.Lists["Web Part Gallery"]; // go through the items in reverse //for (int i = list.ItemCount – 1; i >= 0; i–) for(int i=list.ItemCount-1; i >=0...I have a webpart i build using WSpBuilder and it works OK. When i use Deploy from Visual Studio it installs the WSP sharepoint and add the webpart to the webpart gallery. But it seems the feature...(".")); // delete web parts that have been added if (webpartName == "myStoreComms") { list.Items[i].Delete(); break

  • , but that ends up rendering the actual anchor tag, as opposed to a clickable hyperlink: &lt;a href="http://www.something.com">www.something.com&lt;/a> So then I tried creating a XSL transform, since that is supposed to be the new and improved way of doing this. Unfortunately, I can't get that to work either. I am getting the same thing I got using the RenderPattern. I'm no XSL expert, so can anyone...Has anybody upgraded an old custom field type to use the new XSL transform in SharePoint 2010? I have a multi-column field type, and want the Display pattern to render the 2nd "column", which

  • I have a lookup column in multiple lists that points to a list of dates. These dates go in ascending order, and since the list posts are sequential, the older dates start to become irrelavent, forcing users that add new list items to scroll through to the bottom of the drop-down box to select the lastest date. As you can tell this can be very tedious, all I need is for the lookup column to sort by DESCENDING order, and I've tried fooling with the lookup list's view, but I think it disregards the view and only looks at the list items. Any thoughts?

  • ],[5,["\n \n ",["\n \n ","","",-1],[false,[],true],false,false,false],-1,false,false], Contact Selector Information about the control can br found on the InfoPath team blog: Using...Symptoms: You develop a browser-compatible InfoPath form with managed code for business logic. You use ContactSelector ActiveX control to look up and resolve users from the SharePoint site Users lists (UserInfo table in the Content database). You then validate list of resolved users and copy some of them to another node in the primary data source. The field displays copied contacts

  • I have successfully created a DVWP which queries all lists in my subsites and returns the correct columns using the following selectcommand: &lt;View> &lt;Webs Scope="Recursive">&lt...;FieldRef Name="StreamName" Ascending="TRUE" /> &lt;FieldRef Name="Title" Ascending="TRUE" /> &lt;/OrderBy> &lt;/Query> &lt;/View> One of the things i would now like to be able to do is group the results by the site that the item is located, much the same as a CWQP does group by . The query currently returns the webId, but I cannot see how to return the site

Data information