3

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.

7
  • Is your field set to "rich text" or "plain text"?
    – PhilFancy
    Jul 10, 2018 at 5:15
  • Did you try FieldValuesAsText Jul 10, 2018 at 5:41
  • its richtext @PhilFancy
    – Dheeraj
    Jul 10, 2018 at 5:44
  • 3
    It will return as plain text. What do you mean by preserving the format and removing the html tags at the same time. Jul 10, 2018 at 5:54
  • 1
    can you share some code that you are using to fetch and bind data? Those bullet points in your data are also HTML tags, namely ul and li, so those are needed when you want to present the data in UI as well Jul 10, 2018 at 9:04

3 Answers 3

1

You 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();

1
  • I have already tried this...it eliminates all the bullet points in the text @Bharat Khunti
    – Dheeraj
    Jul 10, 2018 at 8:10
1

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"]);
1

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.

2
  • Hi, i am using angularjs too to bind the data and I have used ng-bind-html to bind the data, But when I try to limit the number of characters using limitTo, it's not working properly..Thanks
    – Dheeraj
    Jul 11, 2018 at 5:35
  • LimitTo won't work with html, since it essentially truncates the array, which would chop the closing tags in the case of html. What you could do is wrap your bound content inside of another div and set an explicit height/width on it. Then add overflow rules to either hide, scroll, or perhaps add an ellipsis. This is all done in css.
    – Chad
    Jul 11, 2018 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.