Tag: Visual Studio 2010

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”.

Here I’ll show you how to create a List Form for a SharePoint List using InfoPath 2010. Therefore you need SharePoint 2010 Server with Enterprise CALs! – With InfoPath Forms Services it’s possible to render List Forms (“New”, “Edit” and “Display” forms) as browser enabled InfoPath forms. This is really, really cool!

During deployment this creates the form aspx pages that will host the InfoPath Web Part that renders the form at runtime.

In the XML code there are several references to the destination list. All of them have to be set to “Lists/TestList1”. All the code needs to remain unmodified. Don’t change “WPTypeId” attributes! “{b1dc92e2-8558-f555-ae81-35ed9ddf1644}” is the identifier for InfoPath Render Web Part.

BUT change the “ID” attributes of the three “WebPart” tags! Use the “Create GUID” tool from the “Tools” menu of Visual Studio 2010.

15. Now open the “Schema.xml” file of the List Definition project item of “Test List 1”. Add the following code at the before the closing tag of our list content type.

23. In InfoPath 2010 you’ll see that “Title” cannot be removed. To remove it you would need the edit the xsn file with Notepad: XSN is a CAB file. So you can rename it to cab, extract it’s content, modify the “manifest.xfs” file and compress the folder as CAB file named “template.xsn”… (If you need assistance on this please post as comment.)

In the “Fields” pane select “Title”.

(This is the “advanced view” !)

Click the arrow behind the element. Click “Properties” in the context menu.

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.)

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:

1:<?xmlversion="1.0"encoding="utf-8"?>

2:<Elementsxmlns="http://schemas.microsoft.com/sharepoint/">

3: <ListTemplate

4:Name="Workflow 1 Tasks"

5:Type="107"

6:BaseType="0"

7:OnQuickLaunch="TRUE"

8:SecurityBits="11"

9:Sequence="360"

10:DisplayName="Workflow 1 Tasks"

11:Description="Tasks of Workflow 1"

12:Image="/_layouts/images/itgen.png"/>

13:</Elements>

11. Now we modify the “Elements.xml” file of the list instance that will be created during deployment:

Here we also need to modify the type identifier to “107”. Furthermore we change the list url: “Lists/Workflow1Tasks”.

Here is the complete content of “Elements.xml”:

1:<?xmlversion="1.0"encoding="utf-8"?>

2:<Elementsxmlns="http://schemas.microsoft.com/sharepoint/">

3: <ListInstanceTitle="Workflow 1 Tasks"

4:OnQuickLaunch="TRUE"

5:TemplateType="107"

6:Url="Lists/Workflow1Tasks"

7:Description="Tasks of Workflow 1">

8: </ListInstance>

9:</Elements>

In a “real world” scenario we would prevent the list from being listed on the Quick Launch bar. So the corresponding parameter must be set to “FALSE”.

12. Now we need to modify the “Schema.xml” file of the list definition.

First we set the list type to “107” and configure some other attributes:

1:<Listxmlns:ows="Microsoft SharePoint"Title="Workflow 1 Tasks"

2:FolderCreation="FALSE"Direction="$Resources:Direction;"

3:EnableContentTypes="TRUE"VersioningEnabled="TRUE"

4:Url="Lists/Workflow1Tasks"

5:Type="107"BaseType="0"

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

7:[…]

Now remove the the content types defined in the “ContentTypes” tag in the “schema.xml” file.

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.

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:

“AfterProperties” = “task1Changed1_AfterProperties”

“BeforeProperties” = “task1Changed1_BeforeProperties”

“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”

DISCLAIMER

The informations on my blog are provided as is and I cannot guarantee that editing the registry or executing the provided code or provided scripts or doing other actions described in my articles will not cause serious damage. You use this information at your own risk! - I'm not responsible content on external sites.