3

What I like in SharePoint Client Object model is that you can make batch queries. What I cannot find is if it's possible to get related items (linked by lookup field) in the same query?

An example would be: - A list Partners with fields: Title, Address, ZIP, City and Country (lookup to Countries) - A list Countries with fields: Title, Abbreviation, Continent (Lookup to Continents)

If I'd want to load also a Continent for each partner (cannot be done through related fields in Lookup column), I'd need to first load the list of partners and then afterwards an additional query to load countries for each of the partner. Is it possible to do this in one query?

Currently this goes something like this (unoptimized, just for demo purpose):

ClientContext ctx = new ClientContext("http://mydemospsite");
List lst = ctx.Web.Lists.GetByTitle("Partners");
List lstCountries = ctx.Web.Lists.GetByTitle("Countries");
CamlQuery qry = new CamlQuery() { ViewXml = "" };

ListItemCollection itms = lst.GetItems(qry);

ctx.Load(itms);

ctx.ExecuteQueryAsync((o, e) => 
{
   foreach(ListItem itm in itms) 
   {
       ListItem itmCountry = lstCountries.GetItemById(((FieldLookupValue)itm["Country"]).LookupId);
       //Do whatever, link the item,...
   }
   ctx.ExecuteQueryAsync(null, null);
}, 
(o, e) => 
{ 
    Dispatcher.BeginInvoke(() => 
    { 
        MessageBox.Show("Error"); 
    }); 
});

The sample above is poor and serves only as an example of the problem. Is it possible to load also (only) related countries without running the ExecuteQueryAsync twice?

1 Answer 1

1

Your CAML query can join Partners and Countries so you can get the Continent as a projected field.

http://msdn.microsoft.com/en-us/library/ie/ee539975.aspx

1
  • Seems like the only way to do it :) And then translate projected fields over to my own objects.
    – Gomiunik
    Commented Jan 28, 2012 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.