Sunday, February 28, 2010

Read Rich Text Fields inside IP from workflow code

If your form contains a rich text field and you need to use this data inside your workflow code , may be you will use property promotion to publish your data source to the form library But you will notice that rich text fields published as plain text and you will find all of your formatting lost.

The solution is to read the inner html of the eich text so as you can will not loose the formatting.

Follow these steps
1- inside your form add your rich text control
2- then add another field that will hold the source html inside the rich text.
3- add a sumbit event handler then add the following code.

public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
FileSubmitConnection dc =
(FileSubmitConnection)this.DataConnections["Submit"];

XPathNavigator root = MainDataSource.CreateNavigator();

string notes = root.SelectSingleNode("/my:myFields/my:MemoContents",
NamespaceManager).InnerXml;

root.SelectSingleNode("/my:myFields/my:MemoContentSource",
NamespaceManager).SetValue(notes);

dc.Execute();
e.CancelableArgs.Cancel = false;
}


the Idead behind reading the InnerXml from rich text field not Value which will read the plain text of the field

4- publish the field that contains the html source of the control.
5- then you can read this field from your workflow and format it as you like.



Thanks