1

I am making application page that add, edit, delete the list items.

Now I want to make Edit page, and I retrieved the Title and Body value.

But in body value, it returned like this.

<div class="ExternalClassD0DCC2B7E533459DA80922D67FE8A7A4"><p>Test 2​</p></div>

I don`t want to see div tag. How can I do that?

My code is below.

private void DisplaySelectedListItem()
{
    string siteUrl = SPContext.Current.Web.Url;

    int id = int.Parse(Request.QueryString["ID"]);

    using (SPSite spSite = new SPSite(siteUrl))
    {
       using (SPWeb web = spSite.OpenWeb())
       {
          SPList list = web.GetListFromUrl("Lists/List/AllItems.aspx");
          SPListItem item = list.GetItemById(id);
          txtTitle.Text = item["Title"].ToString();
          txtBody.Text = item["Body"].ToString();
        }
     }
}
  • 1
    I am a bit unsure about C# but there is something called FieldValuesAsText and if you Google it you can find some documentation. That is a collection of all the fields as keys and their values are plaintext values. It might contain the non-html version of your rich text field contents. Worth giving it a try I guess. :) – Alex Pedersen Jan 9 '17 at 13:56
  • 3
    If you use a RichTextField for the body you should keep that functionality by using a richtextcontrol too. If you dont wan't the user to be able to input html you just have to edit the field settings and change it to plain text. – Mx. Jan 9 '17 at 13:56
  • Thank you Alex Pedersen and Mx. Your comments are helpful to solve my problem! :) – Ryden Choi Jan 10 '17 at 1:48
1

Try below code if you want to keep using the body as rich text field:

private void DisplaySelectedListItem()
{
    string siteUrl = SPContext.Current.Web.Url;

    int id = int.Parse(Request.QueryString["ID"]);

    using (SPSite spSite = new SPSite(siteUrl))
    {
       using (SPWeb web = spSite.OpenWeb())
       {
          SPList list = web.GetListFromUrl("Lists/List/AllItems.aspx");
          SPListItem item = list.GetItemById(id);
          txtTitle.Text = item["Title"].ToString();
          SPFieldMultiLineText field = (SPFieldMultiLineText)item.Fields["Body"]; 
          txtBody.Text = field.GetFieldValueAsText(item["Body"]);         
        }
     }
}

Reference - SPField.GetFieldValueAsText

Or as using SPUtility as below:

private void DisplaySelectedListItem()
{
    string siteUrl = SPContext.Current.Web.Url;

    int id = int.Parse(Request.QueryString["ID"]);

    using (SPSite spSite = new SPSite(siteUrl))
    {
       using (SPWeb web = spSite.OpenWeb())
       {
          SPList list = web.GetListFromUrl("Lists/List/AllItems.aspx");
          SPListItem item = list.GetItemById(id);
          txtTitle.Text = item["Title"].ToString();
          string bodyValue = item["Body"].ToString();
          txtBody.Text = SPHttpUtility.ConvertSimpleHtmlToText(bodyValue, bodyValue.Length);       

        }
     }
}

For the above code, you need to add Microsoft.SharePoint.Utilities reference.

Reference - SPHttpUtility.ConvertSimpleHtmlToText

Else

In your list, make sure that the Multiple lines of text column setting is set to plain text

  • Go to list setting
  • click on the column name.
  • Beside Additional Column Settings, Check Plain text

enter image description here

This will remove the rich text and automatically it becomes plain text. If you do this, no code change will be required.

  • Thank you Gautam Sheth! It works perfectly. And also your column setting tip is really helpful!!! :) – Ryden Choi Jan 10 '17 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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