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

I've got 2 lists: Companies and Employees. In Companies I have a text field called Name and in Employees I have a lookup to Companies based on the company Name.

What I'm trying to achieve is to get all Employees related to a specific company Name.

Gettting the related fields between the 2 lists:

SPList list = SPContext.Current.Web.Lists[ID];
SPRelatedFieldCollection relatedFields = list.GetRelatedFields();

Getting ALL the values from the child list:

SPList childList = SPContext.Current.Web.Lists[relatedFields[0].ListId];
SPFieldLookup lookupField = childList.Fields[relatedFields[0].FieldId] as SPFieldLookup;

foreach (SPListItem item in childList.Items)
{
    object rawValue = item[relatedFields[0].FieldId];
    if (rawValue !=null)
    {
        if (lookupField.AllowMultipleValues)
        {
            SPFieldLookupValueCollection values = item[relatedFields[0].FieldId] as SPFieldLookupValueCollection;

        }
        else
        {
            SPFieldLookupValue value = new SPFieldLookupValue(rawValue.ToString());
        }
    }
}

So how can I filter the child list to get only the specific employees from a single company(like Company = "Microsoft")? Keep in mind that 2 companies could have the same name, so I cannot based my solution on the value.

Thanks,

share|improve this question
1  
Are you using 2010 Enterprise? –  Lori Aug 19 '11 at 13:56
add comment

1 Answer

up vote 1 down vote accepted

I think I was working too hard and needed a break from the code to see the obvious...

I found out that you have the SPListItem.ID on the parent item will give you the relation with SPFieldLookupValue.LookupId on the child item.

share|improve this answer
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.