SharePoint Designer Workflow Action: Retrieve the File Extension

March 30, 2011

This week I was a experimenting with creating SharePoint Designer workflows. After some time I noticed that it was not possible to retrieve the file extension in a SPD Reusable Workflow. Because this is a very useful action, I am going to explain it in this blog post on how to create a SharePoint 2010 Designer Workflow Action.

Another possibility is working with the "If the file type is …" action, but then you will need to predefine the file extensions.

Create a custom activity

In the following steps I am going to explain you how to create a SPD custom activity. The end result will be:

New SharePoint Designer Action

New SharePoint Designer Action

Extract File Extension Action

Extract File Extension Action

Workflow history

Workflow history

Prepare the project

  1. Start by creating a new Empty SharePoint Project. I named my project: "estruyf.SPDActivity.GetFileExtension";
    Create an Empty SharePoint Project

    Create an Empty SharePoint Project

  2. Link it to the corresponding SharePoint site, and deploy it as a farm solution;
    Link the Project to Your SharePoint Site

    Link the Project to Your SharePoint Site

  3. Add the following references to your project:
    Insert the Following References

    Insert the Following References

Start coding

  1. Create a new class and call it "GetFileExtension";
    Create a New Class

    Create a New Class

  2. Make it the class "public" (when this class is not set to public, SharePoint Designer gives an error that the: <classname> is inaccessible due to the protection level);
  3. Let your class inherit from the "System.Workflow.ComponentModel.Activity" class;
    Inherit from the “System.Workflow.ComponentModel.Activity” Class

    Inherit from the “System.Workflow.ComponentModel.Activity” Class

  4. Define the workflow fields that will be used in the SharePoint activity.;

    The "StringValProperty" will be used as an input value for the document name property. The "SubstringVariableProperty" will be used as an output value that is stored in a workflow variable.
  5. The next step is to override the "Execute" method;
  6. The only thing that needs to be added to the code, are the properties that are part of the Activity class.

Create the workflow action xml

When your code file is created, you need to register the action to SharePoint. This can be done by creating an "Actions" file and place it in the "SharePoint Root/TEMPLATE/Language Code/Workflow" folder.

SharePoint will automatically read the new file and add the custom action into the memory.

  1. Create a mapped folder to the following location: "SharePoint Root/TEMPLATE/Language Code/Workflow";
    Mapped Folder

    Mapped Folder

  2. Add a new XML file to the folder and name it: "GetFileExtension.actions";
  3. Place the next code block into the Actions file;

    If needed change the "Classname" and "Assembly" attributes to your settings.
    Check if the Parameter names are the same as defined in the code file.

Webapplication feature

When you deploy your project, you will notice that the custom activity cannot be used. To be able to use this activity, an "authorizedType" entry needs to be added in the "web.config".

To make this process a little more user friendly, you can create a web application feature event receiver that adds this "web.config" "authorizedType" entry.

  1. Add a new feature to your project;
    Web Application Feature

    Web Application Feature

  2. Add an event receiver to the feature;
  3. Add the following code block to the "FeatureActivated" method.

After this step you can deploy your project to SharePoint.

Troubleshooting

The custom activity is shown in the actions list, but you are not able to select it.

  1. Check if the "Actions" xml file contains the correct assembly and class name.
  2. Check if the web.config contains the "authorizedType" entry.
  3. Check the assembly name and Namespace in the "authorizedType" entry.

The <classname> is inaccessible due to the protection level

Check if you made your class public.

References

SharePoint Designer 2010 Custom Workflow Activities Using Only 1 Solution
Delivering Modular SharePoint Workflow Functionality Part 1
Delivering Modular SharePoint Workflow Functionality Part 2

Sources

Download Project

Comments