i am very pleased to share one of the best solution ever i have in my mind which we have implemented in every SharePoint project, that is often we need to do some work around when we add/update/delete any item from SharePoint list. These are known as List Event Receiver in SharePoint, i use this to quickly remember it is something like writing Triggers as we write in SQL Server on a Table.
Work around
Need to create 2 Classes one for ListFeatureReceiver.cs (to Assosiating EventRecievers With List(s)) this would help to add events like itemadding, itemAdded and item Updated etc on activation of specific feature, and second one ListEventReceiver.cs (containts events along with their action items)
at the end i’ll send email alerts on ItemAdded
Note: To create any library/List from code write that code in ListFeatureReceiver.cs
ListFeatureReceiver.cs Code
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class ListsFeatureReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
base.FeatureActivated(properties);
SPWeb web;
SPList aList;
if (properties.Feature.Parent is SPWeb)
{
web = (SPWeb)properties.Feature.Parent;
}
else
{
web = ((SPSite)properties.Feature.Parent).RootWeb;
}
if (web != null)
{
aList = web.Lists.TryGetList("JobApplication");
if (aList != null)
{
AssosiatingEventRecieversWithList(aList,SPEventReceiverType.ItemAdded,
Assembly.GetExecutingAssembly().FullName,
"Qatalum.Inet.Sharepoint.ListEventReceiver");
}
}
}
private void AssosiatingEventRecieversWithList(SPList _list, SPEventReceiverType _type, string _assembly, string _class)
{
_list.ParentWeb.AllowUnsafeUpdates = true;
_list.EventReceivers.Add(_type, _assembly, _class);
_list.Update();
_list.ParentWeb.AllowUnsafeUpdates = false;
}
}
To Attach ListFeatureReceiver.cs to feature
add a new feature and click on it, go to its properties write Receiver Assembly and Receiver Class (complete path of ListEventReceiver.cs class with namespace)
ListEventReceiver.cs Code
public class ListEventRevievers : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
if (properties.ListItem.ParentList.Title == "JobApplication")
{
SendJobApplicationStatusAlerts(properties);
}
});
}
private void SendJobApplicationStatusAlerts(SPItemEventProperties properties)
{
try
{
string ToEmail = string.Empty;
SPWeb web;
web = (SPWeb)properties.Web;
string Subject = properties.ListItem["FieldName"].ToString();
string body = properties.ListItem["FieldName"].ToString();
string FromEmail = sproperties.ListItem["FieldName"].ToString();
string ToEmail = properties.ListItem["FieldName"].ToString();
string SMTP = "YourSMTP address";
bool isSent = SendEmail(SMTP, FromEmail, ToEmail, Subject, body);
}
catch (Exception ex)
{
throw ex;
}
}
private bool SendEmail(string smtpServer, string _sFrom, string _sTo, string _sSubject, string _sBody)
{
bool _bResult = false;
try
{
string smtpFrom = string.Empty;
if (string.IsNullOrEmpty(_sFrom))
{
smtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;
}
else
{
smtpFrom = _sFrom;
}
//Create the mail message and supply it with from and to info
MailMessage mailMessage = new MailMessage(smtpFrom, _sTo);
//Set the subject and body of the message
mailMessage.Subject = _sSubject;
mailMessage.Body = _sBody;
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient(smtpServer);
smtpClient.Send(mailMessage);
_bResult = true;
}
catch (Exception ex)
{ ex.ToString());
_bResult = false;
throw ex;
}
return _bResult;
}
}