Tuesday, June 30, 2009
Append new line to SPFieldMultiLineText in SharePoint
By Hristo Yankov
Do you have a multiline text box field in your SharePoint site and ever wondered how to append a new line to it? Are you confused whether you should use \r\n, Environment.NewLine or perhaps even #xD #xA to mark a line break? Allow me to save you the time and give you a straight answer.
The first thing you have to do is convert the SPListItem that represents your field into SPFieldMultiLineText.
SPFieldMultiLineText multilineField
= item.Fields.GetField(COLUMN_NAME) as SPFieldMultiLineText;
Then you can use its GetFieldValueAsHtml method to get the value in the form of HTML (which will preserve the multiple lines):
string text = multilineField.GetFieldValueAsHtml(item[COLUMN_NAME], item);
You should use '<br/>' as a line break:
string sharePointNewLine = "<br/>";
text = text
+ sharePointNewLine + sharePointNewLine
+ "Appended Text:" + sharePointNewLine
+ "This text is appended on a new line!";
And after that you just update your item:
this.Web.AllowUnsafeUpdates = true;
item[COLUMN_NAME] = text;
item.Update();
this.Web.AllowUnsafeUpdates = false;
So if the original text in the field was "Test test test", after executing this code, it would read:
Test test test
Appended Text:
This text is appended on a new line!
Simple? As long as you remember that text should be retrieved as HTML and you should use '<br/>' as a line break, yes.
Here is the full code snippet, ready to use:
public void AppendTextToMultiline(SPListItem item)
{
string COLUMN_NAME = "Multiline Column Name";
string sharePointNewLine = "<br/>";
SPFieldMultiLineText multilineField = item.Fields.GetField(COLUMN_NAME) as SPFieldMultiLineText;
if (multilineField != null)
{
// Get the field value as HTML
string text = multilineField.GetFieldValueAsHtml(item[COLUMN_NAME], item);
// Append the text
text = text
+ sharePointNewLine + sharePointNewLine
+ "Appended Text:" + sharePointNewLine
+ "This text is appended on a new line!";
this.Web.AllowUnsafeUpdates = true;
item[COLUMN_NAME] = text;
item.Update();
this.Web.AllowUnsafeUpdates = false;
}
}
Hope this helps!
Do you have a multiline text box field in your SharePoint site and ever wondered how to append a new line to it? Are you confused whether you should use \r\n, Environment.NewLine or perhaps even #xD #xA to mark a line break? Allow me to save you the time and give you a straight answer.
The first thing you have to do is convert the SPListItem that represents your field into SPFieldMultiLineText.
SPFieldMultiLineText multilineField
= item.Fields.GetField(COLUMN_NAME) as SPFieldMultiLineText;
Then you can use its GetFieldValueAsHtml method to get the value in the form of HTML (which will preserve the multiple lines):
string text = multilineField.GetFieldValueAsHtml(item[COLUMN_NAME], item);
You should use '<br/>' as a line break:
string sharePointNewLine = "<br/>";
text = text
+ sharePointNewLine + sharePointNewLine
+ "Appended Text:" + sharePointNewLine
+ "This text is appended on a new line!";
And after that you just update your item:
this.Web.AllowUnsafeUpdates = true;
item[COLUMN_NAME] = text;
item.Update();
this.Web.AllowUnsafeUpdates = false;
So if the original text in the field was "Test test test", after executing this code, it would read:
Test test test
Appended Text:
This text is appended on a new line!
Simple? As long as you remember that text should be retrieved as HTML and you should use '<br/>' as a line break, yes.
Here is the full code snippet, ready to use:
public void AppendTextToMultiline(SPListItem item)
{
string COLUMN_NAME = "Multiline Column Name";
string sharePointNewLine = "<br/>";
SPFieldMultiLineText multilineField = item.Fields.GetField(COLUMN_NAME) as SPFieldMultiLineText;
if (multilineField != null)
{
// Get the field value as HTML
string text = multilineField.GetFieldValueAsHtml(item[COLUMN_NAME], item);
// Append the text
text = text
+ sharePointNewLine + sharePointNewLine
+ "Appended Text:" + sharePointNewLine
+ "This text is appended on a new line!";
this.Web.AllowUnsafeUpdates = true;
item[COLUMN_NAME] = text;
item.Update();
this.Web.AllowUnsafeUpdates = false;
}
}
Hope this helps!
1 comment:
This does not work.
Post a Comment