开发者

Experiencing ServerException in .NET C# MVC 3 when querying SharePoint 2010 list with CAML

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);
spConte开发者_开发百科xt.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.


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.


Try this:

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

instead of

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

and yeah, caml is case sensitive

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