Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a query that is working when using any CAML query builder, when in using it in c# for a SharePoint web part, it always returns no results.

using (SPWeb ThisWeb = ThisSite.OpenWeb())
{
  IList<Tweet> Tweets = GetTweets(item["Mode"].ToString(), item["String"].ToString(), LastTweet, ThisSite);
  SPList ThisList = ThisWeb.Lists.TryGetList(Variables.TwitterTweetList);

  foreach (var tweet in Tweets)
  {
    SPListItemCollection itms = ThisList.GetItems(new SPQuery() { Query = @"<Where>" + 
        "<Eq>" +
        "<FieldRef Name=""Title"" />" +
        "<Value type=""Text"">" + tweet.tweetID.ToString() + "</Value>" +
        "</Eq>" +
        "</Where>"
  });

  if (itms.Count == 0)
  {
    // add the tweet to 'ThisList'
  }
}

}

Stepping into the code, you'll see tweet.tweetID.ToString() is "337958304577892353"

When running this code, it return ZERO items.

When running the query in U2U builder or any other CAML query it returns 1 (2, 3, 4, etc if the code is ran more then once).

Query ran in U2U builder:

<Query>
    <Where>
        <Eq>
            <FieldRef Name="Title" />
            <Value type="Text">"337958304577892353"</Value>
        </Eq>
    </Where>
</Query>

(Yes, when doing CAML in SharePoint, you drop the tags... I have 3 other queries that work just fine.. it's just this one.)

share|improve this question
The only difference I see between your code and the U2U query is you have the tweet id inside quotes in the U2U query. It shouldn't matter, but it might be worth a try. – Robbert May 24 at 22:15

2 Answers

As a corollary to what Robbert said, have you tried declaring the tweet.tweetID.ToString() as its own variable? If nothing else, you can look at it in debug and confirm that it's passing the correct information to the SPList object.

foreach(var tweet in Tweets)
{
    string tweetID = tweet.tweetID.ToString();
    SPQuery query = new Query();
    query.Query = string.format(queryformat, tweetID);
    SPListItemCollection tweetItems = list.GetItems(query);
}

...and so on. My experience is that SharePoint will happily work with strings that happen to be a long row of digits. It's possible, however, that tweetID.ToString() isn't returning what you think it is, and as such a cast ( (string)tweetID or ((long)tweetID).ToString()) is necessary instead.

share|improve this answer
Yes, I have tried that with the same result. :( – RyanFromIT May 29 at 16:55
It does pass the proper value – RyanFromIT May 29 at 16:56
Have you tried replacing Eq with Contains? – John Craven May 29 at 18:34

Well may be you sorted out this issue but may thats because of a simple reason that you are using double quotation within double quotation. like in the following CAML query.

    "<Eq>" +
    "<FieldRef Name='Title' />" +
    "<Value type='Text'>" + tweet.tweetID.ToString() + "</Value>" +
    "</Eq>" +
    "</Where>"
share|improve this answer
When you are using a string with the @ in front, you need to user two double quotes to.. @"this is how add you ""quotes"" in this string" – RyanFromIT May 29 at 16:54

Your Answer

 
discard

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.