Wednesday, October 16, 2013

SharePoint 2013: Adding a Rich Text Column to a List Using CSOM

When you add a rich text column to a list from the SharePoint UI, you select Multiple lines of text as the type of information:


Towards the bottom, you have two type of text options, Plain text or Enhanced rich text:

After you create the column and go back in to edit the properties, you now get a third "in-between" Rich text option:


I needed to add a rich text column programatically to a list using the client side object model (CSOM). I found general guidelines for adding a field to a list here on MSDN.

Based on that post, I followed suit and constructed my code:

Field styleField = list.Fields.AddFieldAsXml("<Field DisplayName='Style' Type='Note' />", true, AddFieldOptions.DefaultValue);

FieldMultiLineText fieldMultiLineText = context.CastTo<FieldMultiLineText>(styleField);
fieldMultiLineText.RichText = true;
fieldMultiLineText.AllowHyperlink = true;
fieldMultiLineText.UpdateAndPushChanges(true);
list.Update();
context.ExecuteQuery();

I knew what the real type of Field the object was so I casted it to FieldMultiLineText. This allowed me to set the RichText property to true. However, upon generation, the settings of the column where just the regular Rich Text:


This was actually what I needed but I thought, how do you create one as Enhanced rich text? There is a RichTextMode property but it is not exposed in a FieldMultiLineText object. After trying several different things, I just decided to read the properties of a created Enhanced rich text column and use the SchemaXML to generate the new field:

Field rtfField = list.Fields.AddFieldAsXml("<Field DisplayName=\"Enhanced RTF\" Type=\"Note\" RestrictedMode=\"TRUE\" RichText=\"TRUE\" RichTextMode=\"FullHtml\" RowOrdinal=\"0\" Required=\"FALSE\" EnforceUniqueValues=\"FALSE\" Indexed=\"FALSE\" NumLines=\"6\" IsolateStyles=\"TRUE\" AppendOnly=\"FALSE\" />"
, true, AddFieldOptions.DefaultValue);

rtfField.UpdateAndPushChanges(true);list.Update();
context.ExecuteQuery();



RestrictedMode=\"TRUE\" RichText=\"TRUE\" RichTextMode=\"FullHtml\"
I believe that these properties are the key to creating it properly. I did not perform any casts as it seemed that was working against me. Once I just used the Field object and the XML, all was good:




 

7 comments:

  1. Why does my multiple lines of text type not show the Enhanced rich text option in SharePoint 2013?

    ReplyDelete
    Replies
    1. Not sure. I would have to look.

      Delete
    2. Not sure. I would have to look.

      Delete
    3. This might be because you are trying to add that in a document library for which the option would not be Shown in the Sharepoint page and instead you would have to try to make the change using the SharePoint Designer.

      Delete
    4. This would be because you are trying to add this in a document library for which you need to make the changes from the SharePoint Designer.

      Delete
  2. Hi, I have a multiple lines of text column with rich text enabled. However when we edit the properties of the item in the library, the column can only be completed in plain text - there is no rich text editor. Can you help please? Tracey

    ReplyDelete
    Replies
    1. Did you try different browsers? IE vs. Chrome?

      Delete