Scenario
You want to add custom column to your KPI list using, but don't want to do this in UI.
Solution
1. Add your custom column to the Site Columns
SPWeb web = ...;
string name = "Mighty Column";
SPFieldType type = SPFieldType.Text;
bool required = false;
SPField field = null;
string internalName = web.Fields.Add(name, type, required);
field = web.Fields.GetFieldByInternalName(internalName);
web.Update();
2. Alternative 1 - Add the created column to one of the KPI List content types
SPList list = null;
string sListTitle = "Mighty KPI List";
try
{
list = sweb.Lists[sListTitle];
}
catch
{
}
if (list != null)
{
SPContentType ct = list.ContentTypes["Indicator using data in SharePoint list"];
// or Indicator using data in Excel workbook
// or Indicator using data in SQL Server 2005 Analysis Services
// or Indicator using manually entered information
if (ct != null && !ct.Fields.ContainsField(name))
{
ct.FieldLinks.Add(new SPFieldLink(field));
ct.Update();
}
}
2. Alternative 2 - Add column to the list fields straight away
string internalName = list.Fields.Add(name, type, required);
list.Update();
Stay tuned.
You want to add custom column to your KPI list using, but don't want to do this in UI.
Solution
1. Add your custom column to the Site Columns
SPWeb web = ...;
string name = "Mighty Column";
SPFieldType type = SPFieldType.Text;
bool required = false;
SPField field = null;
string internalName = web.Fields.Add(name, type, required);
field = web.Fields.GetFieldByInternalName(internalName);
web.Update();
2. Alternative 1 - Add the created column to one of the KPI List content types
SPList list = null;
string sListTitle = "Mighty KPI List";
try
{
list = sweb.Lists[sListTitle];
}
catch
{
}
if (list != null)
{
SPContentType ct = list.ContentTypes["Indicator using data in SharePoint list"];
// or Indicator using data in Excel workbook
// or Indicator using data in SQL Server 2005 Analysis Services
// or Indicator using manually entered information
if (ct != null && !ct.Fields.ContainsField(name))
{
ct.FieldLinks.Add(new SPFieldLink(field));
ct.Update();
}
}
2. Alternative 2 - Add column to the list fields straight away
string internalName = list.Fields.Add(name, type, required);
list.Update();
Stay tuned.
Comments
Post a Comment