SharePoint 2007: Creating a simple non-human Worfklow

Posted on 8/7/2006 @ 11:45 PM in #SharePoint by | Feedback | 12047 views

I’ve been talking a bit about workflow foundation lately. Basically I’ve been trying to set a background for what was coming – Talking about Workflow in SharePoint 2007.

So far I’ve talked about –

What is Windows Workflow Foundation?

Workflow Foundation: The Hello World App

CompositeActivities: Workflow activities that contain one or more child activities.

.. and finally, some theory and criticism of ..

Transaction support in Workflow Foundation

I still owe you a number of topics on Workflow Foundation, including custom services, persistence, correlation, and of course the workflow compiler and runtime. Time permitting I will talk more about those in subsequent blog posts.

For now, let us dive straight into a simple “Hello World’ish” example of Workflow in SharePoint 2007.

In this blog post, I will write up a simple workflow example, associate it with a simple custom list. The workflow is super simplistic, it simply takes the newly added list item, and does some activity behind it. There is no human interaction involved. This kind of workflow is perfect for doing system work behind the scenes, such as shooting an email, or throwing a wait condition etc. In a future blog post I will talk about associating workflows with human input using InfoPath 2007 forms, or ASPX’es.

So let us get started.

The first step is to create a SharePoint Sequential Workflow Library as shown below.

I’m calling it SMWorkflow, you can call it whatever you wish.

Next, open the workflow1.cs file, and it should look a bit like this.

Now you can add more activities if you wish, but I am going to keep this super duper simple. So I will double click on the green blurb, and simply add the following code in the _Invoked event handler.

private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)

{

    workflowProperties.Item["Title"] =

        workflowProperties.Item["Title"].ToString() +

            ": Processed by Custom Workflow";

    workflowProperties.Item.Update();

}

 

As you can see, I am simply getting a hold of the current item, and updating appending “: Processed by Custom Workflow” to its Title Text. You can make this workflow complicated if you wish – you can add custom activities etc. To keep this example simple, I am going to skip that, but trust me – it’s rather easy to extend this.

Next you need to do some hardwork in installing this workflow.

Now as it turned out, my code snippets were broken, so I had to hand-type the feature.xml, workflow.xml and install.bat files. What a pain in the donkey!!

Anyway, here goes. First the workflow.xml file (I’ve already strong named my assembly, and pulled out the public key token etc.):

<?xml version="1.0" encoding="utf-8" ?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

      <Workflow

             Name="SM Workflow Example"

             Description="This is a simple workflow I created."

             Id="59DD482E-27A5-4cc4-9ED6-5B2F56FA4D22"

             CodeBesideClass="SMWorkflow.Workflow1"

             CodeBesideAssembly="SMWorkflow, Version=3.0.0.0, Culture=neutral, PublicKeyToken=bf67e65a604d67e1"

             >

            <Categories/>

            <MetaData>

                  <StatusPageUrl>_layouts/WrkStat.aspx</StatusPageUrl>

            </MetaData>

      </Workflow>

</Elements>

Then the feature.xml file which do note that it has a reference to the workflow.xml file.

<?xml version="1.0" encoding="utf-8"?>

<Feature  Id="AE34D7E1-0CC3-4ba7-BB87-48F06F771553"

          Title="SMWorkflow"

          Description="SM worfklow."

          Version="12.0.0.0"

          Scope="Site"

          ReceiverAssembly="Microsoft.Office.Workflow.Feature, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"

          ReceiverClass="Microsoft.Office.Workflow.Feature.WorkflowFeatureReceiver"

          xmlns="http://schemas.microsoft.com/sharepoint/">

      <ElementManifests>

            <ElementManifest Location="workflow.xml" />

      </ElementManifests>

      <Properties>

            <Property Key="GloballyAvailable" Value="true" />

            <Property Key="RegisterForms" Value="*.xsn" />

      </Properties>

</Feature>

 

And finally the install.bat file which throws my assembly in the GAC (which means I need to strong name it), and installs and activates the feature in SharePoint 2007 at site scope.

echo Copying the feature...

echo.

rd /s /q "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\SMWorkflow"

mkdir "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\SMWorkflow"

 

copy /Y feature.xml  "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\SMWorkflow\"

copy /Y workflow.xml "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\SMWorkflow\"

xcopy /s /Y *.xsn "%programfiles%\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\SMWorkflow\"

 

 

echo.

echo Adding assemblies to the GAC...

echo.

"%programfiles%\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" -uf SMWorkflow

"%programfiles%\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" -if bin\Debug\SMWorkflow.dll

 

echo.

echo Activating the feature...

echo.

pushd %programfiles%\common files\microsoft shared\web server extensions\12\bin

