none
How to get recurring items with SPQuery by a specific ID

    Question

  • Hi folks,

    I need to get all recurring items of a particular event. Lets say I create a event 'SPFestival' with recurring properties (every day with 2 occurences). So I have a 1 item with recurring data. I get these SPListItem (2) items with SPQuery sentence (with month attribute) if they are within this month (sharepoint month :) ):

    <Where><DateRangesOverlap><FieldRef Name=\"EventDate\" /><FieldRef Name=\"EndDate\" /><FieldRef Name=\"RecurrenceID\" /><Value Type=\"DateTime\"><Month /></Value></DateRangesOverlap></Where>

    Okay thats fine. But how I can get these two items with ID. I need to know when deleting these items that there is no more any non-deleted events active?

    I tried this with spquery (i equals for the correct ID):

    query.ExpandRecurrence = true;
    query.ItemIdQuery = true; //With and without this
    query.Query = "<Where><Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">" + i.ToString() + "</Value></Eq></Where>";

    This only returns the MAIN item, not the recurring items. I need to do something neat when all the recurring items of a specific repeating event have been deleted. But I havent found the way this, the ItemDeleting/ItemDeleted never occurs on this recurring situation.

    Wednesday, May 05, 2010 10:29 AM

Answers

  • Hi Pauli,

    I think I understood what you meant. I saw that when you delete occurrences of a recurrent event with the UI indeed new (really separate) list items are being created.

    And since you have new items created it is the ItemAdded event that is really being invoked - so you can recognize these by either checking the "Deleted" prefix in the title or to check the EventType system field whose value for such "deleted" items is 3 (it's an integer)

    The CAML query to get these events would be:

    <Where><Eq><FieldRef Name="MasterSeriesItemID" /><Value Type="Integer"> + yourMasterID + </Value></Eq></Where>

    But even if you have all these "deleted" items the thing is how you're going to use them

    If you goal is to detect the moment when the last occurrence of a recurrent event is being deleted then it's better to check it this way:

    <Where><And><DateRangesOverlap><FieldRef Name="EventDate" /><FieldRef Name="EndDate" /><FieldRef Name="RecurrenceID" /><Value Type="DateTime"><Year /></Value></DateRangesOverlap><Eq><FieldRef Name="ID"/><Value Type="Integer"> + yourmasterid + </Value></Eq></And></Where>

    when this query returns no items this will mean that there are no left occurrences of the recurrent event for the specified year (SPQuery.CalendarDate) and basically if you check the years contained in the EventDate-EndDate range of the MAIN item you will know for sure whether all occurrences have been deleted.

    this check I think you should make in both the ItemAdded event for items with EventType=3 and in the standard ItemDeleted (in the ItenDeleted event you should use the ID of the event item, not the MasterSeriesItemID - you will expect the MAIN item being deleted in this case)

    • Marked as answer by Pauli J Wednesday, May 12, 2010 10:01 AM
    Thursday, May 06, 2010 3:36 PM

All replies

  • internally there is just one item (the MAIN item) - you can see that if you check the ID-s of the items (the plural here should stand for something like a multiplication of the MAIN item) returned by the DateRangesOverlap query. so if you delete the MAIN item this will result in the deletion of the whole recurring sequence.
    Wednesday, May 05, 2010 6:14 PM
  • No no, I dont want to delete the MAIN item that way; if sharepoint user deletes an event of the calendar that is repeating event, (that repeats twice in this case). So there is only 1 item left and he/she decides to delete that one too (from the calendar at the sharepoint site), all the recurring items have now been deleted by user and I need to do one thing, I have a sharepoint list that is created for each event (only for main item) when event is created.

    When normal (non-repeating) event is deleted by user, this 'sub-list' is deleted (from itemDeleted receiver).But when there is repeating events in the particular MAIN item, and these repeating events are deleted, the itemDeleted doesnt launch from Receivers; only ItemAdded that created new list item with title "Deleted: Title"; I made an IF sentence that checks (to itemadded receiver) if the title contains "Deleted" try to do next: Get all recurring items of the main item (MasterSeriesID or something like that contains the main item ID), but this is the dilemma, how can I get all Recurring items of a specific main ITEM (like you get recurring items  with spquery /w daterangesoverlap).

    I cannot do this with SPQuery /w DateRangesOverlap, because I cannot limit the search with time, I need all possible sub-items. So I need to get these recurring items something that looks like this (/w ExpandRecurrence = true):

    <Where><Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">" + masterSeriesID + "</Value></Eq></Where>";

    But this doesnt work the way it should :) It only returns the main item.

    Does this make any sense at all? :) Thx 4 advance

    Thursday, May 06, 2010 5:49 AM
  • Hi Pauli,

    I think I understood what you meant. I saw that when you delete occurrences of a recurrent event with the UI indeed new (really separate) list items are being created.

    And since you have new items created it is the ItemAdded event that is really being invoked - so you can recognize these by either checking the "Deleted" prefix in the title or to check the EventType system field whose value for such "deleted" items is 3 (it's an integer)

    The CAML query to get these events would be:

    <Where><Eq><FieldRef Name="MasterSeriesItemID" /><Value Type="Integer"> + yourMasterID + </Value></Eq></Where>

    But even if you have all these "deleted" items the thing is how you're going to use them

    If you goal is to detect the moment when the last occurrence of a recurrent event is being deleted then it's better to check it this way:

    <Where><And><DateRangesOverlap><FieldRef Name="EventDate" /><FieldRef Name="EndDate" /><FieldRef Name="RecurrenceID" /><Value Type="DateTime"><Year /></Value></DateRangesOverlap><Eq><FieldRef Name="ID"/><Value Type="Integer"> + yourmasterid + </Value></Eq></And></Where>

    when this query returns no items this will mean that there are no left occurrences of the recurrent event for the specified year (SPQuery.CalendarDate) and basically if you check the years contained in the EventDate-EndDate range of the MAIN item you will know for sure whether all occurrences have been deleted.

    this check I think you should make in both the ItemAdded event for items with EventType=3 and in the standard ItemDeleted (in the ItenDeleted event you should use the ID of the event item, not the MasterSeriesItemID - you will expect the MAIN item being deleted in this case)

    • Marked as answer by Pauli J Wednesday, May 12, 2010 10:01 AM
    Thursday, May 06, 2010 3:36 PM
  • The first one didnt work, it only returned Deleted items. The second one worked. Thx :)
    Wednesday, May 12, 2010 10:01 AM