Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to check If deleted item is the Last Item deleted in the List in following method,

public override void ItemDeleted(SPItemEventProperties properties)
{ 

}

I know I can get web url using properties.WebUrl and then find list and then items count in it, but is there any better way of doing it ?

If item wasn't the last one then I can go through all List Items to find the List Item that was added nearest to DateTime.Now, but then I Can go through each list item and compare there dates, Does anyone knows any efficient way ?

EDIT

I find LastItemModifiedDate property to get last item, so don't need to go through each list item to find the latest added to the list

EDIT 2

I said I can get Last Item, but in real I am able to get last Item modified date, so only thing I know is to go through all items to find one I want :S, I bet there will be a better way

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

To find if it's the last item I would use:

properties.List.ItemCount < 1

But to get the last modified item I would consider using a query:

SPQuery q = new SPQuery();
q.query="<OrderBy><FieldRef Name='Modified' Ascending='False'></FieldRef></OrderBy>";
q.RowLimit=1;
SPListItemCollection results = properties.List.GetItems(q);
//Last item should be the only entry in results. If no entries, then no items in list.

Note: I'm not sure about the sort order on the OrderBy - but I think descending will get the latest modified item.

share|improve this answer
add comment

There is a property called ID in SPListItem, that property contains the item's 1-based integer ID, which is one greater than the ID of the item that was previously added. If the item is deleted, its ID is not reuse. check this link in MSDN for further explanation.

Technically to get last item added is to get biggest ID in SPListItem.

share|improve this answer
    
The highest ID is the most recently created item, not necessarily the most recently modified. –  Andy Burns Jul 26 '12 at 10:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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