Anything Microsoft...

Blog about the Microsoft technologies

InfoPath - Programatically set values for the Rich Text field from SharePoint List

InfoPath - Programatically set values for the Rich Text field from SharePoint List

  • Comments 1

Scenario:

-  Populating the rich text field content from SharePoint List to the InfoPath form's Rich Text field

Problem:

-  The InfoPath form's rich text field won't recognize the contents read from SharePoint list's rich text field as valid XML. The SharePoint list's rich text field storest the raw html (malformed) which is not compatible with XML.

Solution:

-  The SharePoint list's rich text field contents needs to be parsed/converted to a well-formed HTML so that the XML can recognize without any validation errors.

-  The malformed HTML can be converted to a well-formed XML compatible using the codeplex tool HTMLAgilityPack.

Code Snippet:

// Instantiate the HtmlDocument instance from HtmlAgilityPack

HtmlDocument commentsDoc = new HtmlDocument();

commentsDoc.LoadHtml(richTextComment);

commentsDoc.OptionOutputAsXml =

true;

 

 

HtmlNodeCollection nodes = commentsDoc.DocumentNode.SelectNodes("//div[@class!='']");

 

 

if (nodes != null)

{

 

 

foreach (HtmlNode node in nodes)

{

node.Attributes.Remove(

"class");

}

}

nodes =

null;

commentsNode.InnerXml =

"<div xmlns='http://www.w3.org/1999/xhtml'>" + commentsDoc.DocumentNode.InnerHtml + "</div>";

Leave a Comment
  • Please add 4 and 5 and type the answer here:
  • Post
  • Excellent Post. Worked like a dream.

    Many Thanks

Page 1 of 1 (1 items)