Click here to monitor SSC

Charles Lee

Initiate a SharePoint workflow from a button on the list view.

Published Tuesday, November 24, 2009 7:00 AM

I came across a scenario recently where a project required that a list of items could have a button or link on a list view, which would cause a workflow to start.

Essentially the users involved were not happy using the out-of-the-box way that SharePoint (WSS 3.0) allows users to manually initiate workflows. They believed there were too many steps involved.

After a bit of searching, I came up with a method involving the use of a hyperlink column on the list in question, coupled with a very simple workflow. I will outline the steps involved in this process below. This is not the most graceful solution I am sure, but it is a simple solution to what can be a tricky problem.

How was it done?

Note: I am assuming that you have a simple workflow attached to a list, if not then please create a simple SharePoint Designer workflow. You can see how to do this here.

1. Add a new hyperlink column to your list, this is going to represent the button which users click on to start the workflow, so give the column an appropriate name.

 CreateWFColumn

2. Click on the list settings for your list and select 'Advanced settings', ensure that 'Allow management of content types' is set to 'Yes'. This will allow us to hide the hyperlink column from the forms involved, if your list is a custom list then you may be able to go to 'Form settings' within list settings and make more detailed amendments to what appears on the new, display and edit forms.

ManageCTs

3. In the 'Content Types' section of the 'List Settings' page, click on the content type for your list. Then click on the name of your column. On the 'Change List Content Type Column' page click on the radio button for 'Hidden'. Click 'OK' and return to your list, you should see an empty space for your new column. However if you click on 'New' or try to edit an existing item your column should not display.

HideColumn

