Look Up columns in SharePoint does not show the mapping if we save the list as a template

PROBLEM:

Look Up columns in SharePoint does not show the mapping if we save the list as a template and then create new list using this template which already has the lookup column definition

RESOLUTION:

Provided the explanation that when we save the lookup column with the list template , it stores the GUID as a reference of the other list and when we try creating a new list using that template , look column mapping fails because it does not find that GUID on the site. Possible workaround in our scenario was to re-map the columns after creating a list. Provided the sample code snippet to just giving an idea on creating look up columns programmatically.


SPSite objSite = new SPSite( "Site URL" ); 
// or we can do the SPControl.Getcontext() also

SPWeb objWeb = objSite.OpenWeb();

foreach(SPList objList in objWeb.Lists )
{
if(objList.Title ==txtLstFrom.Text.Trim())
{
string fieldDef = "<Field Type=\"Lookup\" List=\"{"+objWeb.Lists[txtLstTo.Text].ID+ "}\" ShowField=\"Title\" DisplayName=\"Look Me\" Name=\"LookMe\" /> ";

objList.Fields.AddFieldAsXml(fieldDef);
objList.Update();
}
}
Ebook Download
View all
Learn
View all