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 am trying to pull populate a lookup field using a lookup value without the lookup ID. I am getting the error: "One or more field types are not installed properly. Go to the list settings page to delete these fields." I have the following code:

   public static SPFieldLookupValue getLookUpValue(string lookupValue, SPList myList, String LookupField, string LookupSourceField)
   {
       SPFieldLookup lookUpField = (SPFieldLookup)myList.Fields[LookupField];
       SPList lookupSourceList = myList.ParentWeb.Lists.GetList(new Guid(lookUpField.LookupList), false); //ParentWeb.Lists[lookUpField.LookupList];

       SPQuery query = new Microsoft.SharePoint.SPQuery();
       query.Query = String.Format("<Where><Eq><FieldRef Name=’{1}’/><Value Type=’Text’>{0}</Value></Eq></Where>", lookupValue, LookupSourceField);
       SPListItemCollection listItems = lookupSourceList.GetItems(query);
       if (listItems[0] != null)
       {
           return new SPFieldLookupValue(listItems[0].ID.ToString());
       }
       return null;
   }

I call the code like this:

  string LookupValue = null;
  if (properties.ListItem["SupplierID"] != null)
  {
      LookupValue = properties.ListItem["SupplierID"].ToString();
  }
  string FieldFromWhereToLookup = "sup";
  string SourceField = "SupplierID";
  SPFieldLookupValue lookupID = getLookUpValue(LookupValue, properties.List, FieldFromWhereToLookup, SourceField);

While stepping through the code, query.Query shows the following:

<Where>
    <Eq>
        <FieldRef Name=’SupplierID’/>
        <Value Type=’Text’>100367</Value>
    </Eq>
</Where>

I ensured that lookupSourceList is populated with the correct list. U2U CAMLBuilder shows the following when I build a query using the same list, field, and filter value:

<Where>
    <Eq>
        <FieldRef Name="SupplierID" />
        <Value Type="Text">100367</Value>
    </Eq>
</Where>
share|improve this question
add comment

2 Answers

Double quotes will do as well, like

 query.Query = String.Format("<Where><Eq><FieldRef Name=\"{1}\"/><Value Type=\"Text\">{0}</Value></Eq></Where>", lookupValue, LookupSourceField);
share|improve this answer
add comment
up vote 0 down vote accepted

My single quotes were off.

<Where><Eq><FieldRef Name=’{1}’/><Value Type=’Text’>{0}</Value></Eq></Where>

should have been:

<Where><Eq><FieldRef Name='{1}'/><Value Type='Text'>{0}</Value></Eq></Where>
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.