First published on TECHNET on Aug 01, 2012
This post is a contribution from Bharat Rathod, an engineer with the SharePoint Developer Support team.
By default, SharePoint 2010 allows you to add multiple items in the calendar for the same date and time. Below screenshot shows that Meeting1 and Meeting2 occur at the same date and time for the same user.
In real world scenario, we do not let a user have multiple entries of meetings at the same date and time. For example, Outlook warns the user of the clashes of the meeting that occur at the same date and time.
Here’s a sample feature that will prevent this from occurring in your Calendar list.
The Feature.xml file
<Feature Scope="Site"
Title="Simple Event Handler Registration"
Id="853A4952-122D-4F97-8239-A2B02FFFDA68"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="Elements.xml"/>
</ElementManifests>
</Feature>
The Elements.xml file
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="106">
<Receiver>
<Name>doublemeeting</Name>
<Type>ItemAdding</Type>
<SequenceNumber>10001</SequenceNumber>
<Assembly>doublemeeting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30c59cdee5e7a547</Assembly>
<Class>doublemeeting.PreventDoubleBooking</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
<Receiver>
<Name>doublemeeting</Name>
<Type>ItemUpdating</Type>
<SequenceNumber>10001</SequenceNumber>
<Assembly>doublemeeting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30c59cdee5e7a547</Assembly>
<Class>doublemeeting.PreventDoubleBooking</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>
And the complete code file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace doublemeeting
{
public class PreventDoubleBooking : SPItemEventReceiver
{
/// <summary>
/// This event is triggered when the user adds a new item
/// </summary>
/// <param name="properties"></param>
public override void ItemAdding(SPItemEventProperties properties)
{
//Our query string variable
string strQuery = null;
try
{
//Get the Sharepoint site instance
using (SPWeb oWebsite = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
{
//Get the collection of properties for the Booking item
SPListItemCollection collItems = oWebsite.Lists[properties.ListTitle].Items;
//Get the Calendar List that we will be querying against
SPList calendar = oWebsite.Lists[properties.ListId];
//Get the internal name of the fields we are querying.
//These are required for the CAML query
string start_internal = collItems.List.Fields["Start Time"].InternalName;
string end_internal = collItems.List.Fields["End Time"].InternalName;
string Category_Internal = collItems.List.Fields["Category"].InternalName;
//Get the query string parameters
string start_str = properties.AfterProperties[start_internal].ToString();
string end_str = properties.AfterProperties[end_internal].ToString();
string Category_str = properties.AfterProperties[Category_Internal].ToString();
//Construct a CAML query
SPQuery query = new SPQuery();
//Create the CAML query string that checks to see if the booking we are attemping
//to add will overlap any existing bookings
strQuery = string.Format(@"
<Where>
<And>
<Or>
<Or>
<And>
<Leq>
<FieldRef Name='EventDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value>
</Leq>
<Gt>
<FieldRef Name='EndDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value>
</Gt>
</And>
<And>
<Lt>
<FieldRef Name='EventDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value>
</Lt>
<Geq>
<FieldRef Name='EndDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value>
</Geq>
</And>
</Or>
<Or>
<And>
<Leq>
<FieldRef Name='EventDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value>
</Leq>
<Geq>
<FieldRef Name='EndDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value>
</Geq>
</And>
<And>
<Geq>
<FieldRef Name='EventDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value>
</Geq>
<Leq>
<FieldRef Name='EndDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value>
</Leq>
</And>
</Or>
</Or>
<Eq>
<FieldRef Name='Category' />
<Value Type='Choice'>{2}</Value>
</Eq>
</And>
</Where>
<OrderBy>
<FieldRef Name='EventDate' />
</OrderBy>
", start_str, end_str, Category_str);
//Set the query string for the SPQuery object
query.Query = strQuery;
//Execute the query against the Calendar List
SPListItemCollection existing_events = calendar.GetItems(query);
//Check to see if the query returned any overlapping bookings
if (existing_events.Count > 0)
{
//Cancels the ItemAdd action and redirects to error page
properties.Cancel = true;
//Edit the error message that will display on the error page
properties.ErrorMessage += "This booking cannot be made because of one or more bookings in conflict. <BR><BR>";
//Here you can loop through the results of the query
//foreach (SPListItem oListItem in existing_events)
//{
// ....
//}
properties.ErrorMessage += "Please go back and schedule a new time.";
}
}
}
catch (Exception ex)
{
//Cancels the ItemAdd action and redirects to error page
properties.Cancel = true;
//Edit the error message that will display on the error page
properties.ErrorMessage = "Error looking for booking conflicts: " + ex.Message;
}
}
/// <summary>
/// This event is triggered when the user edits an calendar item
/// </summary>
/// <param name="properties"></param>
public override void ItemUpdating(SPItemEventProperties properties)
{
string strQuery = null;
try
{
//Get the Sharepoint site instance
using (SPWeb oWebsite = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
{
//Get the collection of properties for the Booking item
SPListItemCollection collItems = oWebsite.Lists[properties.ListTitle].Items;
//Get the Calendar List that we will be querying against
SPList calendar = oWebsite.Lists[properties.ListId];
//Get the internal name of the fields we are querying.
//These are required for the CAML query
string start_internal = collItems.List.Fields["Start Time"].InternalName;
string end_internal = collItems.List.Fields["End Time"].InternalName;
string Category_Internal = collItems.List.Fields["Category"].InternalName;
string guid_internal = collItems.List.Fields["GUID"].InternalName;
//Get the query string parameters
string start_str = properties.AfterProperties[start_internal].ToString();
string end_str = properties.AfterProperties[end_internal].ToString();
string Category_str = properties.AfterProperties[Category_Internal].ToString();
string guid_str = properties.AfterProperties[guid_internal].ToString();
//Construct a CAML query
SPQuery query = new SPQuery();
//Create the CAML query string that checks to see if the booking we are attemping
//to change will overlap any existing bookings, OTHER THAN ITSELF
strQuery = string.Format(@"
<Where>
<And>
<And>
<Or>
<Or>
<And>
<Leq>
<FieldRef Name='EventDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value>
</Leq>
<Gt>
<FieldRef Name='EndDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value>
</Gt>
</And>
<And>
<Lt>
<FieldRef Name='EventDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value>
</Lt>
<Geq>
<FieldRef Name='EndDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value>
</Geq>
</And>
</Or>
<Or>
<And>
<Leq>
<FieldRef Name='EventDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value>
</Leq>
<Geq>
<FieldRef Name='EndDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value>
</Geq>
</And>
<And>
<Geq>
<FieldRef Name='EventDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{0}</Value>
</Geq>
<Leq>
<FieldRef Name='EndDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>{1}</Value>
</Leq>
</And>
</Or>
</Or>
<Eq>
<FieldRef Name='Category' />
<Value Type='Choice'>{2}</Value>
</Eq>
</And>
<Neq>
<FieldRef Name='GUID' />
<Value Type='GUID'>{3}</Value>
</Neq>
</And>
</Where>
<OrderBy>
<FieldRef Name='EventDate' />
</OrderBy>
", start_str, end_str, Category_str, guid_str);
//Set the query string for the SPQuery object
query.Query = strQuery;
//Execute the query against the Calendar List
SPListItemCollection existing_events = calendar.GetItems(query);
//Check to see if the query returned any overlapping bookings
if (existing_events.Count > 0)
{
//Cancels the ItemAdd action and redirects to error page
properties.Cancel = true;
//Edit the error message that will display on the error page
properties.ErrorMessage += "This booking cannot be made because of one or more bookings in conflict. <BR><BR>";
//Here you can loop through the results of the query
//foreach (SPListItem oListItem in existing_events)
//{
// ....
//}
properties.ErrorMessage += "Please go back and schedule a new time.";
}
}
}
catch (Exception ex)
{
//Cancels the ItemAdd action and redirects to error page
properties.Cancel = true;
//Edit the error message that will display on the error page
properties.ErrorMessage = "Error looking for booking conflicts: " + ex.Message;
}
}
}
}
Below are the source code and the feature file samples for your reference.
Feature: DoubleMeeting Feature
Sample source code: DoubleMeetingSource
Hope this post was helpful!
Welcome to the SharePoint Blog! Learn best practices, news, and trends directly from the SharePoint team.