Resolving a file in a different document library using the Workflow Power Pack

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.

  1. Create a Document Library named "Source Library".
  2. Create a Document Library named "Destination Library".
  3. Open SharePoint Designer, Create a new workflow and attach it to the "Source Library".
  4. Set the workflow to "Start Manually"
  5. Add the 'Convert Document' action. (This assumes the Muhimbi PDF Converter for SharePoint is installed.)
  6. 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
  7. Add the 'Execute Custom Code' workflow action and set the following parameters:
    • 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)
  8. Insert the following in the code parameter:

    // ** 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;

  9. Add a 'Log to History' workflow action and select the 'result' workflow variable.
  10. Publish the workflow.
  11. Upload an image to "Source Library" and manually start the workflow.
  12. Open the workflow history to see the output, which should contain the name of the destination file.


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());


Have more questions? Submit a request


Please sign in to leave a comment.