Recently, in one of our projects we came across a requirement where in we needed to clear all fields for a list item on an item copy or a checkin.
In order to implement this , we used eventhandlers for ItemCheckedIn and implemented the code to clear all fields, setting all fields to null i.e.
private void ClearPropertiesOfItem(SPListItem itemCopiedOrCheckedIn, List<String> fieldsTobeCleared)
{
String validationFormula = "";
try
{
if (fieldsTobeCleared.Count > 0)
{
foreach (String fieldToBeCleared in fieldsTobeCleared)
{
foreach (SPField field in itemCopiedOrCheckedIn.Fields)
{
if (field.Title.Equals(fieldToBeCleared))
{
if (!String.IsNullOrEmpty(field.ValidationFormula))
{
validationFormula = field.ValidationFormula;
field.ValidationFormula = string.Empty;
field.Update();
}
itemCopiedOrCheckedIn[field.Id] = null;
if (!String.IsNullOrEmpty(validationFormula))
{
field.ValidationFormula = validationFormula;
field.Update();
}
}
}
}
itemCopiedOrCheckedIn.SystemUpdate();
}
}
catch (Exception Ex)
{
throw new Exception(base.GetResourceString(COMMON.Constants.EXCEPTION_OCCURED_WHILE_CLEARING_PROPERTIES), Ex);
}
}
This worked well for all fields except the managed metadata or the taxonomy columns which give an exception while setting the value to null.
In order to clear these columns , we explicitly need to set the properties "WssId, TermGuid and Label" to null as is shown below:
private void ClearPropertiesOfItem(SPListItem itemCopiedOrCheckedIn, List<String> fieldsTobeCleared)
{
String validationFormula = "";
try
{
if (fieldsTobeCleared.Count > 0)
{
foreach (String fieldToBeCleared in fieldsTobeCleared)
{
foreach (SPField field in itemCopiedOrCheckedIn.Fields)
{
if (field.Title.Equals(fieldToBeCleared))
{
if (!String.IsNullOrEmpty(field.ValidationFormula))
{
validationFormula = field.ValidationFormula;
field.ValidationFormula = string.Empty;
field.Update();
}
//special handling for managed metadata column (single value)
if (field.TypeAsString.Equals(“TaxonomyFieldType
“))
{
if (itemCopiedOrCheckedIn[field.Id] != null)
{
TaxonomyFieldValue managedMetadataFieldValue = itemCopiedOrCheckedIn[field.Id] as TaxonomyFieldValue;
managedMetadataFieldValue.WssId = 0;
managedMetadataFieldValue.TermGuid = null;
managedMetadataFieldValue.Label = null;
TaxonomyField managedMetadataField = itemCopiedOrCheckedIn.Fields[field.Id] as TaxonomyField;
managedMetadataField.SetFieldValue(itemCopiedOrCheckedIn, managedMetadataFieldValue);
}
}
else if (field.TypeAsString.Equals("TaxonomyFieldTypeMulti"))//special handling for managed metadata column (multi value)
{
if (itemCopiedOrCheckedIn[field.Id] != null)
{
TaxonomyFieldValueCollection managedMetadataFieldValueColl = new TaxonomyFieldValueCollection(string.Empty);
managedMetadataFieldValueColl.PopulateFromLabelGuidPairs(itemCopiedOrCheckedIn[field.Id].ToString());
int fieldValueCount = managedMetadataFieldValueColl.Count;
managedMetadataFieldValueColl.RemoveRange(0, fieldValueCount);
TaxonomyField managedMetadataField = itemCopiedOrCheckedIn.Fields[field.Id] as TaxonomyField;
managedMetadataField.SetFieldValue(itemCopiedOrCheckedIn, managedMetadataFieldValueColl);
}
}
else
{
itemCopiedOrCheckedIn[field.Id] = null;
}
if (!String.IsNullOrEmpty(validationFormula))
{
field.ValidationFormula = validationFormula;
field.Update();
}
}
}
}
itemCopiedOrCheckedIn.SystemUpdate();
}
}
catch (Exception Ex)
{
throw new Exception(base.GetResourceString(COMMON.Constants.EXCEPTION_OCCURED_WHILE_CLEARING_PROPERTIES), Ex);
}
}