Showing posts with label SharePoint Rich Text Column. Show all posts
Showing posts with label SharePoint Rich Text Column. Show all posts

Wednesday, October 16, 2013

SharePoint 2013/2016: 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:




Wednesday, September 11, 2013

SharePoint 2013/2016: HTML Entity Codes in Rich Text List Columns

Recently I ran into a problem attempting to programatically replace characters within a rich text list column from a SharePoint 2010 workflow activity in SharePoint 2013. The replacement characters are identified by being placed in curly brackets (e.g. {ReplaceMe}). The code that used to run in SharePoint 2007 no longer did.

In investigating the stored string value of the list item, I noticed that the brackets were being replaced with ASCII-based HTML entity codes. Therefore the replace statement would never find {ReplaceMe} since it was in the retrieved string as &#123;ReplaceMe&#125;. Since there are possibly hundreds of workflows that already look for values based on the brackets, I needed to actually replace the brackets in what workflow was looking for with the HTML entity codes.

Lucky enough retrieving these values using the object model (SPList, SPQuery, etc.), seems to convert the string back to the friendly version as I also had an email console app that did a similiar find and replace from a rich text list field and that continued to work without any modifications.
However, it still worked because I was using HttpUtility.HtmlDecode when retrieving the value from the list item.


So basically, using the HttpUtility.HtmlDecode will prevent any mishaps but, just in case, I wanted to document the HTML Entity Codes that are used within SharePoint 2013 Rich Text Fields:

CharacterHTML Entity CodeName
{ &#123; Left Curly Bracket
} &#125; Right Curly Bracket
& &amp; Ampersand
< &lt; Less Than
> &gt; Greater Than
: &#58; Colon
" &quot Double Quotes

Matched Content