How to create a lookup field for the Choice field..For eg: In a list i have Choice field and i have to create an lookup column in other list pointing to this choice field ..When i select this list this column is not appearing in the dropdown…Please let me know if there is any Limitation for that
Sharepoint LookUp field on Choice field
lookupsharepointsharepoint-2007sharepoint-2010
Related Solutions
Here's some code that will add a lookup field to an exsisting content type.
If your using list definitions then this is the only way a lookup field can be included. It can't be added into the CAML of the list definition because a guid for the lookup list is required and this is not known before hand. SharePoint autogenerates this Guid when the list is created.
So you need to first create the lookup column inside the root SPWeb of the SPSite
private void CreateLookup(SPWeb web, SPList lookupList, String lookupField, String fieldName, String fieldGroup, bool allowMultiLookup)
{
using (SPSite site = web.Site)
{
using (SPWeb rootWeb = site.RootWeb)
{
rootWeb.Fields.AddLookup(fieldName, lookupList.ID, web.ID, false);
SPFieldLookup fieldLookup = (SPFieldLookup)rootWeb.Fields[fieldName];
if (fieldLookup == null) return;
fieldLookup.AllowMultipleValues = allowMultiLookup;
fieldLookup.LookupField = lookupField;
fieldLookup.Group = fieldGroup;
fieldLookup.Title = fieldName;
fieldLookup.Update(true);
}
}
}
And then you'll need to add this field to the exsisting content type
private void AddLookupToContentType(SPWeb web, String fieldName, String contentTypeName)
{
using (SPSite site = web.Site)
{
using (SPWeb rootWeb = site.RootWeb)
{
SPFieldLookup lookupField = (SPFieldLookup)rootWeb.Fields[fieldName];
if (lookupField == null) return;
SPContentType riskContentType = rootWeb.ContentTypes[contentTypeName];
if (riskContentType == null) return;
riskContentType.FieldLinks.Add(new SPFieldLink(lookupField));
riskContentType.Update(true);
}
}
}
What that log message means is that Lists/FavoriteSites does not exist in either the site collection root site or the site where the feature is being activated (if the latter is a subsite).
If Lists/FavoriteSites is being created by a feature, then make sure that it is activated before your Fields feature. Put the ListInstance element right above your Field element if you have to. But the message is clear: the list does not exist at the time that your custom field is being provisioned to the site collection. And that is why it is not working.
To be clear, the list has to exist at the time that the feature is activated, not just when you create a list instance that will use that field.
P.S. I found this by checking the "documentation" for the Microsoft.SharePoint.SPFieldElement.PerformFixUpIfLookUpField method.
Best Answer
It is not possible to create a lookup field for a choice (dropdown) field. There are two ways to resolve your problem: The programmatic approach and the workaround.
The programmatic approach involves an creating event receiver to do the magic - pretty work intensive. But there is an explanation here:
You can also just create another list, containing your choice field values and use a calculated field as a source for your lookup column. Check out the following explanation: