Steps involved in creating a webcontrol:
1) Create a button control,intialize it on the Page load, Assign the click event to that
And attach code for that event handler.
Code goes like this.
namespace AlertMeWC
{
[ToolboxData("<{0}:ManageAlerts runat=server>")]
public class ManageAlerts : WebControl
{
private System.String btnCreateAlert;
private LinkButton btnCreate;
public System.String btnCreateAlertID
{
get
{
return btnCreateAlert;
}
set
{
btnCreateAlert = value;
}
}
private void CreateAlert(object sender, EventArgs e)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate(){
SPSite _site = SPContext.Current.Site;
SPWeb _web = _site.OpenWeb();
SPAlert newalert = _web.Alerts.Add();
newalert.AlertType = SPAlertType.Item;
newalert.AlertFrequency = SPAlertFrequency.Immediate;
newalert.DynamicRecipient = Get the Logged in user details.
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("/pages"))
{
newalert.Item = _web.GetListItem(HttpContext.Current.Request.Url.ToString()); newalert.Title = "Pages: FirstPage.aspx";
newalert.Update();
_web.AllowUnsafeUpdates = true;
_web.Update();
_web.AllowUnsafeUpdates = false;
}
});
}
catch (Exception ex)
{
Page.Response.Write("Error in ManageAlerts " + ex.Message.ToString());
}
finally
{
//Dispose web
}
}
private void InitializeControls()
{
btnCreate = InitializeControls
}
private T InitializeControls
{
object _BaseControl = null;
T _Return = default(T);
if (String.IsNullOrEmpty(strControlID))
{
throw new ApplicationException("ManageAlerts Initialization failed: The " + strPropertyName + " attribute must specify a valid " + typeof(T).ToString() + ". The attribute was not specified.");
}
_BaseControl = this.NamingContainer.FindControl(strControlID);
if (_BaseControl == null)
{
throw new ApplicationException("ManageAlerts Initialization failed: The " + strPropertyName + " attribute must specify a valid " + typeof(T).ToString() + ". The control " + strControlID + " cannot be found.");
}
if (!(_BaseControl is T))
{
throw new ApplicationException("ManageAlerts Initialization failed: The " + strPropertyName + " attribute must specify a valid " + typeof(T).ToString() + ". The control " + strControlID + " is a " + _BaseControl.GetType().ToString() + ", and is not supported.");
}
_Return =(T)_BaseControl;
Return _Return;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
try
{
InitializeControls();
btnCreate.Click += new EventHandler(this.CreateAlert);
}
catch (Exception ex)
{
Page.Response.Write("Error: " + ex.Message.ToString());
}
finally
{
}
}
}
}
2)Assign the strong name, GAC and grab public key token.
3)Navigate to your site where we would like to stick this webcontrol on.
4)Create a new page and edit that page in Sharepoint designer.
Add the following lines to that Page in SPD.
<@ Register Tagprefix="muki" Namespace="AlertMeWC" Assembly="AlertmeWC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=#$@$%@$%@#%@5" %>
Add this in body tag.
<muki:ManageAlerts ID="ManageAlerts" runat="server" btnCreateReportID ="btnCreate"/>
When ever the user clicks on this link, then automatically alerts are created
Enjoy SPD'ing!!