|
Due to the lack of an easy way to do a find and replace in InfoPath, I developed a simple web service with a web method called replace that uses managed code to call the replace method of the string data type. Here’s how I call the web method in a browser-based Sharepoint InfoPath form.
First create a form like so..

With fields txtFind, txtReplace and txtText
The idea in this sample form is to replace mother with sister in the text “Don’t forget to call your mother today!”
The next thing you must to is create a new data connection.

You should first see if your Sharepoint server already has a data connection to the Replace web method

Enter the URL of the site like in the above image and then expand the data connections.
Select the Replace.udcx data connection. If you do not see this data connection you must create a new data connection to receive data and then select the web service
Once you have created the web service data connection you must convert it to a data connection file that you must store in a data connection library in your Sharepoint site.
Now you will be presented with the data connection wizard

The web method requires three parameters:
Text: the text you want to do the search and replace on
stringToFind: the text you want to replace ie mother
stringToReplace: the text you want to replace it to ie daughter
escapeSpaces: If you are having difficulty with spaces in your strings and need to use %20 then set this to true otherwise default it to false
For now don’t set default values and click on next, next deselect Automatically retrieve data when form is opened and then click on finish
Your data connection has now been created
So the next thing we need to do is create some rules for our button
The rule will have the following actions - set each of the parameters of the data connection , then query the data connection and then set the txtText value to the result of the data connection query

For the first rule we set the text parameter of the Replace datasource to the value of the txtText field. (make sure to select the Replace data source and not the main data source)
Then create the following actions so that it looks something like this:

Now test out your rule by previewing the form

Click on the Replace button and the text shoudl read Don’t forget to call your daughter today!

Here’s the code for my web method:
_Â
_
Public Function Replace(ByVal text As String, ByVal stringToFind As String, ByVal stringToReplace As String, ByVal escapeSpaces As String) As String
If escapeSpaces.ToUpper = “TRUE” ThenstringToFind = stringToFind.Replace(” “, “%20″)
End If
Return text.Replace(stringToFind, stringToReplace)
End FunctionÂ
|