Pat’s .Net and Sharepoint Blog
In the Microsoft Sharepoint and .Net Trenches

XSLT Code to output a hyperlink column in Sharepoint Designer

December 11th, 2008 . by <Patrick/>
 

<td class=”ms-vb”>
    <a class=”ms-toolbar”>
              <xsl:attribute name=”href”>
              <xsl:text>/_layouts/FormServer.aspx?</xsl:text>
              <xsl:text>XmlLocation=</xsl:text>
              <xsl:value-of select=”@FileRef”></xsl:value-of>
              <xsl:text>&</xsl:text>
              <xsl:text>OpenIn=Client</xsl:text>�
              </xsl:attribute>
              <xsl:value-of select=”@FileLeafRef”></xsl:value-of>
         </a>
   </td>


Using a Replace Web Service in a Sharepoint Web Based InfoPath form

December 10th, 2008 . by <Patrick/>
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
 


Replace XSLT Template for Sharepoint Designer

December 3rd, 2008 . by <Patrick/>

If you need to do a find and replace when outputting a column in a data view you can use the following:
�
<xsl:template name=”stringreplace”>
  <xsl:param name=”stringvalue” />
  <xsl:param name=”from” />
  <xsl:param name=”to” />
  <xsl:choose>
   <xsl:when test=”contains($stringvalue, $from)”><xsl:value-of select=”substring-before($stringvalue, $from)” />
    <xsl:if test=”contains(substring($stringvalue, 1, string-length($stringvalue) - 1), $from)”><xsl:value-of select=”$to” />
     <xsl:call-template name=”stringreplace”>
      <xsl:with-param name=”stringvalue” select=”substring-after($stringvalue, $from)” />
      <xsl:with-param name=”from” select=”$from” />
      <xsl:with-param name=”to” select=”$to” />
     </xsl:call-template>
    </xsl:if>
   </xsl:when>
   <xsl:otherwise><xsl:value-of select=”$stringvalue” /></xsl:otherwise>
  </xsl:choose>
 </xsl:template>
�
Call the template code:
Say you have a column named ColumnName and you want to replace all occurrences of True and set them to False…..

 <xsl:call-template name=”stringreplace”>
 <xsl:with-param name=”stringvalue” select=”@ColumnName” />
 <xsl:with-param name=”from” select=”True” />
 <xsl:with-param name=”to” select=”False” />
 </xsl:call-template>

�


Line feeds / Line breaks Sharepoint Dataviews

December 2nd, 2008 . by <Patrick/>

After having published an Infopath form with a rich textbox field to a document library, I created a dataview of the list.

The column that came from the rich textbox control (Comments) lost it’s linebreaks. In order to fix the problem I used the following code:

 <xsl:value-of select="ddwrt:AutoNewLine(string(@Comments))" disable-output-escaping="yes" />

Â