I'm working on a SharePoint 2019 event receiver and I need to count the number of items in a list that belong to a specific content type. I've tried using the SPList.ItemCount property, but it gives the total item count, not the count for a specific content type.
Here's what I've tried so far:
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPItem item = properties.ListItem;
SPList list = item.ParentList;
SPContentType itemContentType = list.ContentTypes.GetById(item.ContentTypeId);
// How to count items of the same content type as the current item?
}
I'm looking for a way to efficiently count the items of the same content type as the newly added item in SharePoint event Receiver.
Any suggestions or code examples would be greatly appreciated.