The below solution will help you while moving the lists which are having the lookups fileds from one site to number sites.
public const string Parent_Child_LookUpFieldName = "LookupFieldName"; public const string CustomParentList = "ParentList"; public const string CustomChildList = "ChildList"; FixLookupFieldsInList(Parent_Child_LookUpFieldName, currentWeb.Lists[CustomParentList], currentWeb.Lists[CustomChildList]); #region FixLookupFieldsInList ////// FUNCTION : FixLookupFieldsInList /// PURPOSE : To fix the LookupFields in a list imported using a stp file /// PARAMETERS : currentWeb -> current web /// fieldName -> The column name to be fixed /// list -> The list to be fixed /// targetList -> Then list to which the fieldName must reference /// RETURNS : None /// --------------------------------------------------------------------- /// public static void FixLookupFieldsInList(string fieldName, SPList list, SPList targetList) { try { list.ParentWeb.AllowUnsafeUpdates = true; if (list.Fields.ContainsField(fieldName)) { SPFieldLookup field = (SPFieldLookup)list.Fields[fieldName]; //check if the field link to the list is broken. if (field.LookupList != targetList.ID.ToString()) { //copy the schema from the field string newFieldXml = field.SchemaXml; string oldsourceid = field.LookupList; //put the ID of the current list as the source for the lookup field.SchemaXml = newFieldXml.Replace(oldsourceid, "{" + targetList.ID.ToString() + "}"); } } } catch (Exception ex) { //Error Handling Code } finally { list.ParentWeb.AllowUnsafeUpdates = false; } } #endregion
No comments:
Post a Comment