Monday, June 11, 2012

Setting an optional parameter in workflow activity

Just a little something I encountered today, been working on some new custom actions for SharePoint Designer workflow editor.

I wanted to add an action that converts a file to a PDF and stores it somewhere on the server.

Everything was going smooth, except for 1 thing: the target PDF file name parameter – I wanted this parameter to be optional, so that people can either calculate a value for it or leave it empty. Once empty – it would keep the source file name, or will take the list item title as the new file name.

So, looks like marking a workflow activity parameter as optional is not a simple task as I thought it would be.

It starts by changing the attribute on the property in the Activity class:

   1: [Description("Target Filename")]
   2: [Browsable(true)]
   3: [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
   4: [ValidationOption(ValidationOption.Optional)]
   5: public string TargetFilename
   6: {
   7:     get { return ((string)(base.GetValue(TargetFilenameProperty))); }
   8:     set { base.SetValue(TargetFilenameProperty, value); }
   9: }

I had to change the “ValidationOption” from required to optional. First, I tried using “None” but it seemed not to work.

Once I’ve done that, I thought I was done. But still – SharePoint Designer didn’t accept my workflow and prompted for a value.

So, after some more reading and help from Kevin Vieira, he thought we should try and change the direction property on the parameter definition in the actions xml file:

   1: <Parameters>
   2:     <Parameter Name="TargetFilename" Type="System.String, mscorlib" Direction="Optional" />
   3: </Parameters>

Although in several places in MSDN I found that the parameter direction can only be In or Out, Kevin found one page that said “Optional” was also supported. Of course with no other explanation or documentation regarding what is optional and when to use it.

So, marking it as optional saved the day!

Oh, another very important note for those of you writing custom actions/activities and working with SharePoint Designer:

On almost any change you make, you have to clear SharePoint Designer cache before your changes take effect.

Kevin found a nice blog post by Matthew Workman about that as well explaining which folders to clear (this is how I like them – like a mini skirt: short enough to keep it interesting - long enough to cover the story):

“cd "%APPDATA%\Microsoft\Web Server Extensions\Cache"
del *.web /S /Q "%APPDATA%\Microsoft\Web Server Extensions\Cache"
cd "%USERPROFILE%\AppData\Local\Microsoft\WebsiteCache\"
rmdir /S /Q "%USERPROFILE%\AppData\Local\Microsoft\WebsiteCache\."
mkdir "%USERPROFILE%\AppData\Local\Microsoft\WebsiteCache"
dir "%APPDATA%\Microsoft\Web Server Extensions\Cache"
dir "%USERPROFILE%\AppData\Local\Microsoft\WebsiteCache"pause”



Shai Petel said...

One more place found by Stephane Gregoire:

Search and delete this DLL from the client machine:

(should be under %USERPROFILE%\AppData\Roaming\Microsoft\SharePointDesigner\ProxyAssemblyCache)

ACTS_MEN said...

Hi Shai,

I'm very new to this and i need to do a similar thing:
When a xml document is uploaded to sharepoint, i need to save it on the server desktop for example. I need to be able to give the workflow option for example: 1. Save on server 2. send to msmq, or send to service.

I then need to in C# code, see which option was chosen in the workflow and then write the code to do it.

I know i need to do an activity. I'm using sharepoint designer 2013, visual studio 2013, any help would be really appreciated.

Shai Petel said...

Haven't done it in VS2013, but google shows this walkthrough you can try: