views:

31

answers:

3

Hi,

I need to edit the value in an SP Rich Text field via the WinForms web browser control. Most other controls (input tags) are easy to get and we can change the value quite simply. However, not so simple with Rich Text. I headed over to: http://blog.drisgill.com/2007_05_01_archive.html

and got some ideas. At first, I tried creatign a javascript function and adding it to the page:

function GetRichTextRange(strBaseElementID)
    var docEditor=RTE_GetEditorDocument(strBaseElementID);
    if (docEditor == null)
    { return; }
    var selection = docEditor.selection;
    var range = selection.createRange();
    return range;
}

However, every time I call this, I always get a null value back. So I tried this instead:

object docEditor = document.InvokeScript("RTE_GetEditorDocument", new object[] { fieldName });
IHTMLDocument2 doc = (IHTMLDocument2)docEditor;
IHTMLSelectionObject selection = doc.selection;
IHTMLTxtRange textRange = (IHTMLTxtRange)selection.createRange();
textRange.pasteHTML(value);

Well, now I am getting an error on the second line: "Unable to cast object of type 'System.DBNull' to type 'mshtml.IHTMLDocument2"

I am not even sure if I am casting to the correct object type anyway, but in any case, it seems what I am getting back from the RTE_GetEditorDocument function is of System.DBNull.

All I want to do is say something like myRichTextHtmlElement.SetAttribute("value", html); but that obviously cannot be done.

To make things worse, I am completely new to javascript and I'm more of a WinForms guy, so my HTML is not exactly hot stuff. Below is the HTML for my RichText field:

<tr>
  <td nowrap="true" valign="top" width="190px" class="ms-formlabel">
    <h3 class="ms-standardheader">
      <nobr>RichText</nobr>
    </h3>
  </td>
  <td valign="top" class="ms-formbody">
    <!-- FieldName="RichText"
             FieldInternalName="RichText"
             FieldType="SPFieldNote"
          -->
    <span dir="none">
      <div class='ms-rtestate-field ms-rtefield' style=''>
        <div id='ctl00_m_g_29d60052_5630_4981_8452_850a87a50b56_ctl00_ctl05_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_TextField_inplacerte_label'
             style='display:none'>Rich text editor
        </div>
        <div class=' ms-rtestate-write ms-rteflags-0'
             id='ctl00_m_g_29d60052_5630_4981_8452_850a87a50b56_ctl00_ctl05_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_TextField_inplacerte'
             style='min-height:84px'
             aria-labelledby='ctl00_m_g_29d60052_5630_4981_8452_850a87a50b56_ctl00_ctl05_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_TextField_inplacerte_label'
             contentEditable='true' >
          <div class="ExternalClassD74B4D64D01941CDB34619757AAA30D8">
            <html>
              <body>
                <h4>A Definition List:</h4>
                <dl>
                  <dt>Coffee</dt>
                  <dd>Black hot drink</dd>
                  <dt>Milk</dt>
                  <dd>White cold drink</dd>
                </dl>
              </body>
            </html>
          </div>
        </div>
        <div style="clear:both;"></div>
      </div>
      <span dir="ltr">
        <input name="ctl00$m$g_29d60052_5630_4981_8452_850a87a50b56$ctl00$ctl05$ctl07$ctl00$ctl00$ctl04$ctl00$ctl00$TextField_spSave"
               type="HIDDEN"
               id="ctl00_m_g_29d60052_5630_4981_8452_850a87a50b56_ctl00_ctl05_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_TextField_spSave" />
      </span>
    </span>
  </td>
</tr>

Anyone got any ideas? Thanks!

A: 

do you have to use a winforms webbrowser control? One could use whatever html code editor they wanted in a winform and capture the results and use the sharepoint web services or object model(depending on use case) to update the item as an alternative option.

brian brinley
A: 

AFAIK there is no way to do this via DOM. The iMacros web automation tool can populate these Rich Text fields, but I think they use Windows hooks for it (no DOM).

SamMeiers
A: 

Well, after a lot of pulling hair out, I got the answer and it was simpler than I thought. Once I got the correct HtmlElement (the div with id ".....TextField_inplacerte") then all I needed to do was this:

HtmlElement element = document.GetRichTextFieldCell(fieldName);

if (element != null)
{
   element.InnerHtml = value;
   element.SetAttribute("value", value);
}

I probably don't need the SetAttribute bit, so I will attempt the same thing later without that line.

Thanks anyway guys. :-)

Matt