Thursday, March 27, 2014

Event Receiver


SharePoint Item Event Receiver
public class EventReceiver1 : SPItemEventReceiver

    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);
           string workphone = properties.AfterProperties["WorkPhone"].ToString();
           string email = properties.AfterProperties["Email"].ToString();
           if (!string.IsNullOrEmpty(workphone))
           {
               if (!System.Text.RegularExpressions.Regex.IsMatch(workphone , @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$"))
               {
                   string sErrMsg = "Business Phone is not in correct format";
                   properties.ErrorMessage = sErrMsg;
                   //properties.Status = SPEventReceiverStatus.CancelWithError;
                   properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                   properties.RedirectUrl = string.Format("/_layouts/ListItemEventReceive/EventReceiverErrorPage.aspx?ErrMsg={0}",sErrMsg);
               }              
           }
           if (!string.IsNullOrEmpty(email))
           {
               if (!System.Text.RegularExpressions.Regex.IsMatch(email, @"^(?("")("".+?""@)|(([0-9a-zAZ]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zAZ]{2,6}))$"))
               {
                   string sErrMsg = "Email is not in correct format";
                   properties.ErrorMessage = sErrMsg;
                   //properties.Status = SPEventReceiverStatus.CancelWithError;
                   properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                   properties.RedirectUrl = string.Format("/_layouts/ListItemEventReceive/EventReceiverErrorPage.aspx?ErrMsg={0}", sErrMsg);
               }
           }
       }
    }

Application Page:

EventReceiverErrorPage.aspx

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EventReceiverErrorPage.aspx.cs" Inherits="ListItemEventReceive.Layouts.ListItemEventReceive.EventReceiverErrorPage" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:Label ID="lblErrMsg" runat="server" Text=""></asp:Label>
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Event Receiver Error
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
Event Receiver Error
</asp:Content>


EventReceiverErrorPage.aspx.cs

    public partial class EventReceiverErrorPage : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sErrMsg = Request.Params["ErrMsg"];
            lblErrMsg.Text = sErrMsg;
        }
    }