4. I have a simple workflow attached to this list. This basic workflow merely adds an entry to the workflow history list saying 'Workflow started OK'. I assume your workflow is going to be slightly more complex, however the principle is the same. If you click on an item in your list (if you don't have one then create a quick test entry), and select the 'Workflows' icon from the item toolbar. Now click on the name of your workflow.

ItemToolbar

5. You should now be looking at the initiation page which SharePoint Designer has created for us, now this page can be customised to say whatever you wish via SharePoint Designer, but I am going to leave it as the default for now. The important part here is the URL and parameters.

WFInit

The URL of this page will be something similar to:

http://SITE URL/List Name/Workflows/Workflow Name/Workflow Name.aspx

and it should have 4 URL parameters, these are as follows:

List
The guid which identifies the list involved. This will not change for each link required on our list.

ID
The numerical ID of the list item involved. This is the key to making this process work.

TemplateID
The guid of the workflow to be initiated. This will not change for each link required on our list. Note: This guid, unlike the list guid above requires the braces {}, though these may be url encoded (so don't be surprised to see the guid starting %7B which is { when URL encoded).

Source
The referring URL, responses will be redirected back to here once the workflow is successfully started.

You can probably see that we could manipulate this URL to generate a link for our new hyperlink column by simply altering the ID parameter being sent to this page. How can we do this? We simply use another workflow!

6. Ensure you have copied the URL from step 5 to the clipboard. Open SharePoint Designer and create a new workflow for this site. This simple workflow should be set to 'Automatically start this workflow when a new item is created' only.

WF1

7. Click 'Next' and then add a 'Build Dynamic String' action click on 'dynamic string' and paste your URL into the 'String Builder' window. Find the ID parameter and delete the value which you had pasted in. Leaving the cursor in this location click on 'Add Lookup' and select the 'ID' column of the 'Current Item'.

WFLookup

Click 'OK'. Your URL should now look similar to that shown above. At the end of the URL add a comma (,) followed by the text which you want your hyperlink column to display in the list view. For example I have added ', Start my workflow'. There must be a space between your comma and your link text. Otherwise you will get the entire URL as the text for your hyperlink!

Click 'OK' again.

8. Add another action to 'Update List Item' and with Current Item selected click on 'Add'. The 'Value Assignment' dialog will allow you to assign your new hyperlink column to the value of the URL string we have just generated. Select your new hyperlink column from the 'Set this field:' list. Next to the 'To this value:' list select the 'fx' button. In the 'Source:' drop down select 'Workflow Data' and in the 'Field:' select your variable (probably named with the default 'Variable: variable')

WFVariable

ValueAssign

Click 'OK' then 'OK' again on the 'Value Assignment' dialog. The 'Update List Item' dialog should now show this field and value in the list of items to be updated. Click 'OK' again.

9. We can now click 'Finish' to complete our simple workflow. SharePoint Designer will now generate the required files and bind this workflow to your list.

10. Go to your list and click on 'New' in the list toolbar, complete a test item (noting that your hyperlink column is not listed). Click 'OK' and you should return to your list view and your hyperlink column should be populated with the display value you entered in stage 7.

ListView

Clicking on this hyperlink should take you to the initiation page that SharePoint Designer created for you, but (crucially) the ID will be the ID for the item which the hyperlink column exists on.

Clicking 'Start' on this page will start your workflow as designed in SharePoint Designer.

So there you go! Wait for it.There are a few buts.

Issues

1. Document Libraries with check in/out enabled
This is the same as designing any workflow which is going to update these items, you must handle checking in/out. Because our workflow is only initiated on creation then this is not a major issue. I did come across some problems in this scenario, but they were overcome by having the workflow check the item back in before updating.

2. Custom Workflow
This workaround is designed for dealing with either SharePoint Designer workflows or custom workflows which use an initiation form as SPD always creates a workflow initiation form anyway. It will not work for custom workflows that do not require an initiation form as there is no URL to link to (This is a scenario that I am interested in so if anyone has any recommendations then please post a comment). If your custom workflow uses an InfoPath form to initiate then you might want to look at creating links to the following URL:

http://SITE URL/_layouts/IniWrkflIP.aspx

This page takes similar parameters to the SPD generated page except you can supply an xsn location for your InfoPath form too. More information can be found here. The above technique should work perfectly well in this scenario, I just haven't tried it.

Comments

 

Initiate a SharePoint workflow from a button on the list view. « F5 to Debug said:

November 25, 2009 8:11 AM
 

sacrot2 said:

Thanks for your article, but I keep getting an Error updating a list item.  Can you help me figure out whats going on?
November 25, 2009 8:49 AM
 

CharlesLee said:

Have you got more information on the error being raised?  Have you definitely put a comma followed by a space between your URL and the link text in step 7?
November 25, 2009 8:56 AM
 

sacrot2 said:

I dont have more information on the error.  yeah i put a comma and a space in.

I got
URL.aspx, Push me

Anyway you can post a picture of your conditions and actions  for your workflow?
November 25, 2009 9:27 AM
 

CharlesLee said:

The only other thing I could think of would be if you had check in/out enabled on the list.  As I mentioned in the article you would need to handle this within the workflow.  If you have this feature turned on then disable it and see if it makes any difference.
November 27, 2009 2:36 AM
 

sacrot2 said:

Yeah I tried that too, with no luck.  I turned the check in/out off.
November 30, 2009 9:22 AM
 

mada2885 said:

I just tried to implement this and I ran into an issue with the hyperlink column not accepting my site URL along with the specific workflow URL because they exceeded 255 characters.  What I did instead of using the Hyperlink column is to use the Multiple Lines of Text column, and add "<a href=[site&workflow url>" and "</a>" after the text you want to appear for users to click.  Hope this helps someone else.
December 31, 2009 12:31 PM
 

CharlesLee said:

mada2885 makes a very valid point.  However I would be looking at methods of reducing your URL length rather than trying to work around this issue.  There is lots of information out there about how to keep your SharePoint URLs nice and short.  Its important to note that the Source URL parameter will usually work with relative URLs so you do not need to specifiy the full URL.
January 4, 2010 5:42 AM
 

pacecar said:

Thanks for the great article. I made the link on my list and then edited the workflow's aspx page to add some fields from the item and changed the workflow start button to say "Request" (for a Lending Library application) instead of the basic Start. All of this with no coding - wonderful!
March 17, 2010 5:54 PM
 

rahiks said:

Hi Charlie! Thanks for posting this! You are a life-saver! I had similar requirement from the client and I tried your approach and it worked like a charm. I was looking for something that would include JavaScript, Hyperlink/Button, EventHandler all together, but I'm not good at JavaScript, so your way was handy.

Cheers!
June 3, 2010 3:20 AM
 

Alan J said:

Neato!  However, I am having trouble getting my hyperlink text top show up.  Any thoughts?  This is my url, sort of.  I notice when I copy and paste the url from the macro, the variables and text disappear.
https://my.site.com/facpuch/Workflows/Workflow%202/Workflow%202.aspx?List=67f9e194-7b11-42df-b051-c4d55eed4aec&ID=[%Time Sheet:ID%]&TemplateID={325b395b-9976-44e5-bfee-0b02ac8dcc42}&Source=https%3A%2F%2Fproxy%2Eisaacsdeli%2Ecom%2Ffacpuch%2FLists%2FTime%2520Sheet%2FAllItems%2Easpx, CLICK
Thanks for any suggestions.
July 2, 2010 10:42 PM
 

Alan J said:

I notice that my list has a space int the name, "Time Sheet", so I replaced the space with %20, but that didn't make the hypertext show up.
July 5, 2010 12:42 PM
 

JimEh said:

Alan,

Did you ever solve it?  have the same issue. Help.

http://rod/ER/Job%20Opportunities/Workflows/Apply%20Field/Apply%20Field.aspx?List=e2c10dc3-e43a-44eb-a042-67da078b97d3&ID=22&TemplateID={c77adee1-202b-4896-b9b0-780bcc1119c5}&Source=http%3A%2F%2Frod%2FER%2FJob%2520Opportunities%2FLists%2FOpen%2520Positions%2FAllItems%2Easpx, Click
July 22, 2010 4:09 AM
 

Alan J said:

I think we are exceeding the 255 character limit.  When I have a free moment, I want to try mada2885's work-around.
July 26, 2010 9:53 PM
 

marquito said:

Excelent Post!!!

Best regards
January 12, 2011 5:16 PM
 

KJP said:

it's not bad, but if you make any changes to your workflow the templateID= changes, so your workflow building the link needs to be redone and all previously deployed links need to be deleted and rebuilt. If you don't have a large list might not be an issue but just a heads up.

February 17, 2011 6:59 PM
 

Mooscar Chan said:

Is it possible to click the hyperlink and then start the workflow directly?
User do not need to click "Start" button.
September 28, 2011 5:08 AM
 

iamandyogden said:

Hey Charles

Found this post a while back and finally got around to implmenting it today.  Everything works exactly as planned, however when the workflow completes it takes me directly to the list, rather than returning me to the page within which the list was displayed in a web part.

Is there any way to control the exit page?

Thanks
Andy
October 19, 2011 5:01 PM
You need to sign in to comment on this blog

About CharlesLee

Charles Lee is a software solutions developer for a large law firm based in Birmingham in the UK. He has been developing with ASP.NET since 2003, and currently focuses on WSS 3.0 and MOSS 2007 solutions to complex business problems.
<November 2009>
SuMoTuWeThFrSa
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. David Wesley... Read more...

Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across, and start getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...