Search code examples
c#htmlsharepoint-2013web-parts

Get SharePoint rich text field value in C#


In case rich text field contains new lines, bold, italic, formatting or any color, the first line of my document, which is the value of the rich text field is being exported nicely (with the same formatting, that is in item), and the rest code is not being exported in the word document. However, in case I open the exported file with notepad, I can see that the whole code is being exported, but in word is visible only the first line.

I try to export SharePoint list in a word document. I have several rich text fields in list items. I am able to export everything I need in the word document if the rich text field contains only one line simple string, everything works fine.

strHTMLContent.Append("<table style=margin-top: 8px; border=0 frame=hsides rules=rows cellpadding=0 cellspacing=0 width=100%>".ToString());
        //Looping through each list item in  the list
        foreach (SPListItem oListItem in collListItems)
          {
             count1++;

         strHTMLContent.Append("<tr><td>"+count1+". "+ oListItem.GetFormattedValue("DocName_Arm") + "</td></tr>");
          }
        strHTMLContent.Append("</table>".ToString());


// Giving path for saving word documents
        SPList word = (SPDocumentLibrary)oSiteCollection.AllWebs["My URL"].Lists["Mylist"];
        SPListItem item1 = word.Items[0];
        string destUrl = word.RootFolder.Url + "/" + "MyWord7" + ".doc";
        // Encoding the document to UTF8 format
        byte[] byteArray = Encoding.UTF8.GetBytes((strHTMLContent.ToString()));
        SPFile destFile = word.RootFolder.Files.Add(destUrl, byteArray, true);`

No error, I get value of Docname_Arm, in the same formatting that is in the list item, the other code (I have another HTML table after the code described) is not working in word document(only if Docname_Arm has some formatting, if not works fine), but is visible in notepad


Solution

  • You could use GetFieldValueAsHtml to get the html snippet for rich text field:

     using (SPSite site = new SPSite("http://sp/sites/jerry"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    var list = web.Lists.TryGetList("TestList");
                    var item = list.GetItemById(1);
                    var eventDescField = list.Fields.GetFieldByInternalName("Parameters");
                    var eventDesc = item[eventDescField.Id];
                    var eventDescText = eventDescField.GetFieldValueAsHtml(eventDesc);
                    SPList word = (SPDocumentLibrary)web.Lists["Jerrydoc"];
    
                    string destUrl = word.RootFolder.Url + "/" + "MyWord8" + ".doc";
                    // Encoding the document to UTF8 format
                    byte[] byteArray = Encoding.UTF8.GetBytes((eventDescText.ToString()));
                    SPFile destFile = word.RootFolder.Files.Add(destUrl, byteArray, true);
                }
            }
    

    enter image description here

    This is the list item data for rich text field, a table with some font color text:

    enter image description here

    enter image description here