This time a tiny neat walkthrough of how to add an Event Receiver at runtime in SharePoint 2010.
Let’s say you have a SharePoint site that your colleagues already use. In this site you have an existing list. Now you want to add some automation to this existing list. – You cannot deploy the list as List Definition w/ List Instance again in a VS 2010 SharePoint project, because the list exists and the data must not be touched.
One solution is to add an List Event Receiver that is contained in a VS2010 solution package.
1. You create a Empty SharePoint 2010 project in Visual Studio 2010.
2. Now you add an “Event Receiver” project item
3. Now you add the events you want to handle. Select “List Item Events” and “Custom List”.
4. Implement some functionality in the newly created Event Receiver class.
5. Now create or open an Feature Event Receiver for the SharePoint feature that will configure the event receiver. – You have to create a new feature or use an existing feature… If you create a new feature event receiver you have to uncomment the methods “FeatureActivated” and “FeatureDeactivating”.
6. Add this code to the “FeatureActivated” method:
try {
SPWeb web = (SPWeb)properties.Feature.Parent;
SPList l = web.Lists["My SharePoint List"];
if( l != null )
{
bool found = false;
foreach( SPEventReceiverDefinition er in l.EventReceivers )
{
if( er.Class == "Full.Namespace.Qualified.Class.Name.Of.Your.Event.Receiver.Class") {
found = true;
break;
}
}
if( !found )
{
SPEventReceiverDefinition newERD = l.EventReceivers.Add();
//the next line is only valid if the event receiver class is in the same assembly as the feature event receiver!!!
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 !!!