Question

I'm sure I'm missing something simple here, but I can't seem to figure it out. I have two lists:

Rating Sections:

  1. Title - Single line of text

Rating Categories:

  1. Title - Single line of text
  2. Rating Section - Lookup to Title of Rating sections

I am attempting to use CSOM to join and query the two lists. Each lists only contains 10 items so I shouldn't be hitting any sort of size limit. My code is as follows:

using (var spContext = new ClientContext("http://myserver/jointest")) {
    var list = spContext.Web.Lists.GetByTitle("Rating Categories");
    var query = new CamlQuery {
        ViewXml = @"
<View>
    <ViewFields>
        <FieldRef Name='ID' />
        <FieldRef Name='Title' />
        <FieldRef Name='Rating_x0020_Section' />
        <FieldRef Name='RatingSectionTitle' />
    </ViewFields>
    <Joins>
        <Join Type='INNER' ListAlias='RatingSections'>
            <Eq>
                <FieldRef Name='Rating_x0020_Section' RefType='Text' />
                <FieldRef List='Rating Sections' Name='Title' />
            </Eq>
        </Join>
    </Joins>
    <ProjectedFields>
        <Field Name='RatingSectionTitle' Type='Lookup' List='RatingSections' ShowField='Title' />
    </ProjectedFields>
</View>"
            };
    var listItems = list.GetItems(query);
    spContext.Load(listItems);
    spContext.ExecuteQuery();
}

Every time I run the code, I get the exception "Value does not fall within expected range". Any idea what I have wrong here?

Edit: I've also tried it with the following values:

<FieldRef Name='Rating_x0020_Section' RefType='Id' />
<FieldRef List='Rating Sections' Name='ID' />

Which still throws the same error. I've also tried every variation of capitalizing ID in both FieldRefs just in case it was case sensitive.

Was it helpful?

Solution

Finally found the answer. The List in the second FieldRef has to be the same as the ListAlias, so it should be:

<FieldRef List='RatingSections' Name='ID' />
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top