stsadm -o deactivatefeature -filename SMWorkflow\feature.xml -url http://localhost

stsadm -o uninstallfeature -filename SMWorkflow\feature.xml

 

stsadm -o installfeature -filename SMWorkflow\feature.xml -force

stsadm -o activatefeature -filename SMWorkflow\feature.xml -url http://localhost

 

 

echo Doing an iisreset...

echo.

popd

iisreset

 

Whew !! That was a mouthful.

Now just run Install.bat.  I am going to skip posting the scary screenshot of the gazillion unixy command line thingies the installation throws at you. I seriously think given the love SharePoint has for fragile XML files and Batch files, and GAC, I think SharePoint is a closet unix-o-sexual. But that is a whole another discussion.

Great, the feature is installed and ready to use (activated). Let us go ahead and set up a simple custom list.

In the custom list, go to List Settings, and under Permissions and Policies, click on “Workflow Settings”

Fill in the ensuing form a bit like this:

Then go back to your list, and add a new item.

You would note that the item has been added as shown below:

WAIT A MINUTE!! Where is my workflow?

Well, the workflow did get fired. Since it was asynchronously fired, you don’t see it yet. But hit Refresh (F5), and you will see that your item has been processed by your custom workflow.

HOW NEAT IS THAT!!! :)

Not only that, you can debug your workflow by attaching to the w3wp process. Just for kicks, modify this  workflow properties so it is kicked automatically when the item is changed also.

Observe what happens!! Can you explain the behavior? ;-)

.. Well hope you enjoyed this blog post, sometime in the near future, I shall talk about workflows in SharePoint that need human IO, and of course other workflow foundation topics.

Sound off but keep it civil:

Older comments..


On 8/8/2006 6:09:08 AM Rob Garrett said ..
Is it essential to add workflow assemblies into the GAC, or is this a quick and easy way to get around the whole SharePoint minimal trust problem?

BTW, love this series, I read all your posts on WF.


On 8/8/2006 6:40:03 AM Sahil Malik said ..
Robert -

It must be deployed in the GAC. :)

SM


On 8/9/2006 9:41:14 AM Bil Simser said ..
Nice summary and intro. Also just a couple of ideas on posting on your blog. Have you tried CopySourceAsHtml? It makes reading source on blogs easy so you might want to check it out. Also if you remember it would be good to add the sharepointkicks code to your posts to get more people reading it (like any of us needs more traffic ;)


On 8/9/2006 11:40:40 AM Sahil Malik said ..
Bill -

I saw that on someone's blog and I wanted to use it - but then I lost where it was. Thanks for pointing that out to me, I'll google for it.

I am enhancing my blog engine this weekend to include better kicking support. That whole "Copy Paste" the URL was gettin' a tad bit old :)

SM


On 9/14/2006 2:58:22 PM mayu said ..
I tried it, but got error, Failed on start. how can I screwed this simple stuff. Do you have any idea why? Anything helps. Thanks in advance!


On 10/11/2007 1:30:18 AM ghdf said ..
Have the same problem. Faild on start.....

My log says Sharepoint cannot load dll. But the dll is in the gac. Any support on this?


On 10/11/2007 10:54:31 AM Sahil Malik said ..
Guys - did you look in the ULS/Event Log and see if you have any additional info? Has to be a simple and straightforward reason.

Also, I have written a whole another, possibly much clearer series on WF's. Did you check that out?

SM


On 1/8/2008 8:47:11 AM Mark van Lunenburg said ..
mayu and ghdf,

check if the public key token in the GAC corresponds with you pkt in your workflow.xml.

cheers


On 1/10/2008 3:40:01 AM Sams said ..
Hi Mark,

I am facing the the problem "Failed on start". And as per your suggestion, the the public key token in the gac is same with the PKF in the Workflow.xml file.

thank you for your help,


Sams


On 3/13/2008 8:33:19 PM Luis Galaz said ..
HI, I have a workflow and it working fine but if I try to use it on another site it wont work... well the workflow does start but the custom task edit form wont show and just gives an error.

Any ideas?


On 4/4/2008 7:59:22 AM sumedha said ..
Thank you very much for the sample work.


I too got the "fail on start", but solved with correct pkt.


On 4/8/2008 2:48:28 PM SharePointNewbie said ..
Got a Failed to Start error, but go it working after adding the correct the public key to workflow.xml. thanks for your tutorial.


On 8/24/2008 11:57:30 PM shee said ..
Hi,

Can you pls guide me how to call a custom workflow in the event handker of Project server 2007

Thanks


Shee


On 4/30/2011 3:05:18 AM ram said ..
can you briefly explain how to work with infopath in moss step by step