Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to cancel the event in Item Adding and display javascript message and prevent the page from redirecting to the SharePoint Error Page?

$
0
0

How to cancel the event in Item Adding without going to the SharePoint Error Page?

Prevent duplicate item in a SharePoint List

The following Event Handler code will prevent users from creating duplicate value in "Title" field.

ItemAdding Event Handler

public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
if (properties.ListTitle.Equals("My List"))
{
try
{
using(SPSite thisSite = new SPSite(properties.WebUrl))
{
SPWeb thisWeb = thisSite.OpenWeb();
SPList list = thisWeb.Lists[properties.ListId];
SPQuery query = new SPQuery();
query.Query = @"<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + properties.AfterProperties["Title"] + "</Value></Eq></Where>";
SPListItemCollection listItem = list.GetItems(query);
if (listItem.Count > 0)
{
properties.Cancel = true;
properties.ErrorMessage = "Item with this Name already exists. Please create a unique Name.";
}
}
}
catch (Exception ex)
{
PortalLog.LogString("Error occured in event ItemAdding(SPItemEventProperties properties)() @ AAA.BBB.PreventDuplicateItem class. Exception Message:" + ex.Message.ToString());
throw new SPException("An error occured while processing the My List Feature. Please contact your Portal Administrator");
}
}
}

Feature.xml


<?xml version="1.0" encoding="utf-8"?>
<Feature Id="1c2100ca-bad5-41f5-9707-7bf4edc08383"
Title="Prevents Duplicate Item"
Description="Prevents duplicate Name in the "My List" List"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Web"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml"/>
</ElementManifests>
</Feature>

Element.xml


<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="100">
<Receiver>
<Name>AddingEventHandler</Name>
<Type>ItemAdding</Type>
<SequenceNumber>10000</SequenceNumber>
<Assembly>AAA.BBB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8003cf0cbff32406</Assembly>
<Class>AAA.BBB.PreventDuplicateItem</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>

Below link explains adding the list events.

http://www.dotnetspark.com/kb/1369-step-by-step-guide-to-list-events-handling.aspx

Reference link:

http://msdn.microsoft.com/en-us/library/ms437502(v=office.12).aspx

http://msdn.microsoft.com/en-us/library/ff713710(v=office.12).aspx


Amalaraja Fernando,
SharePoint Architect
Please Mark As Answer if my post solves your problem or Vote As Helpful if a post has been helpful for you. This post is provided "AS IS" with no warrenties and confers no rights.







Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>