I add lookup columns with a elements.xml file and like everyone else I had issues with them once deployed, i.e. "Get information from:" was empty for the lookup list. I found www.n8d.at/blog/lookup-fields-as-site-column-declarative-deployed/ which solved the issue for the root site. A content type contains my custom site columns and when I create a subsite, add this content type to a library and check the lookup columns on this library they are empty. If I go to library settings and add "from existing columns" and add one of the lookup columns and give it a different name it's empty. If I go to the root site and create a new lookup column through the GUI, go to the subsite and add it to the library the column works fine.
So what am I missing here when it comes to subsites?
In a feature receiver I have the following code:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite targetSite = properties.Feature.Parent as SPSite;
using (SPSite site = new SPSite(targetSite.ID))
{
using (SPWeb web = site.OpenWeb())
{
SPField invoiceStatusField = web.Fields.TryGetFieldByStaticName("InvoiceStatus");
if (invoiceStatusField != null)
Utility.SetLookupColumnList(invoiceStatusField, web);
}
}
}
public static void SetLookupColumnList(SPField field, SPWeb web)
{
XDocument fieldSchema = XDocument.Parse(field.SchemaXml);
XElement root = fieldSchema.Root;
if (root.Attribute("List") != null)
{
string listurl = root.Attribute("List").Value;
SPFolder listFolder = web.GetFolder(listurl);
if (listFolder != null && listFolder.Exists == true)
{
XAttribute attrList = root.Attribute("List");
if (attrList != null)
{
attrList.Value = listFolder.ParentListId.ToString();
}
XAttribute attrWeb = root.Attribute("SourceID");
if (attrWeb != null)
{
attrWeb.Value = web.ID.ToString();
}
field.SchemaXml = fieldSchema.ToString();
}
}
}
When I add an item to a list in the root site I create a new library (event receiver) in a subsite and assign it the invoice content type
SPContentType invoiceCt = rootWeb.AvailableContentTypes["Invoice"];
if (invoiceCt != null)
pagesLibrary.ContentTypes.Add(invoiceCt);
The content type gets added but as I previously mentioned the lookup columns have "Get information from:" empty.
Any ideas?
Thanks in advance.
To repeat steps. Deploy site columns, content type, in root site lookup columns works. Create subsite, assign content type to new library - lookup columns doesn't work.
Edit: after I created a new site column and added it to the list and checked the xml schema for the list I saw that the new column has a web id but the one in my content type doesn't. Also my column doesn't have list GUID wrapped in {} but the new column does (the GUID is however the same). SourceID is also different.