Wednesday, June 18, 2008

MOSS 2007 : Implement Task - Task Activities (Parent-Child Relationship) on the same List

The Idea here is :
I want to implement a Task-Task Activities (Parent-Child Relationship on the same list) on Sharepoint 2007 Custom List. The requirement says that every Task may have one or more Task Activity.

How to Implement it :

From the requirement, an Idea came to my mind by creating a Task content type that inherits from Folder content type, becos in our case the Task is a container of Task Activities. So :

1) Create a Task Content Type that inherits from Folder Content Type. Then add fields like (Assigned To , Status , Priority … etc ) as you want.
2) Create a Task Activity Content Type that inherits from Item Content Type and add fields as you want. in my case I added Activity Description field.
3) Attach the two Content types to your Custom List, so that only those type of contents are allowed in your List.

Till this moment, if you click the new button of the list you will find yourself able to create a Task or Task Activity on the Same level, but the requirement says that the Task is a container of Task Activities, so you should have one option when you press new button which is (Create New Task), and when you enter one level more inside a specific Task you should see the Task Activities for that Task and have also one option which is (Create New Task Activity)

4) From the List Advanced Settings, change the content types to have the Task content type available on the most top level.

5) Create new view that will show the Task Activities whenever a user select specific Task.

6) But what about the Task Activity content type? Its hidden!

7) Till this moment, you cannot create Task Activity, to do this , we need to develop a Feature on Visual Studio that will execute some code whenever a new Task is added to the list to have the create Task Activity option available when the user go one level down by selecting specific Task.

To Create the above mentioned Feature:
1) Create a Class Library Project.

2) Add reference to Microsoft.Sharepoint DLL.

3) Add a class that inherit from SPItemEventReceiver, override the ItemAdded method and implement the following code:

public override void ItemAdded(SPItemEventProperties properties)
using (SPWeb web = properties.OpenWeb())//Open the web you want to use
SPContentType TaskCT = web.ContentTypes["TaskCT"];

if (TaskCT != null)

// Check if the Added Item Belongs to Task Content T ype
if (properties.ListItem.ContentType.Name == TaskCT .Name)
SPList TaskList = web.Lists[properties.ListId];

// Get folder in the list and a content type.
SPFolder TaskFolder = TaskList .RootFolder.SubFolders[properties.AfterProperties["Title"].ToString()];
SPContentType TaskActivityCT = TaskList .ContentTypes["TaskActivityCT"];
List contentTypes = new List();

// Set the content type order for the Task Folder and update
TaskFolder.UniqueContentTypeOrder = contentTypes;

4) Attach the event handler with your list by creating a Class that inherits from SPFeatureReceiver and override FeatureActivated , FeatureDeactivating methods.
5) Create the feature.xml.
6) Install your feature using the stsadm command line for installing features.
7) Activate your feature.

enjoy :)

see this article also MOSS 2007 : Access Denied exception even when using SPSecurity.RunWithElevatedPrivileges Method

Mohammed Barakat Kharboush

No comments: