Muhimbi's Workflow Power Pack (WPP) is a flexible and powerful solution to add C# or VB logic to SharePoint Designer workflows. Although the product comes with comprehensive documentation, including a tutorial, it is sometimes the little things that are difficult to figure out.
One of our more popular WPP sample scripts is the one that attaches the SharePoint file that started the workflow to an email, but what if you wish to attach a different file?
In this brief example we show how to attach a different file, even one that is stored in a different Document Library (but in the same site), to the email.
Please note that, similar to all our other WPP examples, some C# and SharePoint object model knowledge is required. If you need help creating custom scripts then please contact us and we'd be happy to put you in touch with a SharePoint consultancy.
- Create a Document Library named "Source Library".
- Create a Document Library named "Destination Library".
- Open SharePoint Designer, Create a new workflow and attach it to the "Source Library".
- Set the workflow to "Start Manually"
- Add the 'Convert Document' action. (This assumes the Muhimbi PDF Converter for SharePoint is installed.)
- Change the following parameters to convert the source item to the root of the "Destination Library" and store the converted file's Item ID and List ID in two workflow variables.
- this document: Current Item
- this file: Destination Library/ (Note the trailing slash)
- file type: PDF
- include meta data
- parameter 1: Specify the 'List ID' Workflow Variable
- parameter 2: Specify the 'List Item ID' Workflow Variable
- this variable: Create new, name it "result" (type string)
// ** Resolve the passed in List ID and List Item ID
Guid listID = new Guid( (string) MyWorkflow.Parameter1);
int itemID = int.Parse( (string) MyWorkflow.Parameter2);
// ** Resolve the List Item from the IDs
SPList list = MyWorkflow.Web.Lists[listID];
SPListItem item = list.GetItemById(itemID);
// ** Return some 'useful information', e.g. the file's name
MyWorkflow.ReturnValue = item.Name;
In this example we take the 'List ID' and 'List Item ID' from parameter 1 and 2. Naturally these values can come from other locations.
This code is a small part of larger solution. By itself it doesn't do anything useful, it needs to be integrated by a software developer into the main email script. For example, reading the content of the file (e.g. to pass it as an attachment into an email) can be done as follows:
Stream attachmentStream = new MemoryStream(item.File.OpenBinary());