2

I am trying to load a drop down list with the same items that a lookup field uses. I thought it would be relatively straightforward, but the line

SPList lookupList = lookupWeb.Lists[lookupField.LookupList];

Throws an exception despite the list with that ID existing in the SPWeb. (I checked both through the object model and SharePoint Manager)

        SPFieldLookup lookupField = targetLibrary.Fields["DocumentType"] as SPFieldLookup;
        using (SPWeb lookupWeb = SPContext.Current.Site.OpenWeb(lookupField.LookupWebId))
        {
            SPList lookupList = lookupWeb.Lists[lookupField.LookupList];
            foreach (SPListItem item in lookupList.Items)
            {
                ddlDocumentType.Items.Add(new ListItem(item["Title"].ToString(), item["ID"].ToString()));
            }
        }

List '{843fa16d-ec85-45c3-8d27-20d0241d41d6}' does not exist at site with URL

Is there something simple I am missing?

2 Answers 2

1

Highest voted answer doesn't even provide the right answer. Try doing this:

SPList lookupList = lookupWeb.Lists[new Guid(lookupField.LookupList)];

Instead of this:

SPList lookupList = lookupWeb.Lists[lookupField.LookupList];
2

So SPFieldLookup has an actual Guid for LookupWebId property and only a string for the LookupList property. So the line

SPList lookupList = lookupWeb.Lists[lookupField.LookupList];

Is using the string override which looks up the list name.

So the learning here, yet again, is do not expect the SharePoint object model to be consistent in any way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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