SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to query an SPList object using a CAML query (see code below).

SPQuery queryGetExistingTargetTask = new SPQuery();
queryGetExistingTargetTask.Query = string.Format(@"<Where><Eq><FieldRef Name=""TaskID""></FieldRef><Value Type=""Text"">{0}/Value></Eq></Where>", System.Security.SecurityElement.Escape(Convert.ToString(sourceTask["TaskID"])));
SPListItemCollection matchedTargetTasks = destinationList.GetItems(queryGetExistingTargetTask);

if (matchedTargetTasks.Count == 1) {...}

The SPList.GetITems call isn't throwing an error and it does return a SPListItemCollection Object. However, when I try to access any of the properties on the collection they throw an unhelpful error (see image below).

enter image description here

I'm fairly new to SharePoint development, so there is a good chance I am doing something very wrong here. Can you guys see anything obviously wrong with this code?

What I've tried so far:
- I confirmed that the destinationList (an SPList) is a valid instance of the correct list.
- Checked and re-checked the query in CAML builder

Note: This particular query should might (and will in my demo data) return no hits. However, I'd assume it would still return an object with a 0 value in the count property. Or am I mistaken about that?

share|improve this question
Your CAML query is missing an angle bracket before /Value. – James Love Jun 14 '11 at 20:39
Damnit! You are right! If you make that an answer I'll accept it. – JohnFx Jun 14 '11 at 20:39
Did that fix it? I'll pop it in as an answer so can be marked correct. – James Love Jun 14 '11 at 20:40
Added anyway :) – James Love Jun 14 '11 at 20:42
It did fix it. I feel like a dolt for missing something so obvious, but I appreciate that sometimes fresh perspective is the best debugging tool. – JohnFx Jun 15 '11 at 0:27
up vote 3 down vote accepted

Your CAML query is missing an angle bracket before /Value.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.