none
InfoPath 2010 Rich Text field and SharePoint 2010 List with a Rich Text Column

    Question

  • The short question is "Why doesn't a InfoPath 2010 Rich Text field maintain the picture link and text formatting, when content is copied from a SharePoint 2010 List which contains a Rich Text Column?"

    Example:

    (1) Create a SharePoint list, and add an Enhanced Rich Text Column

    Add a couple of items to this list, including paragraph formatting, bold text and embedded pictures.

     

    (2) Create a new InfoPath form, with a data connection to the above-mentioned list.  Bring back the Title and rich text column.

    On the InfoPath form, create two fields: a dropdown list box and a Rich Text field

    For the drop-down list box, get the choices from above-mentioned data connection, setting the Value and Display Name to the data source field Title

    For the drop-down list box, set a rule (type=Action) and rule details (Action:set a fields value; Field: InfoPath Rich Text Field; Value: <data connection>()[Title = current()]

     

    Save and preview the form with InfoPath 2010

    The dropdown list will contain the Title items created in step(1)

    After selecting one of the title values, the InfoPath Rich Text Box will contain the text created in step(1), however the text will lack all formatting, text enhancements and picture links.

     

    If SharePoint 2010 and InfoPath 2010 are supposed to be a no-code or low-code solution, it would seem that this should work.

    Thanks, Andy

     

     

    Friday, December 16, 2011 5:59 PM

Answers

  • Hi Andy,

    This is because that the rich text data coming from SharePoint does not contain the various rich text namespace information which is required in the InfoPath XHTML control to render the formatted rich text. So we need to get rich text data from the SharePoint list and populate it to a rich text control with that data programming.

    Here is the code to get data from SharePoint list and display it in a rich text box control in InfoPath.

     

     

    //Create a Navigator object for the main DOM
    
    XPathNavigator xnMain = this.MainDataSource.CreateNavigator();
    
     
    
    //Open your site
    
    SPSite site = new SPSite("http://server");
    
    SPWeb web = site.OpenWeb();
    
     
    
    //Create a list object for your specific list
    
    SPList list = web.Lists["YourListName"];
    
     
    
    //Value selected from a dropdown that represents the specific list item
    
    string iListItem = xnMain.SelectSingleNode("/my:myFields/my:ListItemNum", this.NamespaceManager).Value;
    
     
    
    //Create an SPListItem object to get just the specific list item
    
    SPListItem item = list.GetItemById(Convert.ToInt32(iListItem));
    
     
    
    //Create a new XMLDocument to store the XML returned from the list item
    
    XmlDocument doc = new XmlDocument();
    
    doc.PreserveWhitespace = true;
    
     
    
    //Load the XML from that list item into the XMLDocument
    
    doc.LoadXml(item.Xml);
    
     
    
    //Create a NamespaceManager object to add the "z" namespace returned from SharePoint
    
    XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
    
    ns.AddNamespace("z", "#RowsetSchema");
    
     
    
    //Create an XMLNode object to get just the rich text column from the list XML in the XmlDocument
    
    XmlNode node = doc.SelectSingleNode("//z:row[@ows_Title='" + item.Title + "']/@ows_txtRichText", ns);
    
     
    
    //Create a string object to store the InnerText value of the rich text
    
    string nodeText = node.InnerText;
    
     
    
    //Now, check for the rich text markup:
    
    //If the InnterText contains the word: div then replace the starting tag with div
    
    //and the XHTML namespace
    
    if (nodeText.Contains("div"))
    
    {
    
    nodeText = nodeText.Replace("<div", "<div xmlns='http://www.w3.org/1999/xhtml'");
    
    }
    
     
    
    //If the InnterText contains 'nbsp' (meaning there are carriage returns) then replace it with
    
    //a div tag and the XHTML namespace
    
    if (nodeText.Contains("nbsp"))
    
    {
    
    nodeText = nodeText.Replace("&nbsp;", "<div xmlns='http://www.w3.org/1999/xhtml'> </div>");
    
    }
    
     
    
    //If the InnterText contains '<ul' (meaning a list) then replace it with
    
    //a ul tag and the XHTML namespace
    
    if (nodeText.Contains("<ul"))
    
    {
    
    nodeText = nodeText.Replace("<ul", "<ul xmlns='http://www.w3.org/1999/xhtml'");
    
    }
    
     
    
    //If the InnterText contains '<p' (meaning a paragraph) then replace it with
    
    //a p tag and the XHTML namespace
    
    if (nodeText.Contains("<p"))
    
    {
    
    nodeText = nodeText.Replace("<p", "<p xmlns='http://www.w3.org/1999/xhtml'");
    
    }
    
     
    
    //Set the rich text control in the InfoPath form to the updated Xml
    
    XPathNavigator xnRTControl = xnMain.SelectSingleNode("/my:myFields/my:field1", this.NamespaceManager);
    
    xnRTControl.InnerXml = nodeText;
    
     
    
    
    

    Or refer to this blog to get the InnerXML of the rich text.
    http://www.thekickboard.com/archive/2011/07/21/pulling-a-rich-text-field-from-a-sharepoint-list-into-an-infopath-2010-form.aspx

     

    Best regards.


    Emir Liu

    TechNet Community Support


    Monday, December 19, 2011 11:59 AM

All replies

  • Hi Andy,

    This is because that the rich text data coming from SharePoint does not contain the various rich text namespace information which is required in the InfoPath XHTML control to render the formatted rich text. So we need to get rich text data from the SharePoint list and populate it to a rich text control with that data programming.

    Here is the code to get data from SharePoint list and display it in a rich text box control in InfoPath.

     

     

    //Create a Navigator object for the main DOM
    
    XPathNavigator xnMain = this.MainDataSource.CreateNavigator();
    
     
    
    //Open your site
    
    SPSite site = new SPSite("http://server");
    
    SPWeb web = site.OpenWeb();
    
     
    
    //Create a list object for your specific list
    
    SPList list = web.Lists["YourListName"];
    
     
    
    //Value selected from a dropdown that represents the specific list item
    
    string iListItem = xnMain.SelectSingleNode("/my:myFields/my:ListItemNum", this.NamespaceManager).Value;
    
     
    
    //Create an SPListItem object to get just the specific list item
    
    SPListItem item = list.GetItemById(Convert.ToInt32(iListItem));
    
     
    
    //Create a new XMLDocument to store the XML returned from the list item
    
    XmlDocument doc = new XmlDocument();
    
    doc.PreserveWhitespace = true;
    
     
    
    //Load the XML from that list item into the XMLDocument
    
    doc.LoadXml(item.Xml);
    
     
    
    //Create a NamespaceManager object to add the "z" namespace returned from SharePoint
    
    XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
    
    ns.AddNamespace("z", "#RowsetSchema");
    
     
    
    //Create an XMLNode object to get just the rich text column from the list XML in the XmlDocument
    
    XmlNode node = doc.SelectSingleNode("//z:row[@ows_Title='" + item.Title + "']/@ows_txtRichText", ns);
    
     
    
    //Create a string object to store the InnerText value of the rich text
    
    string nodeText = node.InnerText;
    
     
    
    //Now, check for the rich text markup:
    
    //If the InnterText contains the word: div then replace the starting tag with div
    
    //and the XHTML namespace
    
    if (nodeText.Contains("div"))
    
    {
    
    nodeText = nodeText.Replace("<div", "<div xmlns='http://www.w3.org/1999/xhtml'");
    
    }
    
     
    
    //If the InnterText contains 'nbsp' (meaning there are carriage returns) then replace it with
    
    //a div tag and the XHTML namespace
    
    if (nodeText.Contains("nbsp"))
    
    {
    
    nodeText = nodeText.Replace("&nbsp;", "<div xmlns='http://www.w3.org/1999/xhtml'> </div>");
    
    }
    
     
    
    //If the InnterText contains '<ul' (meaning a list) then replace it with
    
    //a ul tag and the XHTML namespace
    
    if (nodeText.Contains("<ul"))
    
    {
    
    nodeText = nodeText.Replace("<ul", "<ul xmlns='http://www.w3.org/1999/xhtml'");
    
    }
    
     
    
    //If the InnterText contains '<p' (meaning a paragraph) then replace it with
    
    //a p tag and the XHTML namespace
    
    if (nodeText.Contains("<p"))
    
    {
    
    nodeText = nodeText.Replace("<p", "<p xmlns='http://www.w3.org/1999/xhtml'");
    
    }
    
     
    
    //Set the rich text control in the InfoPath form to the updated Xml
    
    XPathNavigator xnRTControl = xnMain.SelectSingleNode("/my:myFields/my:field1", this.NamespaceManager);
    
    xnRTControl.InnerXml = nodeText;
    
     
    
    
    

    Or refer to this blog to get the InnerXML of the rich text.
    http://www.thekickboard.com/archive/2011/07/21/pulling-a-rich-text-field-from-a-sharepoint-list-into-an-infopath-2010-form.aspx

     

    Best regards.


    Emir Liu

    TechNet Community Support


    Monday, December 19, 2011 11:59 AM
  • Hi Emir,

    Does it mean we have no "no-code" solution, using InfoPath 2010, to solve this issue?

    Thanks.

    Tuesday, January 24, 2012 5:55 PM
  • I am looking for a "no code" solution to this issue as well!

    Thanks!

    Wednesday, February 01, 2012 12:45 AM
  • Emir -

    Is it possible to code using the Data Connection rather then use SPLIST and SPWEB?

    ***************************************

    Or refer to this blog to get the InnerXML of the rich text.

    http://www.thekickboard.com/archive/2011/07/21/pulling-a-rich-text-field-from-a-sharepoint-list-into-an-infopath-2010-form.aspx

     Does someone have an example of this technique? Does the technique replace Emir's above?

    Thanks,


    Chris


    Tuesday, July 10, 2012 6:09 PM
  • If you just want to display the formatted text on your InfoPath form, you may try this:

      • Add a secondary data source to your form (SharePoint list) which contains the formatted text
      • Add this secondary data source to your form as a repeating section
      • Remove all fields and labels from the repeating section except for the richt text field
      • Add a rule to the repeating section to only display the list item of the secondary data source that contains the formatting text.

    Now the formatted text from the SharePoint list is displayed on your InfoPath form.

    Hope this helps.

    Monday, September 03, 2012 7:36 AM
  • Hello Rico,

    I cannot see column (multiple line of text) while adding secondary connection. It seems, we cannot see multiple line of text type column. 

    Thanks, Chintan


    • Edited by Chintan123 Thursday, July 11, 2013 1:29 PM change
    Thursday, July 11, 2013 1:29 PM