This post will tell that how to add different types of columns to a SharePoint List and its views 2007/2010 through code. It will tell how to add all kinds of columns in an already existing list and also their views. You can put this code inside a feature or in whatever context you need it :
Adding a Single Line of Text column :
Adding a Calculated Type of Column :
Adding a Single Line of Text column :
private static void AddSingleLineOfTextFieldToList(SPList objSPList, string FieldName)
{
if (objSPList != null)
{
if (!objSPList.Fields.ContainsField(FieldName))
{
objSPList.Fields.Add(FieldName, SPFieldType.Text, false);
}
}
}
{
if (objSPList != null)
{
if (!objSPList.Fields.ContainsField(FieldName))
{
objSPList.Fields.Add(FieldName, SPFieldType.Text, false);
}
}
}
Adding a Multiple Line of Text column :
private static void AddMultipleLineOfTextFieldToList(SPList objSPList, string FieldName)
{
if (objSPList != null)
{
if (!objSPList.Fields.ContainsField(FieldName))
{
objSPList.Fields.Add(FieldName, SPFieldType.Note, false);
}
}
}
{
if (objSPList != null)
{
if (!objSPList.Fields.ContainsField(FieldName))
{
objSPList.Fields.Add(FieldName, SPFieldType.Note, false);
}
}
}
Adding a Rich Text Type of column :
private static void AddRichTextFieldToList(SPList objSPList, string FieldName)
{
if (objSPList != null)
{
if (!objSPList.Fields.ContainsField(FieldName))
{
objSPList.Fields.Add(FieldName, SPFieldType.Note, true);
SPFieldMultiLineText field = (SPFieldMultiLineText)objSPList.Fields[FieldName];
field.RichText = true;
field.RichTextMode = SPRichTextMode.Compatible;
field.Update();
}
}
}
{
if (objSPList != null)
{
if (!objSPList.Fields.ContainsField(FieldName))
{
objSPList.Fields.Add(FieldName, SPFieldType.Note, true);
SPFieldMultiLineText field = (SPFieldMultiLineText)objSPList.Fields[FieldName];
field.RichText = true;
field.RichTextMode = SPRichTextMode.Compatible;
field.Update();
}
}
}
Adding a Calculated Type of Column :
private static void AddCalculatedFieldToList(SPList objSPList, string FieldName, string Formula, SPFieldType OutputType)
{
if (objSPList != null)
{
if (!objSPList.Fields.ContainsField(FieldName))
{
objSPList.Fields.Add(FieldName, SPFieldType.Calculated, false);
SPFieldCalculated CalcField = (SPFieldCalculated)objSPList.Fields[FieldName];
CalcField.OutputType = OutputType;
CalcField.Formula = Formula;
CalcField.Update();
}
}
}
{
if (objSPList != null)
{
if (!objSPList.Fields.ContainsField(FieldName))
{
objSPList.Fields.Add(FieldName, SPFieldType.Calculated, false);
SPFieldCalculated CalcField = (SPFieldCalculated)objSPList.Fields[FieldName];
CalcField.OutputType = OutputType;
CalcField.Formula = Formula;
CalcField.Update();
}
}
}
Adding a DateTime Type column:
private static void AddDateTimeColumn(SPList objSPList, string columnName)
{
if (objSPList != null && columnName != null)
{
if (!objSPList.Fields.ContainsField(columnName))
{
/* add a new choice field */
objSPList.Fields.Add(columnName, SPFieldType.DateTime, false);
objSPList.Update();
/* get the newly added choice field instance */
SPFieldDateTime objSPField = (SPFieldDateTime)objSPList.Fields[columnName];
objSPField.DisplayFormat = SPDateTimeFieldFormatType.DateTime;
objSPField.Update();
/* finally update list */
objSPList.Update();
}
}
}
{
if (objSPList != null && columnName != null)
{
if (!objSPList.Fields.ContainsField(columnName))
{
/* add a new choice field */
objSPList.Fields.Add(columnName, SPFieldType.DateTime, false);
objSPList.Update();
/* get the newly added choice field instance */
SPFieldDateTime objSPField = (SPFieldDateTime)objSPList.Fields[columnName];
objSPField.DisplayFormat = SPDateTimeFieldFormatType.DateTime;
objSPField.Update();
/* finally update list */
objSPList.Update();
}
}
}
Adding a People and Group Type of Column :
private static void AddPeopleandGroupColumnToList(SPWeb objSPWeb, string listName, string columnName)
{
if (objSPWeb != null)
{
SPList objSPList = objSPWeb.Lists[listName];
if (objSPList != null && (!string.IsNullOrEmpty(columnName)))
{
if (!objSPList.Fields.ContainsField(columnName))
{
objSPList.Fields.Add(columnName, SPFieldType.User, false);
SPFieldUser userField = (SPFieldUser)objSPList.Fields[columnName];
userField.LookupField = "Name";
userField.Update();
objSPList.Update();
AddFieldToListDefaultView(objSPList, columnName);
}
}
}
}
{
if (objSPWeb != null)
{
SPList objSPList = objSPWeb.Lists[listName];
if (objSPList != null && (!string.IsNullOrEmpty(columnName)))
{
if (!objSPList.Fields.ContainsField(columnName))
{
objSPList.Fields.Add(columnName, SPFieldType.User, false);
SPFieldUser userField = (SPFieldUser)objSPList.Fields[columnName];
userField.LookupField = "Name";
userField.Update();
objSPList.Update();
AddFieldToListDefaultView(objSPList, columnName);
}
}
}
}
Adding a Choice Field Type of Columns:
private static void AddChoiceColumn(SPList objSPList, string columnName, StringCollection strChoices, string defaultValue)
{
if (objSPList != null && columnName != null)
{
if (!objSPList.Fields.ContainsField(columnName))
{
/* add a new choice field */
objSPList.Fields.Add(columnName, SPFieldType.Choice, false);
objSPList.Update();
/* get the newly added choice field instance */
SPFieldChoice objSPField = (SPFieldChoice)objSPList.Fields[columnName];
/* set field format type i.e. radio / dropdown */
objSPField.EditFormat = SPChoiceFormatType.Dropdown;
/* set the choice strings and update the field */
if (strChoices.Count > 0)
{
for (int iCounter = 0; iCounter < strChoices.Count; iCounter++)
{
objSPField.Choices.Add(strChoices[iCounter]);
}
}
objSPField.DefaultValue = defaultValue;
objSPField.Update();
/* finally update list */
objSPList.Update();
}
}
}
{
if (objSPList != null && columnName != null)
{
if (!objSPList.Fields.ContainsField(columnName))
{
/* add a new choice field */
objSPList.Fields.Add(columnName, SPFieldType.Choice, false);
objSPList.Update();
/* get the newly added choice field instance */
SPFieldChoice objSPField = (SPFieldChoice)objSPList.Fields[columnName];
/* set field format type i.e. radio / dropdown */
objSPField.EditFormat = SPChoiceFormatType.Dropdown;
/* set the choice strings and update the field */
if (strChoices.Count > 0)
{
for (int iCounter = 0; iCounter < strChoices.Count; iCounter++)
{
objSPField.Choices.Add(strChoices[iCounter]);
}
}
objSPField.DefaultValue = defaultValue;
objSPField.Update();
/* finally update list */
objSPList.Update();
}
}
}
Add a LookUp Type of column :
private static void AddLookUpColumn(SPWeb objSPWeb, SPList objSPList, string LookupSourceListName, string columnName, string sourceColumnName)
{
if (objSPWeb != null)
{
/*create lookup type new column called "Lookup Column"*/
SPList objLookupSourceList = objSPWeb.Lists[LookupSourceListName];
if (!objSPList.Fields.ContainsField(columnName))
{
objSPList.Fields.AddLookup(columnName, objLookupSourceList.ID, false);
SPFieldLookup lkp = (SPFieldLookup)objSPList.Fields[columnName];
lkp.LookupField = objLookupSourceList.Fields[sourceColumnName].InternalName;
lkp.Update();
}
}
}
{
if (objSPWeb != null)
{
/*create lookup type new column called "Lookup Column"*/
SPList objLookupSourceList = objSPWeb.Lists[LookupSourceListName];
if (!objSPList.Fields.ContainsField(columnName))
{
objSPList.Fields.AddLookup(columnName, objLookupSourceList.ID, false);
SPFieldLookup lkp = (SPFieldLookup)objSPList.Fields[columnName];
lkp.LookupField = objLookupSourceList.Fields[sourceColumnName].InternalName;
lkp.Update();
}
}
}
Add a Column to particular List View :
private static void AddColumnToView(SPList objSPList string ViewName, string columnName)
{
if (objSPList != null && columnName != null)
{
if (objSPList.Views[ViewName] != null)
{
SPView objSPListCatalogView = objSPList.Views[ViewName];
if (!objSPListView.ViewFields.Exists(columnName))
{
objSPListView.ViewFields.Add(columnName);
objSPListView.Update();
}
objSPList.Update();
}
}
}
{
if (objSPList != null && columnName != null)
{
if (objSPList.Views[ViewName] != null)
{
SPView objSPListCatalogView = objSPList.Views[ViewName];
if (!objSPListView.ViewFields.Exists(columnName))
{
objSPListView.ViewFields.Add(columnName);
objSPListView.Update();
}
objSPList.Update();
}
}
}
In this way you will be able to add all type of columns and then them to view of a SharePoint List. All you need to do is to add these methods in your and pass asked parameter.
No comments:
Post a Comment