To create a calculated field to sort the outline numbers programmatically. Following code helps to create the fields and calculated values on feature activation.
Feature Receiver Code
string strListName = "Checklist";
string fieldName = "Outline";
string strSortFieldname = "CustomOutlineSort";
string strCalculatedFieldname = "OutlineSorted";
string OutlineFieldValue = string.Empty;
string newOutlineFieldValue;
/// <summary>
/// FeatureActivated
/// </summary>
/// <param name="properties"></param>
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPWeb web = (SPWeb)properties.Feature.Parent)
{
web.AllowUnsafeUpdates = true;
SPList objSPList = web.Lists[strListName];
//Create Readonly field 'CustomOutlineSort'
objSPList.Fields.Add(strSortFieldname, SPFieldType.Text, false);
SPFieldText txtField = (SPFieldText)objSPList.Fields[strSortFieldname];
txtField.ReadOnlyField = true;
txtField.Update();
//Create calculated field 'OutlineSorted'
objSPList.Fields.Add(strCalculatedFieldname, SPFieldType.Calculated, false);
SPFieldCalculated CalcField = (SPFieldCalculated)objSPList.Fields[strCalculatedFieldname];
CalcField.Formula = @"=" + strSortFieldname;
CalcField.Update();
objSPList.Update();
web.AllowUnsafeUpdates = false;
CalculateSortValue(properties);
}
});
}
catch (Exception ex)
{
throw new SPException("An error occured when activating the feature. Make sure that the list 'Checklist' exists in this site.");
}
}
private void CalculateSortValue(SPFeatureReceiverProperties properties)
{
try
{
using (SPWeb oSite = (SPWeb)properties.Feature.Parent)
{
oSite.AllowUnsafeUpdates = true;
SPList oList = oSite.Lists[strListName];
//Check if Outline, CustomOutlineSort and OutlineSorted fields are created.
if (oList.Fields.ContainsField(fieldName) && oList.Fields.ContainsField(strSortFieldname) && oList.Fields.ContainsField(strCalculatedFieldname))
{
foreach (SPListItem oListItem in oList.Items)
{
//Check if Outline field value is not empty
if (oListItem[fieldName] != null)
{
newOutlineFieldValue = string.Empty;
OutlineFieldValue = oListItem[fieldName].ToString();
string[] sections = OutlineFieldValue.Split('.');
//loop all the parts splitted with dot and left pad with '0' and make total width as 2 characters.
foreach (string section in sections)
{
newOutlineFieldValue = newOutlineFieldValue + section.PadLeft(2, '0');
}
//append '00'to make total of 8 characters
for (int i = sections.Length; i < 4; i++)
{
newOutlineFieldValue = newOutlineFieldValue + "00";
}
}
//Store the customized value in readonly field 'CustomOutlineSort'.
oListItem[strSortFieldname] = newOutlineFieldValue;
oListItem.Update();
}
}
}
}
catch (Exception ex)
{
throw new SPException("An error occured while calculating sort values for 'Outline' column in 'Checklist' list");
}
}
EventHandler code for ItemAdded and ItemUpdated events
//Sorting Outline Numbers in SharePoint list
string listName = "Checklist";
string fieldName = "Section";
string strSortFieldname = "CustomSectionSort";
string strCalculatedFieldname = "SectionSorted";
string OutlineFieldValue = string.Empty;
string newOutlineFieldValue;
/// <summary>
/// Method to make the outline number sortable. The logic used is, the value will be splitted by dot and left pad
/// each part with '0' to width of 2. After padding, merge all the parts and save the value in 'CustomOutlineSort' field.
/// For instance 1.1.1 will be saved as 010101.
/// </summary>
/// <param name="properties"></param>
private void CalculateSortValue(SPItemEventProperties properties)
{
try
{
SPWeb oSite = properties.OpenWeb();
SPList oList = oSite.Lists[listName];
SPListItem oListItem = properties.ListItem;
//Check if Outline, CustomOutlineSort and OutlineSorted fields are created.
if (oList.Fields.ContainsField(fieldName) && oList.Fields.ContainsField(strSortFieldname) && oList.Fields.ContainsField(strCalculatedFieldname))
{
if (properties.ListItem[fieldName] != null)
{
newOutlineFieldValue = string.Empty;
OutlineFieldValue = properties.ListItem[fieldName].ToString();
string[] sections = OutlineFieldValue.Split('.');
foreach (string section in sections)
{
newOutlineFieldValue = newOutlineFieldValue + section.PadLeft(2, '0');
}
for (int i = sections.Length; i < 4; i++)
{
newOutlineFieldValue = newOutlineFieldValue + "00";
}
}
oListItem[strSortFieldname] = newOutlineFieldValue;
oListItem.Update();
oList.Update();
}
}
catch (Exception ex)
{
}
}