I have created event receiver with C# coding in SharePoint.when item should match is already have. it showing massage like this is my code..
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
SPList oList = null;
string strCarName = string.Empty;
using (SPWeb web = properties.Web)
{
oList = web.Lists["Cars"];
string dropdwnvalue = web.Lists[properties.ListId].Fields["CarName"].InternalName;
String lookupFieldStringValue = Convert.ToString(properties.AfterProperties[dropdwnvalue]);
SPListItem item = oList.Items[Convert.ToInt32(lookupFieldStringValue) - 1];
strCarName = Convert.ToString(item["LinkTitle"]);
}
string strStartdate = Convert.ToString(properties.AfterProperties["EventDate"]);
SPQuery existingItemsQuery = new SPQuery();
existingItemsQuery.Query = "<Where><And><Eq><FieldRef Name='EventDate'/><Value Type='DateTime'>" + strStartdate + "</Value></Eq><Eq><FieldRef Name='Car_x0020_Name'/><Value Type='Lookup'>" + strCarName + "</Value></Eq></And></Where>";
SPListItemCollection existingItems = properties.List.GetItems(existingItemsQuery);
if (existingItems.Count >= 1)
{
properties.Cancel = true;
properties.ErrorMessage = "Item is already exists";
properties.Status = SPEventReceiverStatus.CancelWithError;
// ClientScript.RegisterStartupScript(typeof(Page), "test", "<script>alert('Hello');return false;</script>");
}
output:-
item is already exists.
that item adding only one time in a day.again i cant add another item. But i want add another item in a day change time
any one can help me out.
Best Answer
As I can see, you have problem with the query string, you should use this:
and you should have strStartdate with format like this '1971-01-01T00:00:00Z'
you can use this method:
Hope this helps!