AlertThis post is over a year old, some of this information may be out of date.
SharePoint Designer Workflow Action: Retrieve the File Extension
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:
Prepare the project
Start by creating a new Empty SharePoint Project. I named my project: “estruyf.SPDActivity.GetFileExtension”;
Link it to the corresponding SharePoint site, and deploy it as a farm solution;
Add the following references to your project:
Start coding
Create a new class and call it “GetFileExtension”;
Make it the class “public” (when this class is not set to public, SharePoint Designer gives an error that the: is inaccessible due to the protection level);
1
2
3
4
publicclassGetFileExtension{}
Let your class inherit from the “System.Workflow.ComponentModel.Activity” class;
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;
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.
Create a mapped folder to the following location: “SharePoint Root/TEMPLATE/Language Code/Workflow”;
Add a new XML file to the folder and name it: “GetFileExtension.actions”;
Place the next code block into the Actions file;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<WorkflowInfo><ActionsSequential="then"Parallel="and"><ActionName="Extract the file extension"ClassName="estruyf.SPDActivity.GetFileExtension.GetFileExtension"Assembly="estruyf.SPDActivity.GetFileExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1f8a604908bb57cd"AppliesTo="all"Category="Utility Actions"><RuleDesignerSentence="Retrieve the file extension from the %1 string (Output to %2)"><FieldBindField="StringVal"Text="Document Name"Id="1"DesignerType="TextArea"/><FieldBindField="SubstringVariable"Text="File Extention String"Id="2"DesignerType="ParameterNames"/></RuleDesigner><Parameters><ParameterName="__Context"Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext, Microsoft.SharePoint.WorkflowActions"Direction="In"DesignerType="Hide"/><ParameterName="StringVal"Type="System.String, mscorlib"Direction="In"DesignerType="TextArea"Description="This should be the document name."/><ParameterName="SubstringVariable"Type="System.String, mscorlib"Direction="Out"DesignerType="ParameterNames"Description="Workflow variable output by this action."/></Parameters></Action></Actions></WorkflowInfo>
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.
Add a new feature to your project;
Add an event receiver to the feature;
Add the following code block to the “FeatureActivated” method.