PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Custom Workflow Activity combined with InfoPath and XSLT

Posted by PANVEGA on May 4, 2008

In my case I needed a custom workflow activity in order to send a email notification to some users. In addition the workflow triggers when a new InfoPath XSN formular was created. And exactly here was my problem.

In my InfoPath Formular there are some repeating sections which should be transformed via XSLT and CSS in the presented email body.  By default the SP Designer workflow Email activity can not 100% realize the repeating sections. So I developed a custom activity in Visual Studion which links to a specific XSLT file.

InfoPath –> MOSS –> (XSLT, CSS) HTML

  • first create you custom activity with the Visual Studio Extension templates. Should inherit from the :Activity

To create the assembly that contains the custom Workflow Activity class, perform the following steps.

  1. Open VS 2005.
  2. Click File| New | Project.
  3. In the Project Types tree select Workflow.
  4. In the Templates list box select Workflow Activity Library.
  5. In the Name text box enter a Name for .ActivityLibrary.
  6. Click the OK button.

To create the class for the custom Workflow Activity, perform the following steps.

  1. In the Solution Explorer, right click the .ActivityLibrary node.
  2. Select Add | Activity.
  3. In the Add New Item window, select Visual C# Project Items in the Categories tree.
  4. Select Activity in the Templates list box.
  5. In the Name text box enter CustomEmailActivity.cs.
  6. Click the Add button.
  7. In the Solution Explorer, right click the CustomEmailActivity.cs file and select View Code.
  • add your properties to the custom email activity e.g.

http://msdn.microsoft.com/en-us/library/cc627284.aspx

http://www.sharepointblogs.com/tbaginski/archive/2007/03/08/how-to-create-a-custom-windows-workflow-activity-and-make-it-available-in-sharepoint-designer.aspx

public static DependencyProperty XSLTProperty = System.Workflow.ComponentModel.DependencyProperty.Register(“XSLT”, typeof(string), typeof(HtmlBodyActivity));

public static DependencyProperty BodyValueProperty = System.Workflow.ComponentModel.DependencyProperty.Register(“BodyValue”, typeof(string), typeof(HtmlBodyActivity));

  • now you need some custom code for example

Note:

you catch the item with the ListId and ListItem property in the activity

SPList sourceList = __Context.Web.Lists[new Guid(ListId)];
SPListItem listItem = __Context.GetListItem(sourceList, ListItem);

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
// get the list
SPList sourceList = __Context.Web.Lists[new Guid(ListId)];
// get list item
SPListItem listItem = __Context.GetListItem(sourceList, ListItem);

HtmlBodyActivity htmlbody = new HtmlBodyActivity();
htmlbody.XSLT = this.XSLT;
string value = TransformXml(listItem, htmlbody.XSLT);

if (value != null)
BodyValue = value;
return ActivityExecutionStatus.Closed;
return ActivityExecutionStatus.Faulting;
}

  • create your custom XSLT file
  • the TransformXml() creates the XPathDocumentand iterates through the XML file and catches the repeating sections tags in the InfoPath file. The method returns the transformed string.

<xsl:template match=”my:XSN”>

<xsl:apply-templates select=”my:users”/>

<xsl:template match=”my:users”>
<table class=”content”>
<tr>
<th class=”head”>
Client <xsl:value-of select=”position()”/>
</th>
</tr>
<tr>
<td class=”formlabel”>
<b>Name:</b>
</td>
<td class=”formbody”>
<xsl:value-of select=”my:Name”/>
</td>
</tr>
<tr>

</table></xsl/template>

Note:

to access a InfoPath XMl formular in SharePoint you need:

SPFile myFile = xmlItem.File;
MemoryStream myInStream = new MemoryStream(myFile.OpenBinary());
XPathDocument xmlDoc = new XPathDocument(myInStream);

  1. Save the file.
  2. Click Build | Build .ActivityLibrary.
  3. Finally, sign the assembly.
    1. Right click on the .ActivityLibrary node in the Solution Explorer.
    2. Select Properties.
    3. Click the Signing tab.
    4. Check the Sign the assembly checkbox.
    5. In the Choose a strong name key file dropdown list select New.
    6. Name the key file .ActivityLibrary.snk.
    7. Uncheck the Protect my key file with a password checkbox.
    8. Click the OK button.
  4. Build the Assembly.
  5. Obtain the PublicKeyToken for your assembly.
    1. Open a command prompt.
    2. Change to the output directory of your .ActivityLibrary project.
    3. Issue the following command: sn –T .ActivityLibrary.dll
    4. Copy the value returned, and paste it into the AssemblyInfo.cs file in the .ActivityLibrary project.
      1. You will use this value later in the deployment process.
    5. Make sure you comment out the line, like this://Public key token is <Your PublicKeyToken will appear here>
  6. Deploy the assembly to the GAC on your SharePoint Server.
  • Add your .ACTIONS file to SharePoint C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\Workflow

<RuleDesigner Sentence=”Convert %1 with this %2 and store in (Output to %3)”>
<FieldBind Field=”ListId,ListItem” Text=”this list” Id=”1″ DesignerType=”ChooseListItem” />
<FieldBind Field=”XSLT” Text=”Xslt” Id=”2″ DesignerType=”stringbuilder” />
<FieldBind Field=”BodyValue” Text=”variable” Id=”3″ DesignerType=”ParameterNames”/>
</RuleDesigner>

then you see the output workflow value as follows:

wfactivity

  • store the XSLT string output in a SP Designer variable. Then you can use the new creates variable as a lookup value in your Email body.
  • Add the <authorizedType Assembly=”InfoHtml, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXXX” Namespace=”InfoHtml” TypeName=”*” Authorized=”True” />  to your web.config
  • I will recomment to build a Deployment Solution which includes all deployment steps
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: