Am trying to retrieve data from Enhanced rich multiline text fields in SharePoint 2013, but html tags are retrieved along with the data. I need to preserve the format of the data but without html tags. Any solution?
Thanks in advance.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityYou can use below code to Get Plain Text from Rich Text box Value
$.ajax({
url: requestURL,
type: "GET",
headers: { "ACCEPT": "application/json;odata=verbose" },
success: function(data){
var plainText= $(data.d.MultilineTextBox).text();
console.log(plainText);
},
error: function(data){
console.log(JSON.stringify(data));
}
});
Note: Here we have used simple jQuery method to get plain text from HTML.
Like, $("Div#Content").text();
Didn't test, but you can probably use
SPFieldMultiLineText.GetFieldValueAsText
Method :
SPListItem yourItem = ....; // obtain the SPListItem with any method
SPFieldMultiLineText field = (SPFieldMultiLineText)yourItem.Fields["Content"];
string text = field.GetFieldValueAsText (yourItem["Content"]);
Unfortunately the way "bullets" and other formatting are added to the Rich Text field are via HTML. If you think about it, how would such bullets be represented in "plain text". I suppose you could do a search/replace for something like <li>
and swap it with " - " or " >>" or whatever, but that format is completely custom. There's nothing it could infer automatically.
"Plain Text" = no formatting, just letters, numbers, special characters, carriage returns, etc.
"Rich Text" = unordered lists, heading tags, bold font, italics, <br>
, etc.
You'll need to customize the format using rules to swap html tags with what you want to be displayed instead of those tags.
ALTERNATIVELY!
Rather than switch from html back to plain text, keep using the rich html. Find a way to bind the html. For example, I use angularjs and typically you bind things using ng-bind="somePlainText" but if you have html you instead use ng-bind-html="someHtmlText"
The html returned by SharePoint might not be "clean" though, so if you go this route you might still end up having to remove css and other undesired formatting.
FieldValuesAsText
ul and li
, so those are needed when you want to present the data in UI as well