2008-08-18

Programmatically creating a Rich Text Field in a SharePoint List

Sounds easy enough, but it wasn't immediately obvious.



The below creates a list, adds a Rich Text field with full HTML and adds it to the default view.




27   SPWeb oWeb = null;


28   oWeb = oSite.RootWeb;


29   Guid gList;


30   SPList oList = null;


31   gList = oWeb.Lists.Add("My List", "Some description", SPListTemplateType.GenericList);


32   oWeb.Update();


33   oList = oWeb.Lists[gList];


34   SPFieldMultiLineText oFldBody = (SPFieldMultiLineText)oList.Fields[oList.Fields.Add("Body", SPFieldType.Note, true)];


35   oFldBody.Description = "Replaceable Parameters: {password}, {userid}, {usergroups}, {usersite}, {siteadmin}";


36   oFldBody.RichText = true;


37   oFldBody.RichTextMode = SPRichTextMode.FullHtml;


38   oFldBody.Update();


39   oList.Update();


40 


41   SPView oView = oList.DefaultView;


42   SPViewFieldCollection oViewFields = oView.ViewFields;


43   oViewFields.Add(oFldBody);


44   oView.Update();


2 comments :

  1. Exactly what I was looking for! Thank you

    ReplyDelete
  2. Excellent!!!! That's what I was looking for

    Many Thanks!!!!

    Jaydev

    ReplyDelete