Tuesday, August 12, 2008

Alert count registered for each list item

Scenario:
You need to have a count for Alerts subscribed for each list item within a particular list.

Solution:
Create a column name "Counter" within the list

Code:

SPWeb oWebsite = SPContext.Current.Web;

SPAlertCollection collAlerts = oWebsite.Alerts;

SPList oList = oWebsite.Lists["List_Name"];
SPListItemCollection collListItems = oList.Items;


foreach (SPAlert oAlert in collAlerts)
{

if (oAlert.ListID == oList.ID)
{

foreach (SPListItem listItem in collListItems)
{
if (listItem.ID == oAlert.ItemID)
{

listItem["Counter"] = Convert.ToInt32(listItem["Counter"]) + 1;
listItem.Update();

}

}
}
}

Possible Use:
Administrative Console Utility , Webpart

Articles:

0 comments: