简体   繁体   English

SharePoint CAML查询可在构建器中使用,而不能在代码中使用

[英]SharePoint CAML query works in builder but not in code

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. 我有一个在使用任何CAML查询生成器时都有效的查询,在SharePoint Web部件的c#中使用它时,它始终不返回任何结果。

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" 进入代码,您将看到tweet.tweetID.ToString()为“ 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). 在U2U构建器或任何其他CAML查询中运行查询时,它返回1(如果代码多次运行则返回2、3、4,等等)。

Query ran in U2U builder: 在U2U构建器中运行查询:

<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.) (是的,当在SharePoint中进行CAML时,您删除了这些标记……我还有其他3个可以正常工作的查询。就是这样。)

As a corollary to what Robbert said, have you tried declaring the tweet.tweetID.ToString() as its own variable? 作为罗伯特所说的推论,您是否尝试过将tweet.tweetID.ToString()声明为其自己的变量? If nothing else, you can look at it in debug and confirm that it's passing the correct information to the SPList object. 如果没有其他问题,您可以在调试中查看它,并确认它正在将正确的信息传递给SPList对象。

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. 我的经验是,SharePoint将很高兴使用恰好是一长串数字的字符串。 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. 但是, tweetID.ToString()可能不会返回您认为的样子,因此,必须使用tweetID.ToString() (string)tweetID((long)tweetID).ToString() )。

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. 就像下面的CAML查询一样。

    "<Eq>" +
    "<FieldRef Name='Title' />" +
    "<Value type='Text'>" + tweet.tweetID.ToString() + "</Value>" +
    "</Eq>" +
    "</Where>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM