I have 2 site content types defined that are deployed as a site feature:

  • Customer
    • Name
    • Address
  • Order
    • Value

There is a second web feature that deploys a list to the root site for Customers. This list uses the Customer content type.

When this feature is activated I create a site lookup column for this list, one for the customer name and a dependent lookup for the Address, and add it to the Order site content type:

using (SPWeb web = properties.Feature.Parent as SPWeb)
    //create the lookup column
    SPList list = web.Lists["Customers"];

    web.Fields.AddLookup("Customer Lookup", list.ID, list.ParentWeb.ID, true);
    SPFieldLookup lookup = (SPFieldLookup)web.Fields["Customer Lookup"];
    lookup.AllowMultipleValues = false;
    lookup.Indexed = true;
    lookup.Group = "Custom Columns";
    lookup.LookupField = "Name";

    //create the dependent lookup column
    string lookupAddress = web.Fields.AddDependentLookup("Customer Lookup: Address", lookup.Id);
    SPFieldLookup fieldLookupAddress = (SPFieldLookup)web.Fields.GetFieldByInternalName(lookupAddress);
    fieldLookupAddress.LookupField = list.Fields["Address"].InternalName;

    //now add the lookup field to our Order content type
    SPContentType contentType = web.ContentTypes["Order"];
    if (!contentType.Fields.ContainsField("Customer Lookup"))
        SPField field = web.Fields["Customer Lookup"];
        SPFieldLink fieldLink = new SPFieldLink(field);

Finally there is a third web feature that deploys another list, to a subsite, called Orders. This list uses the Order content type.


Using the UI I am able to create customers in the root site list and then create an order in the subsite list. This correctly references the customers list and displays the order value, customer name and customer address.

Using linq I am unable to access the value of the dependent lookup columns, i.e. the customer address, Null is returned.

I have used SPMetal to generate the entity class. This looks to be correct and all columns appear in it.

What could be the problem?

  • why don't you use Joins on Lists when using LINQ to SharePoint? see Joins with LINQ to SharePoint – Arsalan Adam Khatri Sep 24 '13 at 11:21
  • @ArsalanAdamKhatri Interestingly SPMetal hasn't created the relationship between the lists even though it's added the lookup columns. Perhaps because the lookup list is in a parent web? – Perrin255 Sep 24 '13 at 11:59
  • Yup, I guess that will be the cause! – Arsalan Adam Khatri Sep 24 '13 at 12:02

Your Answer

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

Browse other questions tagged or ask your own question.