Add Items to SPListItemCollection


  • Hi,

    i have a loop to get different items with a spquery in my collection, at the end of the loop i would like to sum these items to one big collection of all items...

    code example:

    SPList list = web.Lists["Sitzungen"];
                        SPQuery q = new SPQuery();
                        SPQuery sitzungen = new SPQuery();
                        sitzungen.Query = "<Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where><OrderBy><FieldRef Name='Datum' Ascending='False'/></OrderBy>";
                        SPListItemCollection sitzcol = list.GetItems(sitzungen);
                        SPListItemCollection alleBeschlüsse = null;
                        foreach (SPListItem c in sitzcol)
                            q.Query = "<Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>Beschluss</Value></Eq></Where>";
                            q.ViewAttributes = "Scope=\"Recursive\"";
                            if (c.Folder != null)
                                q.Folder = c.Folder;
                            SPListItemCollection col = list.GetItems(q);
    please dont look @query text, cause they are nonsense is just for a test program...
    at the end i get the items i want in the SpistItemCollection "col" now i want sum in every loop the items of these collection to get all my items...

    thanks, markus
    Wednesday, November 25, 2009 1:02 PM

All replies

  • I haven't understood your requirements.
    You want to collect SPListItem's to some collection on some condition?
    You can't programmatic-ally add SPListItems to SPListItemCollection!
    You can create List<SPListItem> and then collect all your required reference to SPListItems there.
    Wednesday, November 25, 2009 1:15 PM
  • but maybe its too slow, for my problem...

    let me describe my problem better and sorry for my bad english :-/

    i have one list with about 5000 folder items. in these folder items are subfolders, other items and items of the content type "Beschluss". in these subfolder are also subfolders, other items and items of the content type "Beschluss", etc.

    with my query i can get all "Beschluss" items, cause of the Property ViewAttributes with the Scope, i can search in all subfolders, etc.

    But my query also needs a validition of a field in the rootfolder of all these solution at the moment:

    get all "Beschluss" items, loop them and check the validition of the column in the rootfolder of each "Beschluss" item, and then i have a collection with my real results...

    this is very slow i have about 16000 "Beschluss" items, the query is very very fast but then i must loop all "Beschluss" items and look at one column value in the rootfolder of each item to get my result...

    i would like to have only one query for all these "Beschluss" items, cause sometimes the result is about 10 "Beschluss" items, and i loop 16 000....

    i hope someone understand my problem :P
    Wednesday, November 25, 2009 1:25 PM
  • i also ask myself of this possiblity:

    get all my folders with one query to check the column and then only make a query on this result collection to get my "Beschluss" items, but i dont think its possible, or?

    Wednesday, November 25, 2009 1:47 PM
  • Yes - Folder is a just ContentType of Item, you could check inside <Eq> ContentType and it's name or ID as you wish.
    Wednesday, November 25, 2009 1:50 PM
  • yes i know, but then i get only my folders. but for my output i need the "Beschluss" items

    so i must build another query on the folder result collection to check the content type "Beschluss"

    and this is afaik not possible

    Wednesday, November 25, 2009 1:55 PM
  • You can use Datatable dt = col.GetDataTable();

    and for every consecutive loop you can use


    and then you can query the final datatable.

    Hope it works

    • Proposed as answer by Vijander Monday, November 30, 2009 4:37 AM
    Thursday, November 26, 2009 4:26 AM