Today I got the following error and did not find a clear description and solution for it:
Error message: “Event “OnWorkflowActivated” on interface type “Microsoft.SharePoint.Workflow.ISharePointService” for instance id “<guid>” cannot be delivered.”
The Solution: …in my case was a missing “EventDriven” activity.
My “Initialization” state looks like this:
I’ve added a “StateInitialization” Workflow Activity and added some code to it. But this is a SharePoint Workflow and it needs at least an “OnWorkflowActivated” event driven activity.
Let’s walk through the solution:
1. Drag a “EventDriven” activity to the “Init” state. Its name may defer in your project.
2. Name the EventDriven activity “onWorkflowActivatedHandler”. (You can use an other name too!)
3. Double click the EventDriven activity.
4. Drag a “OnWorkflowActivated” activity from the Toolbox pane into the “onWorkflowActivatedHandler” activity:
5. Add a correlation token to “onWorkflowActivated1”!!
6. Add a binding for “WorkflowProperties” !!!
7. That’s it. Now you can add activities behind “handleExternalEventActivity1”… As you like.
In this walkthrough I want to show you how to create a Sequential Workflow with Visual Studio 2010 for use in SharePoint 2010. – I will show how to create a custom Task Form for interaction with users. The Task form will be a native SharePoint list form. No InfoPath. There are many InfoPath samples out there but they cannot be used on a SharePoint Foundation 2010 platform. But workflows can be used on SharePoint Foundation 2010 too!
To reproduce the following steps you need to create a SharePoint site. – In the walkthrough I’ll use a Site Collection located at “http://sharepoint.local/sites/workflow”.
The “TemplateType” attribute represents the “Workflow History” list template. It resists on a SharePoint feature with ID “00BFEA71-4EA5-48D4-A4AD-305CF7030140”. It’s a native SharePoint feature.
You can add the attibute “Hidden” to the “ListInstance” tag and set it’s value to “TRUE” to hide the list as it’s done by SharePoint by default for this list. In this case you should also change “OnQuickLaunch” to “FALSE”. For my demo purpose I want to have “Workflow History” visible and on the Quick Launch bar.
5. Now we will create all tools we need for a “Workflow 1”. (May be I’ll create more workflow demos later. So it’s number 1.)
6. We create the SharePoint fields for “Workflow 1”. Therefore we create another “Empty Element” project item named “Workflow 1 Fields”.
I’ll create 3 fields for use in the Task Form we will create: Test1, Test2, Test3. They are all fields of type Text.
This specifies there to store the “Task1Form.aspx” file in the site structure.
9. In the next step we create the task list that will contain our workflow tasks.
First create a “List Definition” project item named “Workflow 1 Tasks”.
Use “Workflow 1 Tasks” as name of the list definition and “Tasks” as base type. Check “Add a list instance…”.
10. Now open “Elements.xml” of the new list definition project item.
We need to change the identifier of the list type we create! – It must be “107”. This is the list template ID for workflow tasks lists in SharePoint. The workflow designer will search for a list with this type inside the site where a new workflow will be created.
Here is the content of “Elements.xml” after our modification:
You see the “FormUrls” tag? Inside this tag we specify our custom form template we created before.
The new content type is derived from the “Workflow Task” content type 0x010801. – In the “FieldRefs” section we add our fields we need inside the workflow.
13. Now we need to add the field definitions of our custom fields to the “Schema.xml”. Copy them from the “Elements.xml” file of project item “Workflow 1 Fields” into the “Fields” tag of “Schema.xml”:
Furthermore you need to add a field definition for the field “Completed”:
Check “Do not prompt…”. Press “Resolve Automatically” – it’s your only option .
15. Have a look into the site using the browser. – We will test our “Edit” form. Remember that we did not specify special “New” form or “Display” form. This you could do the same way as you created the “Edit” form.
We see our lists in the Quick Launch.
Open the “Workflow 1 Tasks” list. On the Ribbon open the “Items” tab. We see our Content Type in the New Item submenu:
Create an item of this type. You see a standard “New” form and on it you see our three “Test” fields:
Enter some data and press “Save”.
Open the item in “Edit” form. Now you should see our custom list form.
If you click “Save As Draft” your changes will be stored in the task item. If you click “Complete Task” two item fields will be changed in addition to the changes made in the form: It sets “Status” to “Tasks_Completed” and “% Complete” to “100”. You can see this in the Code Behind file of the list form.
Test all buttons.
After “Complete Task”:
You see: “% Complete” is set to “100 %”.
So far our projects works as expected.
See Part 2 for the next steps… There I will show you how to create a simple Sequential Workflow that uses our Task Form.
In this walkthrough I want to show you how to create a Sequential Workflow with Visual Studio 2010 for use in SharePoint 2010. – I will show how to create a custom Task Form for interaction with users. The Task form will be a native SharePoint list form. No InfoPath. There are many InfoPath samples out there but they cannot be used on a SharePoint Foundation 2010 platform. But workflows can be used on SharePoint Foundation 2010 too!
To reproduce the following steps you need to create a SharePoint site. – In the walkthrough I’ll use a Site Collection located at “http://sharepoint.local/sites/workflow”.
16. Now we need to add a list that we use to connect a workflow with. We create a “List Definition” project item named “Workflow 1 Host List”.
We change the “Elements.xml” file of the List Instance project item.
We change the Title, URL and Description.
17. Deploy the project.
18. Now we start creating a Sequential Workflow.
Create a “Sequential Workflow” project item named “Workflow 1”.
Create the workflow as “List Workflow”.
You will see all the lists we created in the previous steps. Remember: You have to (successfully) deploy the project before you can use the lists that will be created during deployment. – Without previous deployment the wizard may fail before you see the following dialog page.
In the first dropdown list choose “Workflow 1 Host List”. All other lists should be selected automatically because there is only one usable list of every needed type.
Select all “Workflow start” options: “Manually”, “Item Created”, “Item Updated”.
You get:
19. We will add a “Create Task With Content Type” workflow activity that will use our “Workflow 1 Task 1” we created in the steps 1 to 15 (blog post “part 1”).
Open the “Toolbox” pane and drag “CreateTaskWithContentType” into the Workflow Designer.
After you did this you get:
Now we need to configure the workflow activity in the “Properties” pane.
Set “correlationToken” to “task1” and the sub element of “correlationToken” named “OwnerActivityName” to “Workflow_1”.
Now we need to create code behind properties for some activity properties. As example I’ll show how to create a code behind property for “ContentTypeId”.
Click on the Button “…” at the activity property edit box:
In the dialog select the tab “Bind to a new member”. Enter the name “New member name” and select the “Create Property” radio button. Press “OK”.
Here are the property bindings you need to create:
“ContentTypeId” = “task1ContentTypeId”
“TaskId” = “task1Guid”
“TaskProperties” = “task1Properties”
In “MethodInvoking” type “createTask1Invoke” and press ENTER. – You’ll be directed to the code editor.
(The activity properties look like this after you finished: )
In the method we initialize the task1-Properties we created before. The “ContentTypeId” is taken from the “Schema.xml” file of our “Workflow 1 Tasks” list where we created the Content Type “Workflow 1 Task 1”.
After that your method looks like this:
20. Back in the workflow designer. We drag a “While” activity from the Toolbox pane into the workflow behind the “Create Task” activity.
Into the “While” activity we drag a “Sequence” property from the Toolbox pane. We get:
Into the “Sequence” activity we drag a “OnTaskChanged” activity from the Toolbox pane. We get:
Now we edit the properties of the created “OnTaskChanged” activity. We have to set the “CorrelationToken” as described above. Additionally we specify new property bindings:
“TaskId” => Bind to the existing member “task1Guid”!!!
In the “Invoked” property add the method name “task1Changed1Invoke”. – The code editor will be opened.
(The activity properties look like this after you finished: )
21. In the code we add a property at class level:
This property we set inside the “OnWorkflowItemChanged” activity. It will contain the information whether the Workflow Task Item we created before was “Completed” by the assigned user.
In the “task1Changed1Invoke” method we set the “isFinished” property.
22. Now we need to specify the condition for the “While” activity. In the workflow designer select this activity. In the “Condition” property select “Code Condition” and expand the property. Enter “while1Invoke” in the subproperty with (the same) name “Condition”.
In the code editor enter the code for “while1Invoke” like this:
The “e.Result” property has to be “TRUE” as long as the while loop should run. It should not run anymore (“e.Result = false”) if “isFinished” is TRUE.
23. In the workflow designer add a “CompleteTask” activity behind the “While” activity. Drag the “CompleteTask” activity from the Toolbar pane into the workflow designer. We get:
Select the “completeTask1” activity and edit it’s properties in the Property pane.
Bind “TaskId” to the existing member “task1Guid”
Create a new member namend “task1Outcome” for binding to “TaskOutcome”
Set the “CorrelationToken” to “task1”
24. Whew!
25. Deploy the project. – It deployed successfully ?! – Congratulations!
26. Open the browser and navigate to “http://sharepoint.local/sites/workflow”. Open the “Workflow 1 Host List”. Add a new item… – The “Workflow 1” should start automatically.
(After F5 / page refresh:)
Click “In Process”. We see the Workflow Status page:
In the “Tasks” section open the context menu of the element “(no title)”. – We did not set a title for the created “Task 1”. This could be done in the method “createTask1Invoke”.
Click “Edit Item”.
There it is!!! :
Just edit a property and click “Save As Draft”.
On the Workflow Status page you see that the workflow is still running but was modified (“Last run”).
Then edit the task again. Click “Complete Task”.
After that the workflow is “Completed”:
27. We have done it! – Now we could add a “Task 2” for this “Workflow 1” where some other data is requested from users.
With that method you will be able to add custom forms for Workflow Tasks. You could also use forms you create with Silverlight !!!
I had to develop a Holiday Approval Workflow with SharePoint Designer 2010. – I used a “List Workflow”. – Bad mistake! – It’s not possible to copy this kind of workflow to another list on the same site or another site.
I thought I could use the “Export to Visio” functionality with the workflow at my development site and the “Import from Visio” functionality at the destination list. – That’s impossible. You get this message:
(“This workflow cannot be imported because it was created in SharePoint Designer for a different site, or the original workflow has been moved or deleted. To move a workflow between sites, use Save as Template (.wsp file) instead of a Visio workflow drawing.”)
You cannot “Save as Template” because it’s a “List Workflow”.
Okay. – I’ve found a workaround.
Let’s do it in detail.
1. Create a site collection “http://sharepoint.local/sites/dev”.
2. Open SharePoint Designer 2010. Open the previously created site.
3. Open the “Workflows” view.
4. On the Ribbon click “List Workflow” and select “Shared Documents”. – We will create a workflow for the list “Shared Documents”.
Name the workflow “Workflow 1”. Leave “Description” empty.
5. Insert any workflow steps you like.
6. Change the workflow settings. Select “Task List” and “History List”.
7. Click “Publish” to publish the workflow
8. Click “Export to Visio”. Save to Workflow as .vwi file, e.g. on the desktop.
9. Let’s test the workflow. – I’ve created a text file “test.txt” on the desktop. Now I upload the file to the “Shared Documents” library of “http://sharepoint.local/sites/dev”.
Open the item context menu. Click “Workflow”.
Manually start the “Workflow 1”.
*
Click “Start”.
In the list view you see the workflow status.
Click on the status. In my case it’s “Completed”.
The workflow history says “Workflow executed” as specified in the workflow definition above.
10. Okay. Let’s copy the workflow to the production site “http://sharepoint.local”. – A simple “import” is impossible, because it’s a list workflow… Follow my steps.
11. Start the SharePoint Designer 2010 and open the site “http://sharepoint.local”.
12. On the Ribbon click “List Workflow” and select “Shared Documents”. – We will create a workflow for the list “Shared Documents”.
Name the workflow “Workflow 1 Production”. Leave “Description” empty.
11. Do not add any workflow step!
12. Click on “Workflow Settings” menu item on the Ribbon.
13. Click “Publish” to publish the (empty) workflow.
14. After publishing click “Export to Visio” and save the file as “workflow 1 production.vwi” on the desktop.
15. Open a Windows Explorer. Select “Desktop”. You see both “.vwi” files.
16. Add the extension “.zip” to both files.
17. Open both “.vwi.zip” files. You see:
18. Copy “workflow.xoml.wfconfig” from “workflow 1 production.vwi.zip” to “workflow 1.vwi.zip”.
19. Now remove the “.zip” extension from both files. You’ll get two “.vwi” files.
20. In SharePoint Designer 2010 open the site “http://sharepoint.local”.
21. Click “Import from Visio” and select “workflow 1.vwi”
Click “Next”. (Maybe you get a message: “You must close all views of this workflow before importing this file.” Then stop the import. Close all views of the Workflow and restart from Step 21.)
22. Now you see the this window:
That it!
23. Now the workflow designer opens:
24. Now you can publish the workflow and test it…
25. Be as happy as me with that !!!
PS: Of course you can use any library as destination. In the sample above it does not have to be “Shared Documents” on “http://sharepoint.local” It could be another list or library as well!