4

I've three lists: Competitions, Works and WorksVotes, linked to each other through Lookup fields. WorksVotes list has Lookup field to Works list, and Works list has Lookup field to Competitions list. What I need is to display the Competition in the WorksVotes list. I wrote some CAML code with 2 Joins which works fine with SPQuery object:

var query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name=\"ID\" /></OrderBy>";
query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Attachments\" /><FieldRedf Name=\"LinkTitle\" /><FieldRef Name=\"CompetitionTitle\" />";
query.Joins =@"<Join Type=""INNER"" ListAlias=""worksList"">
<Eq>
<FieldRef Name=""CompetitionWork"" RefType=""ID"" />
<FieldRef Name=""ID"" List=""worksList"" />
</Eq>
</Join>
<Join Type=""INNER"" ListAlias=""competitionsList"">
<Eq>
<FieldRef List=""worksList"" Name=""Competition"" RefType=""ID"" />
<FieldRef List=""competitionsList"" Name=""ID"" />
</Eq>
</Join>";
query.ProjectedFields = "<Field Name=\"CompetitionTitle\" Type=\"Lookup\" List=\"competitionsList\" ShowField=\"Title\" />";

After testing I added this CAML into schema.xml file that defines the WorksVotes list:

<ViewFields>
<FieldRef Name="Title"></FieldRef>
...
<FieldRef Name="CompetitionTitle"></FieldRef>
</ViewFields>
<Joins>
<Join Type="INNER" ListAlias="worksList">
<Eq>
<FieldRef Name="CompetitionWork" RefType="ID" />
<FieldRef Name="ID" List="worksList" />
</Eq>
</Join>
<Join Type="INNER" ListAlias="competitionsList">
<Eq>
<FieldRef List="worksList" Name="Competition" RefType="ID" />
<FieldRef List="competitionsList" Name="ID" />
</Eq>
</Join>
</Joins>
<ProjectedFields>
<Field Name="CompetitionTitle" Type="Lookup" List="competitionsList" ShowField="Title" />
</ProjectedFields>

But in this case I get an SPException telling that the "CompetitionTitle" field is unavailable or does not exist. What I'm doing wrong?

1

This really intrigued me so I invested an hour into analysis of the problem.

First of all, I was able to show the view using unchanged OP's declarative description of the view: enter image description here

Unfortunately, to achieve this I had to change internal state of variables inside Microsoft.Sharepoint.dll. Conclusion, you can't do this without recompiling SharePoint or injecting code.

A little more information for anyone interested. There is a bug in SPFieldCollection class, method InitializeProjectedFields(). Instead of current implementation of for loop:

for (int k = this.m_iCount; k < (this.m_iCount + this.ProjectedFields.get_Count()); k = (int) (k + 1))
{
    string str5 = "<Field/>";
    XmlDocument document = new XmlDocument();
    document.LoadXml(str5);
    XmlNode documentElement = document.DocumentElement;
    SPGlobal.SetNamedStringItem(documentElement.OwnerDocument, documentElement, "Type", "Lookup");
    SPGlobal.SetNamedBoolItem(documentElement.OwnerDocument, documentElement, "ReadOnly", true);
    SPGlobal.SetNamedStringItem(documentElement.OwnerDocument, documentElement, "DisplayName", this.projectedFields.get_Item((int) (k - this.m_iCount)).strName);
    SPGlobal.SetNamedStringItem(documentElement.OwnerDocument, documentElement, "ID", Guid.Empty.ToString());
    this.FieldsSafeArrayFullXML[k] = documentElement;
}

this should be implemented:

for (int k = this.m_iCount; k < (this.m_iCount + this.ProjectedFields.get_Count()); k = (int) (k + 1))
{
    string str5 = "<Field/>";
    XmlDocument document = new XmlDocument();
    document.LoadXml(str5);
    XmlNode documentElement = document.DocumentElement;
    SPGlobal.SetNamedStringItem(documentElement.OwnerDocument, documentElement, "Type", "Lookup");
    SPGlobal.SetNamedBoolItem(documentElement.OwnerDocument, documentElement, "ReadOnly", true);
    SPGlobal.SetNamedStringItem(documentElement.OwnerDocument, documentElement, "DisplayName", this.projectedFields.get_Item((int) (k - this.m_iCount)).strName);
    SPGlobal.SetNamedStringItem(documentElement.OwnerDocument, documentElement, "ID", Guid.Empty.ToString());
    this.FieldsSafeArrayFullXML[k] = documentElement.OuterXml;
}

Notice the OuterXml missing on the last line of current implementation.

0

After trying to make projected fields work in views (either defined programmatically or declaratively), I sadly conclude that one cannot use fields from other lists in views by help of Joins and Projected Fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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