2

I'm an absolute beginner of SharePoint development. I would like to create my first Event Receiver and I'm doing with VS2010 (because I'm on SP 2010).

I would like to check if adding a new event in calendar (ItemAdding) there is already another event by checking EndDate and EventDate, and in case display an error message.

Which is the best way? Could someone provide some examples?

Thanks so much!

2

There are a couple of ways you can do this from the event receiver. The nice thing about an an event receiver is that you can get the instance of the list that the receiver is working on and from there easily access the Items Collection. From there you can iterate through the list and perform a check by item. There are plenty of tutorials to show you how to do this but the following link will explain why some ways are better than others! In the example though, you can skip the line where he get the list using SPContext object--you can get a reference to the list by calling something like:

SPList list = properties.List;
SPListItemCollection coll = list.Items;
foreach (SPListItem itm in coll){
if((DateTime)itm[*fieldname*] == etc...cast to DateTime since you are dealing with calendar event

Iterating through the list is the brute force way of doing it and depending on how many items you have, it could be slow. Another more elegant way is to build an SPQuery object against the list. The following MSDN example will show you how. You can skip the SPWeb/SPSite section since you already have a reference to your list from the event receiver. The tricky part will be building the caml query and there are a few tutorials out there, but this one may be all you need and this one discusses working with dates in CAML. Another thing to keep in mind with this approach is that when writing an SPQuery object, you can omit the Query tag that wraps the Where clause. The MSDN article shows you how to do it correctly, but some blogs out there may not!

If you have follow up questions, just let us know!

  • First of all, thank you! The method I prefer is using a CAML query because I've already tested a Query with CAML Query Helper and it works. My problem, at the moment, is to retrieve two data from "new event" form and insert that data into caml query. Later I will count returned elements and I will use a "if" statement to display an error message. Do you think it is possible? – Pepozzo Dec 17 '13 at 15:31
  • Sounds pretty reasonable. You can access the "new event" information by using BeforeProperties and AfterProperties, but it can be tricky depending on which event receiver you are calling! Look at this blog for more info: (howididit-sharepoint.blogspot.com/2012/09/…) Since you are using the Adding event receiver on a list, you need to use the AfterProperties. As for the "if" statement count, I can't tell exactly what you are doing so I can be sure if whatever it is you are trying to do is possible. – Mario Pereira Dec 17 '13 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.