Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

SPQuery Object and looping code

  Asked By: Krista    Date: Feb 01    Category: Sharepoint    Views: 11926

I'm building an RSS feed application. I have two SPList types that I
use: Config and Feed. An arbitrary number of config lists exist in a
single SP install and each ListItem points to a Feed List. Each Feed
list can receive an arbitrary number of RSS feeds. The app loops
through all the config records (which are stored in a datatable in
memory) and updates the records if it is time (the config records are
updated periodically on a separate thread).

I have a function that is called after every Feed List update:

Private Sub CheckOldStuff(ByVal feedList As SPList, ByVal drCfg As
DataRow, ByVal listWeb As SPWeb)

This function creates an SPQuery that gets the data based on a CAML
query.

Dim queryValue As String = drCfg.Item
(ConfigFields.listID.ToString).ToString & "__" & drCfg.Item
(ConfigFields.ID.ToString).ToString

listQuery.Query = String.Format("<Query><OrderBy><FieldRef
Name='pubDate' />" & _
"</OrderBy><Where><Eq><FieldRef Name='sourceKey' />" & _
"<Value Type='Text'>{0}</Value></Eq></Where></Query>", queryValue)

I then take the list.getItems(listQuery) and put it into a
SPListItemCollection. I then start deleting items from the list
based on the item.ID values in the collection.

This works fine the first time through the loop, but after that, it
keeps leaving information in the collection that doesn't match the
Query syntax.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Laura Walker     Answered On: Feb 01

You cannot modify a collection  while iterating through it.
alternatively, you can store the IDs in an array. then iterate
through the array and delete each item.

foreach(SPListItem item  in itemColl)
arrID.Add(item.ID);

foreach(int itemID in arrID)
itemColl.DeleteItemByID(itemID);

 
Answer #2    Answered By: Alyssa Butler     Answered On: Feb 01

Actually, the problem was with the query. listQuery.Query cannot contain the “<Query></Query>” tag. Once that was removed, everything worked perfect.

 
Answer #3    Answered By: Katy Patton     Answered On: Feb 01

Are you deleting  the listitems from the database or the in memory
collection?

If the database, are you committing your deletes with an Update()?

 
Didn't find what you were looking for? Find more on SPQuery Object and looping code Or get search suggestion and latest updates.




Tagged: