Programmatically create a list and a SPFieldMultiLineText with RichText

dimanche 20 octobre 2013

First, you need to create your List :

Code:
SPWeb web = //[Get your web]
SPList MyList = Utilities.CreateList(web, Title, Key, SPListTemplateType.GenericList);

Then you do the following to create the SPFieldMultiLineText


Code:
// Add RichText column
string htmlFieldName = Utilities.AddField(MyList, SPFieldType.Note, FieldName, false, true);
SPFieldMultiLineText htmlField = MyList.Fields[FieldName] as SPFieldMultiLineText;
htmlField.RichText = true;
htmlField.RichTextMode = SPRichTextMode.FullHtml;
htmlField.Update();

You can even rename the original Title column


Code:
// Rename the original Title column
SPField TitleField = MyList.Fields[SPBuiltInFieldId.Title];
TitleField.Title = anOtherName;

Finally, we run our code snippet into an event receiver


Code:
[Guid("xxx")]
public class MybEventReceiver : SPFeatureReceiver
 {
 public override void FeatureActivated(SPFeatureReceiverProperties properties)
  {
  SPWeb web = //[Get your web]
  SPList MyList = Utilities.CreateList(web, Title, Key, SPListTemplateType.GenericList);
  // Rename the original Title column
  SPField TitleField = MyList.Fields[SPBuiltInFieldId.Title];
  TitleField.Title = anOtherName;
  // Add RichText column
  string htmlFieldName = Utilities.AddField(MyList, SPFieldType.Note, FieldName, false, true);
  SPFieldMultiLineText htmlField = MyList.Fields[FieldName] as SPFieldMultiLineText;
  htmlField.RichText = true;
  htmlField.RichTextMode = SPRichTextMode.FullHtml;
  htmlField.Update();
  }
 }

Actually, this could run for SharePoint 2010 too. 

Copyright @ 2013 Nizar Joober - I'm SharingPoints. Designed by Templateism

Top articles