Got this as a request from a reader- how to prevent users from adding items with same titles as ones that already exist in the list. using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace SPSTIPS.SharePoint.EventHandlers
{
public class TitlePrimaryEventHandler:SPItemEventReceiver
{
const string TITLE_QUERY = @"
public override void ItemAdding(SPItemEventProperties properties)
{
if (properties.AfterProperties["Title"] != null)
{
//get the title of the new item
string currentTitle = properties.AfterProperties["Title"].ToString();
//get the web site object
using (SPWeb web = properties.OpenWeb())
{
//get the current list
SPList list = web.Lists[properties.ListId];
//query the list to check if there are items with the same title
SPQuery q = new SPQuery();
q.Query = string.Format(TITLE_QUERY, currentTitle);
SPListItemCollection itemsWithSameTitle = list.GetItems(q);
//if there are items, cancel the add, and show an error to the user.
if (itemsWithSameTitle.Count > 0)
{
properties.Cancel = true;
properties.ErrorMessage = "There is already an item with the title \"" + currentTitle + "\ in this list".";
}
}
}
}
}
}
Monday, October 26, 2009
Sample event handler to set a field as a pr imary key (enforce no duplicates)
Posted by Rami Reddy Annapu Reddy at 4:56 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment