Example of Event Listener

This is another post from MSDN.

This event listener hooks into the ItemAdding and ItemUpdating event of a custom list and see if the ListTitle is valid; if valid, then allow to create or update it; else, don’t allow it and display an error message.

—————————————————————————————————————-
using System;

using System.Security.Permissions;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Security;

using Microsoft.SharePoint.Utilities;

using Microsoft.SharePoint.Workflow;

 

namespace VerifyJob.EventReceiver1

{


///
<summary>


/// List Item Events


///
</summary>


public
class
EventReceiver1 : SPItemEventReceiver

{


///
<summary>


/// An item is being added.


///
</summary>


public
override
void ItemAdding(SPItemEventProperties properties)

{


try

{


bool allowed = true;

 


if (properties.ListTitle == “Open Positions”)

{

allowed = checkItem(properties);

}

 


if (!allowed)

{

properties.Status = SPEventReceiverStatus.CancelWithError;

properties.ErrorMessage = “The job you have entered is not defined in the Job Definitions List”;

properties.Cancel = true;

}

}


catch (Exception ex)

{

properties.Status = SPEventReceiverStatus.CancelWithError;

properties.ErrorMessage = ex.Message;

properties.Cancel = true;

}

}

 

 


///
<summary>


/// An item is being updated.


///
</summary>


public
override
void ItemUpdating(SPItemEventProperties properties)

{

 


bool allowed = true;

 


if (properties.ListTitle == “Open Positions”)

{

allowed = checkItem(properties);

}

 


try

{

 


if (!allowed)

{

properties.Status = SPEventReceiverStatus.CancelWithError;

properties.ErrorMessage = “The job you have entered is not defined in the Job Definitions List”;

properties.Cancel = true;

}

}


catch (Exception ex)

{

properties.Status = SPEventReceiverStatus.CancelWithError;

properties.ErrorMessage = ex.Message;

properties.Cancel = true;

}

}

 

 


bool checkItem(SPItemEventProperties properties)

{


string jobTitle = properties.AfterProperties[“Title”].ToString();


bool allowed = false;


SPWeb jobDefWeb = null;


SPList jobDefList;


SPUser privilegedAccount = properties.Web.AllUsers[@”SHAREPOINT\SYSTEM”];


SPUserToken privilegedToken = privilegedAccount.UserToken;


try

{


using (SPSite elevatedSite = new
SPSite(properties.Web.Url, privilegedToken))

{


using (SPWeb elevatedWeb = elevatedSite.OpenWeb())

{

jobDefWeb = elevatedWeb.Webs[“JobDefinitions”];

jobDefList = jobDefWeb.Lists[“Job Definitions”];


foreach (SPListItem item in jobDefList.Items)

{


if (item[“Title”].ToString() == jobTitle)

{

allowed = true;


break;

}

}

}

}


return (allowed);

}


finally

{

jobDefWeb.Dispose();

}

}

 

}

}


 

Advertisements
Post a comment or leave a trackback: Trackback URL.

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: