Read Enhanced Rich Text Values in SharePoint

Leave a Comment
Hello Everyone,

In a followin post we will see how to read Enhanced Rich Text value from SharePoint List/Library.
If reading this Column Value in regular way you will get the output in the combination of some html code like <div>.. </div>
public void ReadMultiLines()
        {
            try
            {
                using (SPSite oSite = new SPSite("http://localhost"))
                {
                    using (SPWeb oWeb = oSite.OpenWeb())
                    {
                        SPList list = oWeb.Lists.TryGetList("SampleList");
                        if (list != null)
                        {
                            SPListItemCollection _coll = list.GetItems();
                            foreach (SPListItem item in _coll)
                            {
                                Console.WriteLine(item["Title"].ToString());
                                Console.WriteLine(item["Column1"].ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            { 
            
            }
        }
Out put will be looks as follow,
To get the Rich Text Value in plain we need to write code as below.

if (!string.IsNullOrEmpty(Convert.ToString(item["Column1"])))
{
SPFieldMultiLineText plainBody = item.Fields.GetField("Column1") as SPFieldMultiLineText;
 if (plainBody != null)
  {
   string body = plainBody.GetFieldValueAsText(item["Column1"]);
   Console.WriteLine(body);
  }
}
Now,We can see the output as below,


Related Post

0 comments:

Post a Comment