Using the Event Handler in SharePoint 2010

As organizations increase their use of SharePoint, users need more customized forms of solutions to address their requirements. One such requirement is this: Whenever a Microsoft Word document is added to a document library, users want an exclusive "Tasks" list specifically for this document.

Consider a scenario where document is added to the document library after an important meeting (e.g., a minutes-of-the-meeting document), then all the team members want tasks assignment later and may want to use this newly created task list.

In this tip, you will learn how to use the SharePoint 2010 event handler, namely "ItemAdded," to make sure that a SharePoint "Task List" is automatically created whenever a document of type "MS Word" is added to a document library. This will make sure the users can later use this task list to allocate tasks among the team member's whenever they like.

1. Fire up a Visual Studio 2010 and create a new project of type SharePoint Event Receivers.

2. Select "List Item Events" and "Document Library" from the drop downs and then select "Item is added". Click "Finish". This will create the code file and feature that will deploy this "Event Receiver.

3. The first step is to make sure that we only perform actions when the document/item is added to the document library and it's a text document (i-e file extension is ".txt"). The first part is already covered as "ItemAdded" event is used , for the second part the check need to be done as shown in figure below.

4. Next, you need to make there is a task list created for this document, for this method "EnsureTaskList" is created as shown below.

This method first made sure that the event firing is disabled, this is done by using "EventFiringEnabled" property to "false", and this ensures there will be no event recursion. Next the extension of the document is checked, this is done by checking the "Name" field of "SPListItem".

Lastly call to "EnsureTasksList" is made, this method as shown below will first check if the task list named "Task for Document:" followed by the document display name exists or not, for this method "TryGetList" is used which takes the name of the list. It either returns the List if it exists or null in case it does not exists.

If the list does not exist, this code creates will create list by using "Add" method of "ListItemCollection" class. This method returns the "Guid" of the newly created list, which is not used in the code below.

5. Hit "F5", to build and deploy the solution. Try to browse to any document library and upload a MS Word file (with "doc" or "docx" extension).Make sure that the document is added to the document library successfully, as shown below. Note that it may few minutes before you finally see the document in the document library; this is because behind the scenes the SharePoint is creating a new "Task" list. However, next time if you upload same document, it will not take this extra time.

6. Now Go to "SiteActions ' View All Content", on this page you should see the new SharePoint "Task" list with the title "Tasks for the Document:" followed by name of the document you had added in previous step to the document library. Following figure shows the task list named "Tasks for the Document: Minutes of the Meeting (Feb - 2010)" as shown below.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.