Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
SPQuery Hacks Part 1: InfoPathUse SHIFT+ENTER to open the menu (new window).
Adventures in Excel Services 2010Use SHIFT+ENTER to open the menu (new window).
What Is The Easiest Way To Mess Up SharePoint? Use SHIFT+ENTER to open the menu (new window).
Redirect SharePoint Navigation - NYC SDUG Quick DipUse SHIFT+ENTER to open the menu (new window).
SPQuery Hacks Part 2: WorkflowsUse SHIFT+ENTER to open the menu (new window).
Upgrading SharePoint 2007 RTM to SharePoint 2010 Use SHIFT+ENTER to open the menu (new window).
Using JQuery to add charts to your Data ViewsUse SHIFT+ENTER to open the menu (new window).
Quick Rundown: Multi-line Text ColumnsUse SHIFT+ENTER to open the menu (new window).
Document Previews Won’t Open In FAST Search Using HTTPSUse SHIFT+ENTER to open the menu (new window).
Sleazy Reporting: SharePoint 2010 ListsUse SHIFT+ENTER to open the menu (new window).
Helpful Debugging with SharePoint C Sharp CodeUse SHIFT+ENTER to open the menu (new window).
Configuring Remote Blob Storage in SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Getting E-mail to Work in Your SharePoint 2010 Dev EnvironmentUse SHIFT+ENTER to open the menu (new window).
Sleazy Reporting: SharePoint 2010 External Content TypesUse SHIFT+ENTER to open the menu (new window).
Programmatically Dealing With Potential Multi-Select ColumnsUse SHIFT+ENTER to open the menu (new window).
Quick Rundown: Multi-line Text Columns
By: Arjun Chakraborty
 

Introduction:

This article quickly goes over the programmatic handling of multi-line text columns.  Multi-line columns are useful within SharePoint because they allow a large quantity of descriptive texted to be stored as metadata, while only using a single column.  They can hold specifications, use conditions, history, notes or even pictures.  However, the more extensive the functionality of the text column, the more difficult the field will become to handle.  When creating a multi-line text column, you will find the following options:

The_SharePoint_Blog_Multi_line_Text_Columns

The “Number of lines for editing:” number box indicates how many lines a SharePoint user will have to type text into this field, when editing the corresponding item.  It does not limit how many lines of text this field can hold.  If this field contains more than 6 lines of text, a scroll bar will be provided to the SharePoint user to look through the entire text.

In order to demonstrate the programmatic implication of the three radio buttons, the rest of this article will go as follows:

1)      First, I will briefly explain the benefits of each option.

2)      Then, I will show you what they look like when being populated and when they are side by side.

3)      Afterwards, I’ll show you the two ways to capture the field’s data, and the code surrounding the data acquisition.  I will also show you what the acquired data looks like for each radio option-data acquisition combination.

4)      Finally, I will show you how to populate these fields.

The Benefits:

Plain text is pretty simple.  While it will not allow simple formatting, such as underlining or bolding, it will however predictably provide Environment.Newline comparisons.  This can be quite helpful if rich formatting is not necessary.

Rich text and enhanced rich text are very similar, while obviously the enhanced rich text has more options, such as posting pictures.  I have never found a reason to not use Enhanced rich text if I’m going to use Rich text anyway.  The only reason you might want to use rich text instead of enhanced rich text is if you want to forbid the posting of pictures.  One obvious reason for this is, the height of each row in SharePoint will become quite large for each list item.

How it Looks:

Here is how the 3 fields look when the user is filling them out.  Notice how the user only has 6 lines to view text, but there is also a scroll bar so the user can continue to input even more lines of text.

The_SharePoint_Blog_Multi_line_Text_Columns

Furthermore, here is how the fields look when side by side in a SharePoint List.

The_SharePoint_Blog_Multi_line_Text_Columns

The_SharePoint_Blog_Multi_line_Text_Columns

The_SharePoint_Blog_Multi_line_Text

Programmatic Handling:

Finally, let’s talk about how to handle them in C#.  In my experience, I’ve found multi-line text columns to be more of a nuisance than even look up columns.  Capturing their information is rather counter intuitive and difficult to remember.  This is primarily why I wrote this article.  To capture the text within a multi-line text column, you must first declare a SPFieldMultiLineText object.

SPFieldMultiLineText plainText = item.Fields.GetField("PlainText") as SPFieldMultiLineText;

