Event Handler to Synchronize (Populate Data) List / Document Library Columns Form Other Document Library / List Columns

We have created a solution for one of our clients. The solution is based on uploading documents and attaching workflows with the documents.

Uploaded documents have different columns describing the document’s meta data. Our clients’ wants that these column in the task list.

We decide to write two event handlers. One was against the list and other was against the document library.

The event handler against list runs when a task is created in the task list. It fetches the information from the desired columns in the document library and populates the columns in the task list row.

The code is:

 public class MyClass : SPItemEventReceiver    {       

public override void ItemAdded(SPItemEventProperties properties)        {           

SPListItem item = properties.ListItem;                       

SPFieldUrlValue MyURL = null;           

SPView DefaultView = null;           

SPQuery SelectQuery = null;           

SPListItemCollection SelectedDoc = null;            

string URLText = null;           

string URL = null;                        

SPWeb site = (SPWeb)properties.OpenWeb();           

SPList DocLib = site.Lists[“Document Library Name”];                

MyURL = new SPFieldUrlValue((string)item[“Link”]);           

URL = MyURL.Url; URLText = URL.Substring(URL.LastIndexOf(‘/’) + 1);         

DefaultView = DocLib.DefaultView;            

SelectQuery = new SPQuery(DefaultView);            

SelectQuery.Query = “<Where><Eq><FieldRef Name=’FileLeafRef’/><Value Type=’Text’>” + URLText + “</Value></Eq></Where>”           

SelectedDoc = DocLib.GetItems(SelectQuery);  

foreach (SPListItem doc in SelectedDoc) // run for once            {                  

item[“Col 1”] = doc[“Col 1”];                 

item[“Col 2”] = doc[“Col 2”];                 

item[“Col 3”] = doc[“Col 3”];                 

item.Update();           

}              

site.Dispose();                     

}

}

The second event handler was written against the document library for synchronization purpose. If user changes / update the data in the document library column, these changes must be reflected in the task list column. The event handler capture and runs against the update vent.

The code is:

 public class MYClass1 : SPItemEventReceiver    {       

public override void ItemUpdated(SPItemEventProperties properties)        {            

string DocName = null;           

string DocTitle = null;            

SPView DefaultView = null;           

SPQuery SelectQuery = null;           

SPListItemCollection SelectedDoc = null;                       

SPListItem doc = properties.ListItem;           

SPWeb site = properties.OpenWeb();           

SPList TaskList = site.Lists[“Task List Name”];            

DocName = doc[“Name”].ToString();                       

DocTitle = DocName.Substring(0,DocName.LastIndexOf(‘.’));            

DefaultView = TaskList.DefaultView;            

SelectQuery = new SPQuery(DefaultView);             

SelectQuery.Query = “<Where><Eq><FieldRef Name=’LinkTitle’/><Value Type=’Text’>Please approve “ + DocTitle + “</Value></Eq></Where>”           

SelectedDoc = TaskList.GetItems(SelectQuery);            

foreach (SPListItem task in SelectedDoc)             {               

task[“Col 1”] = doc[“Col 1”];               

task[“Col 2”] = doc[“Col 2”];               

task[“Col 3”] = doc[“Col 3”];               

task.Update();           

}           

site.Dispose();       

}

}

In this way, changes in the columns of document library will be immediately visible in the columns of task list as well.  

In case you don’t know how to create and deploy event handler, follow this link.

Advertisements

8 Comments »

  1. Santhi.c said

    I need to do the same thing using built in web services in sharepoint 2007.
    Please provide any code if u have
    thanks

  2. Anil said

    Am Using MOSS2007. Shall we do the same thing as adding items in the document library and the same time Event handler should trigger to add item to an SPList. Here i mean my Document Library and SPList contains same Fieldnames as for say Document_Name, Document_Url.

  3. Farhan Faiz said

    yes. we can do that.

  4. Biju said

    Hi,

    Am new to SharePoint, I have undergone the training on WSS and MOSS.

    Can anyone please tell me how to Customize the exisitng Task List and Event Handler for Task Lists.

    I want to update the tasklist based on the date and time of start and completion

    Biju

    biju.hsn@gmail.com

  5. Farhan Faiz said

    Any more details? starts and ends date of document or workflow ?

  6. Biju said

    Hi Farhan,
    Thanks for your response.

    Actually my requirement is to create event handler for task lists in projects workflow.

    The event handlers should be managing the events. The event handler should handle new tasks and changes made to exisiting tasks.
    •New task
    •Deleted task
    •Changed start/due/finished date or duration of existing task
    •Added/changed/removed dependency

    Biju
    biju.hsn@gmail.com

  7. embersCek said

    Lots of folks talk about this issue but you said some true words.

  8. Pretty nice post. I just stumbled upon your blog and wanted
    to mention that I’ve really loved browsing your blog posts. After all I will be subscribing to your feed and I’m hoping you write again
    very soon!

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: