Scenario:
Of the common issues with Content Types updates in production is change in field types. OOTB there are few supported field type change but some time not really give you the correct results.
So as a workaround you can add a new field to content type and copy over the data from the old field to the new one.
Along the same lines one of my colleague wanted to copy data across Lookup Column Fields. Source field was a Single Select field and destination was Multi-Select field and there were 100s of sub sites to update.
Solution:
Small utility function did the trick.
Code:
public static void CopyValues()
{
string siteCollectionUrl = "http://localhost";
string sourceFieldDisplayName = "SourceFieldDisplayName";
string destinationFieldDisplayName = "DestinationFieldDisplayName";
using (SPSite site = new SPSite(siteCollectionUrl))
{
foreach (SPWeb web in site.AllWebs)
{
foreach (SPList list in web.Lists)
{
try
{
if (null != list.Fields[sourceFieldDisplayName] && null != list.Fields[destinationFieldDisplayName])
{
string sourceFieldInternalName = list.Fields[sourceFieldDisplayName].InternalName;
string destinationFieldInternalName = list.Fields[destinationFieldDisplayName].InternalName;
foreach (SPListItem item in list.Items)
{
SPFieldLookupValue sourcevalue = new SPFieldLookupValue(item[sourceFieldInternalName].ToString());
SPFieldLookupValueCollection destinationValueCollection = new SPFieldLookupValueCollection();
destinationValueCollection.Add(sourcevalue);
item[destinationFieldInternalName] = destinationValueCollection;
item.Update();
}
list.Update();
}
}
catch
{
}
}
web.Dispose();
}
}
}
0 comments:
Post a Comment