I figure this was a cool one, it took me a few minutes to do this morning but it's worth wild to see.
Below is the code for an Alert rollup webpart:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
namespace AlertingRollup
{
[Guid("9500c07f-f93e-4b2a-9099-b0ff7a87beed")]
public class AlertingRollup : System.Web.UI.WebControls.WebParts.WebPart
{
Label message;
string alerts;
public AlertingRollup()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
message = new Label();
alerts = getAlerts();
}
public string getAlerts()
{
using (SPWeb web = SPContext.Current.Web)
{
SPUser user = web.CurrentUser;
SPAlertCollection altCol;
if (user.IsSiteAdmin)
{
altCol = web.Alerts;
return alertTable(altCol);
}
else
{
altCol = user.Alerts;
return alertTable(altCol);
}
}
}
private string alertTable(SPAlertCollection altCol)
{
StringBuilder builder = new StringBuilder();
try
{
builder.Append("< table cellpadding='5px' cellspacing='0' style='border:solid 0px #fff; width:100%;' >");
builder.Append("< tr style='background-color:#dcdcdc;' >");
builder.Append("< td style='white-space:nowrap;' >");
builder.Append("Title");
builder.Append("< /td >");
builder.Append("< td style='white-space:nowrap;' >");
builder.Append("Template Name");
builder.Append("< /td >");
builder.Append("< td style='white-space:nowrap;' >");
builder.Append("Alert Type");
builder.Append("< /td >");
builder.Append("< td style='white-space:nowrap;' >");
builder.Append("Status");
builder.Append("< /td >");
builder.Append("< td style='white-space:nowrap;' >");
builder.Append("Event Type");
builder.Append("< /td >");
builder.Append("< td style='white-space:nowrap;' >");
builder.Append("Frequency");
builder.Append("< /td >");
builder.Append("< /tr >");
foreach (SPAlert alt in altCol)
{
builder.Append("< tr >");
builder.Append("< td >");
builder.Append(alt.Title);
builder.Append("< /td >");
builder.Append("< td style='white-space:normal;' >");
builder.Append(alt.AlertTemplateName);
builder.Append("< /td >");
builder.Append("< td >");
builder.Append(alt.AlertType.ToString());
builder.Append("< /td >");
builder.Append("< td >");
builder.Append(alt.Status.ToString());
builder.Append("< /td >");
builder.Append("< td >");
builder.Append(alt.EventType.ToString());
builder.Append("< /td >");
builder.Append("< td >");
builder.Append(alt.AlertFrequency.ToString());
builder.Append("< /td >");
builder.Append("< tr >");
}
builder.Append("< /table >");
}
catch (Exception ex)
{
message.Text = "The following error occurred while gathering alerts: " + ex.Message;
}
return builder.ToString();
}
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
writer.Write(alerts);
message.RenderControl(writer);
}
}
}
I'll go over the parts that are doing the "interesting" work. The main portion of gathering the user alerts happens in the "alertTable" method, below I ripped out all the tables to show you just the looping and info gathering:
private string alertTable(SPAlertCollection altCol)
{
StringBuilder builder = new StringBuilder();
try
{
foreach (SPAlert alt in altCol)
{
builder.Append(alt.Title);
builder.Append(alt.AlertTemplateName);
builder.Append(alt.AlertType.ToString());
builder.Append(alt.Status.ToString());
builder.Append(alt.EventType.ToString());
builder.Append(alt.AlertFrequency.ToString());
}
}
catch (Exception ex)
{
message.Text = "The following error occurred while gathering alerts: " + ex.Message;
}
return builder.ToString();
}
After retrieving the SPWeb object from the current context, I test if the current user has admin privelges. Based on that information, if the user is an admin I retrieve all alerts from the current site. If the user is not an admin, I simply retrieve the alerts from the Current User object. I then pass the SPAlertCollection to the alertTable method and loop through each alert in the collection. For each alert in the collection I retrieve information to add to the html table. This is pretty straight forward. Hopefully this gives you an idea of some of the simple parts you build to help in an Information workers day to day life.
~:)
1 comments:
This is a good example for understanding to us. Thanks Eric
Post a Comment