Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
Seems like the only way to do it :) And then translate projected fields over to my own objects. –  Gomiunik Jan 28 '12 at 21:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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