Notice we cannot simply use item[“PlainText”].

To display this text, we can use one of two methods:

1)      plainText.GetFieldValueAsText(object value)

2)      plainText.GetFieldValueAsHTML(object value)

To make this code even more counter-intuitive, the parameters for both of these methods are a single object with no description.  What actually goes into these parameters is item["PlainText"].  That is, the splistitem from which this column value is coming from, and the column itself’s name.

To show you what these methods return, here is the code that I used in a console application.  It is wrapped in a using statement with a SPWeb.  

//declare necessary variables.

SPList list = web.Lists["Multi-line Text"];

SPListItem item = list.Items[0];

int count;

 

//get info from multi-line column

SPFieldMultiLineText mlt = item.Fields.GetField(FIELD_NAME) as SPFieldMultiLineText;

string asText = mlt.GetFieldValueAsText(item[FIELD_NAME]);

string asHtml = mlt.GetFieldValueAsHtml(item[FIELD_NAME]);

 

//display field value as text

Console.WriteLine("Plain text multi-line column: get field as text: " +

    Environment.NewLine + Environment.NewLine + asText);

 

count = 0;

for (int i = 0; i < asText.Length - 1; i++)

{

    if (asText.Substring(i, 2) == Environment.NewLine)

        count++;

}

Console.WriteLine("Total Environment.NewLine registered: " + count);

Console.WriteLine(Environment.NewLine + "----------------------");

 

//display field value as html

Console.WriteLine("Plain text multi-line column: get field as html: " +

    Environment.NewLine + Environment.NewLine + asHtml);

 

count = 0;

for (int i = 0; i < asHtml.Length - 1; i++)

{

    if (asHtml.Substring(i, 2) == Environment.NewLine)

        count++;

}

Console.WriteLine("Total Environment.NewLine registered: " + count);

Console.ReadLine();

Results:

Here are the results for FIELD_NAME = "PlainText":

The_SharePoint_Blog_Multi_line_Text_Columns

Here are the results for FIELD_NAME = "RichText":

The_SharePoint_Blog_Multi_line_Text_Columns

Finally, here are the results for FIELD_NAME = "EnhancedRichText":

The_SharePoint_Blog_Multi_line_Text_Columns

Now you might say, well the NewLine counts are accurate, so what gives?  Why are rich texts more difficult to programmatically handle?  Because, while users might make ample use of pictures, lists and formatting, capturing such details will be impossible with .GetFieldValueAsText, and .GetFieldValueAsHtml will be riddled with unpredictable tags.

Populating Multi-line Text Fields:

Lastly, how do we populate Multi-Line Text Fields?  Well, this is actually a lot simpler.  Simply use a code such as:

item["MultiLine"] = "<u>Line One</u>." + Environment.NewLine +

"<i>Line Two.</i>" + "<br>" + "<b>Line Three.</b>";

item.Update();

In this case, “MultiLine” is a rich multi-line column.  The results are as follows:

The_SharePoint_Blog_Multi_line_Text_Columns

Notice, the Environment.NewLine did not register.

If “MultiLine” is a plain text multi-line column then we instead get the following result from the same code:

The_SharePoint_Blog_Multi_line_Text_Columns

Notice that the html tags are now meaningless.  Therefore, knowing if the column is rich text of plain text is quite important.  To find out during runtime, simply use this:

SPFieldMultiLineText mlt = …
bool isRichText = mlt.RichText;

By: Arjun Chakraborty

        

Comments

Robin_bird

 Do you know of any way to allow these rich text fields to be accessed by an info path form . The form has rich text boxes to capture the multi line data, but these are not seen in the secondary data connection to the appropriate list
Thanks
at 6/17/2010 1:57 PM

Robin_bird

 Do you know of any way to allow these rich text fields to be accessed by an info path form . The form has rich text boxes to capture the multi line data, but these are not seen in the secondary data connection to the appropriate list
Thanks
at 6/17/2010 2:01 PM

Rerto Jordans

I'm very thankful to the author for posting such an amazing development post. Continuing to the post, This is a fantastic survey, very nice write up,Thanks you for sharing.
at 7/2/2010 4:45 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  SharePoint Resources
  Business Intelligence Resources
  Upcoming Webinars



©2009 Gig Werks. All rights reserved. Privacy Policy