Sunday, July 13, 2008

Handling Sharepoint List Events

In May I have blogged about the Search Crawler Custom Workflow. What it basically does is that assume there is a newsletter list which contains a URL field, when a new URL is added to the newsletter list, the workflow will kick start and it will crawls the URL and put it into content source for search later.

In this post, I will talk about how you can handle the newsletter list event to delete the content source when an associated URL is deleted. You can handle the List level event as well as List Item level event. There are Synchronous Events and Asynchronous Events, please take a look at my
blog for more list event details.

Steps:

1. Create a C# class library and create the ItemEventReceivers which handles the ItemDeleting event for the newsletter list. Note that the "external newsletter" in the code below is the URL field name of the newsletter list.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.Office.Server.Search.Administration;

Namespace ATSListReceiver

{

class ItemEventReceivers : SPItemEventReceiver

{

public override void ItemDeleting(SPItemEventProperties properties)

{

base.ItemDeleting(properties);

//Disable events to prevent an endless loop of event handling!

this.DisableEventFiring();

//******************Step 1: Get the site search context*****************************

//string strURL = @"http://mysiteurl.com.au/";

SearchContext context;

using (SPSite site = new SPSite(strURL))

{

context = SearchContext.GetContext(site);

}

//******************Step 2: Get the Newletter url newsletter list*****************************

String url = properties.ListItem["external newsletter"].ToString();

url = url.Substring(0, url.IndexOf(','));

//******************Step 3: Delete the content source *****************************

Content sspContent = new Content(context);

ContentSourceCollection sspContentSources = sspContent.ContentSources;

if (sspContentSources.Exists(url))

{

ContentSource cs = sspContentSources[url];

cs.Delete();

}

this.EnableEventFiring();

}

}

}


2. Give this project a strong name and deploy this assembly to the GAC.

3. Create a console application to add the handler to the newsletter list.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

Namespace ATSAddHandler

{

Class Program

{

static void Main(string[] args)

{

Add_EventHandler_To_Newsletter();

}

public static void Add_EventHandler_To_Newsletter()

{

SPSecurity.RunWithElevatedPrivileges(delegate()

{

string assemblyName = "ATSListReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=dfb38e12474cae53";

string className = "ATSListReceiver.ItemEventReceivers";

SPSite rootSite = new SPSite("http://mysiteurl.gov.au");

SPWeb web = rootSite.AllWebs[""];

web.AllowUnsafeUpdates = true;

SPList newsletter = web.Lists["ATSNewsletter"];

newsletter.EventReceivers.Add(SPEventReceiverType.ItemDeleting, assemblyName, className);

newsletter.Update();

Console.Write("done!");

Console.ReadLine();

});

}

}

}


4. For the purpose of this demo, I used console application, you can also do this in you STSDEV solution and implements this as a List Receiver.

Reference:

Sharepoint 2007: List Events Practical Example: Creating a rigged survey!!

blog comments powered by Disqus