The following Event Handler code will prevent users from creating duplicate value in "Title" field.
ItemAdding Event Handler
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 = @""; 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> <font color="red">Element.xml</font> <?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> " + properties.AfterProperties["Title"] + "
I love what you guys are usually up too. This kind of clever work and coverage!
ReplyDeleteKeep up the excellent works guys I've added you guys to
my personal blogroll.
Check out my blog - รับจ้างกดไลค์
Hi there, just became aware of your blog through Google, and found that it's really informative.
ReplyDeleteI'm gonna watch out for brussels. I will appreciate if you continue this in
future. Numerous people will be benefited from your writing.
Cheers!
Feel free to visit my blog post payday loans
Thank you very much... Its really helpful :-)
ReplyDeletewhere do I add this code to? thanks!
ReplyDelete