使用 CAML 查询 SharePoint 2010 列表时在 .NET C# MVC 3 中遇到 ServerException
我正在尝试从 SharePoint 2010 列表中获取单个项目。我正在使用 .NET C# MVC 3 框架。
我从以下代码块中的 ExecuteQuery() 方法收到运行时错误“ServerException,来自 HRESULT 的异常:0x80131904”。
ClientContext spContext = new ClientContext(Settings.Default.SharePointSite + Settings.Default.SharePointWeb);
spContext.Credentials = new NetworkCredential("[REMOVED]", "[REMOVED]", "[REMOVED]");
var list = spContext.Web.Lists.GetByTitle("ExampleList");
var camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='UniqueId'/>" +
"<Value Type='Lookup'>" + id.ToString() + "</Value></Eq></Where></Query></View>";
var items = list.GetItems(camlQuery);
spContext.Load(items);
spContext.ExecuteQuery();
如果传递空的 CAML 查询,则列表的所有元素都会填充“items”,因此基本连接正常工作。将任何 CAML 引入 .ViewXML 都会导致已经声明的异常,或者不会影响“项目”的结果集(如所有项目都已返回)。
感谢您提供的任何帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题,我试图查询一个列表并返回多选查找“haystack”中包含“needle”值的所有项目:
最初我将 value 元素的 Type 属性设置为“Text”(如果未指定类型,这也是默认值),并且收到“发生异常。(来自 HRESULT 的异常:0x80020009 (DISP_E_EXCEPTION))”错误。
然后我添加了正确的 Type 属性,并错误地将 LookupId 属性添加到 FieldRef 元素,这给了我零结果,但没有错误。
事实证明,原因非常合理,我在 http://sharepointmagazine.net/articles/writing-caml-queries-for-retriving-list-items-from-a-sharepoint-list 。基本上,通过指定 Type='Lookup',您将根据查找字段的显示值进行评估,但向前一步并在另一个元素上说 LookupId='TRUE' 意味着“评估外部项目 ID,而不是评估”显示值”。
因此,如果您使用 CAML 查询查找字段,则该值应始终为“Lookup”类型,但是否使用 LookupId 将取决于您是否要比较查找的键或值。
I was having a similar issue, where I was trying to query a list and return all items where a "needle" value was contained in a multi-select lookup "haystack":
Originally I had the value element's Type attribute as "Text" (which is also the default if no Type is specified), and was getting the "Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))" error.
Then I added the correct Type attribute, and mistakenly added the LookupId attribute to the FieldRef element, which gave me zero results but no error.
The reason it turns out is pretty sensible, and I found described on http://sharepointmagazine.net/articles/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list . Basically, by specifying Type='Lookup', you're evaluating against the display value of the lookup field, but going one step past and saying LookupId='TRUE' on the other element means "evaluate on the foreign item ID, rather than the display value".
So if you're using CAML to query a lookup field, the value should always be of type 'Lookup', but whether or not you use LookupId will depend on whether you want to compare the key or value of the lookup.
试试这个:
而不是
and yes,caml 区分大小写
Try this:
instead of
and yeah, caml is case sensitive