SPQuery is not reusable.

So I’m writing some code to assemble some information about items in a list.

My original approach was this:

SPQuery query = new SPQuery();
query.Query = GenerateLodgedSubmissionsQuery(studentId, activityId);
SPListItemCollection submittedItems = submissionsList.GetItems(query);
response.SubmittedFiles = CreateFileDetailsList(submittedItems, false);
if (0 < submittedItems.Count)
{
	response.ActivityName = submittedItems[0]["ActivityName"].ToString();
	response.ActivityId = submittedItems[0]["ActivityId"].ToString();
}
query.Query = GenerateReturnedSubmissionsQuery(studentId, activityId);
SPListItemCollection returnedItems = submissionsList.GetItems(query);

The problem here was that the second call to GetListItems was returning the exact same results as the first call, despite having changed the Query property of the SPQuery object.

After much second guessing of my CAML query generation code and ensuring that the SPQuery.Query had infact changed I tried making the second GetListItems call with a new SPQuery object:

SPQuery query = new SPQuery();
query.Query = GenerateLodgedSubmissionsQuery(studentId, activityId);
SPListItemCollection submittedItems = submissionsList.GetItems(query);
response.SubmittedFiles = CreateFileDetailsList(submittedItems, false);
if (0 < submittedItems.Count)
{
	response.ActivityName = submittedItems[0]["ActivityName"].ToString();
	response.ActivityId = submittedItems[0]["ActivityId"].ToString();
}
SPQuery returnedItemsQuery = new SPQuery();
returnedItemsQuery.Query = GenerateReturnedSubmissionsQuery(studentId, activityId);
SPListItemCollection returnedItems = submissionsList.GetItems(returnedItemsQuery);

Now I finally saw the results that I was looking for.

Advertisements
This entry was posted in Development, SharePoint. Bookmark the permalink.

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