As we know , Dependent lookup columns is a new feature introduced in sharepoint 2010 to include other columns like Title, ID,Version,created,modified along with your lookup column etc.
Once a lookup column with these dependent lookup columns is associated with a document library/list, they appear as shown in the screenshot.
Recently, we came across a requirement of replicating a lookup column's dependent lookup columns on another site.
The way we accomplished this is by
-storing all the dependent lookup columns for a lookup column in xml, and then,
-creating the lookup column and its dependent lookup columns on the other site from the xml.
The xml can be constructed by fetching all dependent lookup columns for a lookup column using the
GetDependentLookupInternalNames() method.
SPFieldLookup changedSpFieldLookup = changedSpField as SPFieldLookup;
List<string> dependentlookupNames=changedSpFieldLookup.GetDependentLookupInternalNames();
//....construct the xml using the lookup names retrieved in 'dependentlookupNames'...
Once, this is done ,the xml that we constructed looks like this:
<AdditionalColumnConfiguration>
<ColumnConfigData>
<DependentLookupColumnNames>
<DependentLookupColumnName Key="Title">Title</DependentLookupColumnName>
<DependentLookupColumnName Key="ID">ID</DependentLookupColumnName>
</DependentLookupColumnNames>
</ColumnConfigData>
</AdditionalColumnConfiguration>
Once this was done, in order to replicate the same settings on another site, we created a new lookup field and added the dependent lookup columns by parsing this xml.The method 'CreateDependentLookUpColumns' does this:
Here 'spPrimaryField' is the new lookup field with which the dependent lookups are to be associated.
'objSPContentType' is the content type in which spPrimaryField has already been added
'additionalConfigXml' is having the xml string specified above.
private void CreateDependentLookUpColumns(SPFieldLookup spPrimaryField, string additionalConfigXml, SPContentType objSPContentType)
{
XmlDocument objAdditionalConfigXmlDoc = new XmlDocument();
objAdditionalConfigXmlDoc.LoadXml(additionalConfigXml);
XmlNode additionalConfigXmlNode = null;
XmlNode configDataNode = null;
XmlNode dependentColumnsNode = null;
Boolean dependentColumnsExist = false;
if (objAdditionalConfigXmlDoc != null)
{
additionalConfigXmlNode = objAdditionalConfigXmlDoc.DocumentElement;
if (additionalConfigXmlNode != null)
{
if (additionalConfigXmlNode.HasChildNodes)
{
configDataNode = additionalConfigXmlNode.SelectSingleNode("child::*[name()='ColumnConfigData']");
if (configDataNode != null)
{
dependentColumnsNode = configDataNode.SelectSingleNode("child::*[name()='DependentLookupColumnNames']");
if (dependentColumnsNode != null)
{
if (dependentColumnsNode.HasChildNodes)
{
dependentColumnsExist = true;
}
}
}
}
}
}
if (spPrimaryField != null && dependentColumnsExist)
{
if (!spPrimaryField.IsDependentLookup)
{
foreach (XmlNode dependentColumnNode in dependentColumnsNode.ChildNodes)
{
string strDepColName = dependentColumnNode.InnerText;
if (!string.IsNullOrEmpty(strDepColName))
{
strDepColName = SPHttpUtility.HtmlDecode(strDepColName, true);
}
string displayName = spPrimaryField.Title + ":" + strDepColName;
if (displayName.Length > 255)
{
displayName = displayName.Substring(0, 255);
}
SPFieldLookup field = (SPFieldLookup)this.CurrentWeb.Fields.CreateNewField(SPFieldType.Lookup.ToString(), displayName);
if (field == null)
{
continue;
}
field.LookupList = spPrimaryField.LookupList;
field.LookupWebId = spPrimaryField.LookupWebId;
field.LookupField = dependentColumnNode.Attributes.GetNamedItem("Key").Value;
field.PrimaryFieldId = spPrimaryField.Id.ToString();
field.ReadOnlyField = true;
field.AllowMultipleValues = spPrimaryField.AllowMultipleValues;
field.UnlimitedLengthInDocumentLibrary = spPrimaryField.UnlimitedLengthInDocumentLibrary;
if (this.CurrentWeb.RegionalSettings.IsRightToLeft)
{
field.Direction = spPrimaryField.Direction;
}
field.Group = spPrimaryField.Group;
string strName = this.CurrentWeb.Fields.Add(field);
if (objSPContentType != null)
{
objSPContentType.FieldLinks.Add(new SPFieldLink(this.CurrentWeb.Fields.GetFieldByInternalName(strName)));
objSPContentType.Update();
}
}
}
}
}
Once a lookup column with these dependent lookup columns is associated with a document library/list, they appear as shown in the screenshot.
Recently, we came across a requirement of replicating a lookup column's dependent lookup columns on another site.
The way we accomplished this is by
-storing all the dependent lookup columns for a lookup column in xml, and then,
-creating the lookup column and its dependent lookup columns on the other site from the xml.
The xml can be constructed by fetching all dependent lookup columns for a lookup column using the
GetDependentLookupInternalNames() method.
SPFieldLookup changedSpFieldLookup = changedSpField as SPFieldLookup;
List<string> dependentlookupNames=changedSpFieldLookup.GetDependentLookupInternalNames();
//....construct the xml using the lookup names retrieved in 'dependentlookupNames'...
Once, this is done ,the xml that we constructed looks like this:
<AdditionalColumnConfiguration>
<ColumnConfigData>
<DependentLookupColumnNames>
<DependentLookupColumnName Key="Title">Title</DependentLookupColumnName>
<DependentLookupColumnName Key="ID">ID</DependentLookupColumnName>
</DependentLookupColumnNames>
</ColumnConfigData>
</AdditionalColumnConfiguration>
Once this was done, in order to replicate the same settings on another site, we created a new lookup field and added the dependent lookup columns by parsing this xml.The method 'CreateDependentLookUpColumns' does this:
Here 'spPrimaryField' is the new lookup field with which the dependent lookups are to be associated.
'objSPContentType' is the content type in which spPrimaryField has already been added
'additionalConfigXml' is having the xml string specified above.
private void CreateDependentLookUpColumns(SPFieldLookup spPrimaryField, string additionalConfigXml, SPContentType objSPContentType)
{
XmlDocument objAdditionalConfigXmlDoc = new XmlDocument();
objAdditionalConfigXmlDoc.LoadXml(additionalConfigXml);
XmlNode additionalConfigXmlNode = null;
XmlNode configDataNode = null;
XmlNode dependentColumnsNode = null;
Boolean dependentColumnsExist = false;
if (objAdditionalConfigXmlDoc != null)
{
additionalConfigXmlNode = objAdditionalConfigXmlDoc.DocumentElement;
if (additionalConfigXmlNode != null)
{
if (additionalConfigXmlNode.HasChildNodes)
{
configDataNode = additionalConfigXmlNode.SelectSingleNode("child::*[name()='ColumnConfigData']");
if (configDataNode != null)
{
dependentColumnsNode = configDataNode.SelectSingleNode("child::*[name()='DependentLookupColumnNames']");
if (dependentColumnsNode != null)
{
if (dependentColumnsNode.HasChildNodes)
{
dependentColumnsExist = true;
}
}
}
}
}
}
if (spPrimaryField != null && dependentColumnsExist)
{
if (!spPrimaryField.IsDependentLookup)
{
foreach (XmlNode dependentColumnNode in dependentColumnsNode.ChildNodes)
{
string strDepColName = dependentColumnNode.InnerText;
if (!string.IsNullOrEmpty(strDepColName))
{
strDepColName = SPHttpUtility.HtmlDecode(strDepColName, true);
}
string displayName = spPrimaryField.Title + ":" + strDepColName;
if (displayName.Length > 255)
{
displayName = displayName.Substring(0, 255);
}
SPFieldLookup field = (SPFieldLookup)this.CurrentWeb.Fields.CreateNewField(SPFieldType.Lookup.ToString(), displayName);
if (field == null)
{
continue;
}
field.LookupList = spPrimaryField.LookupList;
field.LookupWebId = spPrimaryField.LookupWebId;
field.LookupField = dependentColumnNode.Attributes.GetNamedItem("Key").Value;
field.PrimaryFieldId = spPrimaryField.Id.ToString();
field.ReadOnlyField = true;
field.AllowMultipleValues = spPrimaryField.AllowMultipleValues;
field.UnlimitedLengthInDocumentLibrary = spPrimaryField.UnlimitedLengthInDocumentLibrary;
if (this.CurrentWeb.RegionalSettings.IsRightToLeft)
{
field.Direction = spPrimaryField.Direction;
}
field.Group = spPrimaryField.Group;
string strName = this.CurrentWeb.Fields.Add(field);
if (objSPContentType != null)
{
objSPContentType.FieldLinks.Add(new SPFieldLink(this.CurrentWeb.Fields.GetFieldByInternalName(strName)));
objSPContentType.Update();
}
}
}
}
}
No comments:
Post a Comment