使用 CAML 查询 SharePoint 2010 列表时在 .NET C# MVC 3 中遇到 ServerException

发布于 2024-11-06 22:38:40 字数 917 浏览 0 评论 0 原文

我正在尝试从 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 都会导致已经声明的异常,或者不会影响“项目”的结果集(如所有项目都已返回)。

感谢您提供的任何帮助。

I am attempting to fetch a single item from a SharePoint 2010 list. I am using .NET C# MVC 3 framework.

I am receiving the runtime error "ServerException, Exception from HRESULT: 0x80131904" from the ExecuteQuery() method in the following block of code.

 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();

If an empty CAML query is passed, "items" is populated will all elements of the list, so the basic connection is working. Introducing any CAML to .ViewXML has resulted in either the already stated exception or has not affected the resultset of "items" (as in, all items have been returned).

Any help that you can provide is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

公布 2024-11-13 22:38:40

我遇到了类似的问题,我试图查询一个列表并返回多选查找“haystack”中包含“needle”值的所有项目:

<Where>
  <Contains><FieldRef Name='Environments' /><Value Type='Lookup'>DV</Value></Contains>
</Where>

最初我将 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":

<Where>
  <Contains><FieldRef Name='Environments' /><Value Type='Lookup'>DV</Value></Contains>
</Where>

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.

落花随流水 2024-11-13 22:38:40

试试这个:

<FieldRef Name="UniqueId" LookupId="TRUE"/><Value Type="Lookup">

而不是

<FieldRef Name='UniqueId'/><Value Type='Lookup'>

and yes,caml 区分大小写

Try this:

<FieldRef Name="UniqueId" LookupId="TRUE"/><Value Type="Lookup">

instead of

<FieldRef Name='UniqueId'/><Value Type='Lookup'>

and yeah, caml is case sensitive

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文