Sunday, June 1, 2014

Event Reciever Example in sharepoint 2013

Feature Receiver Example

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace FeatureExample1.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("7d0f6067-7e35-4160-b741-fb2470190e7c")]
    public class Feature1EventReceiver :
        SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated
            (SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;
            web.Lists.Add("Students", "", SPListTemplateType.Contacts);
            SPList list = web.Lists["Students"];
            list.OnQuickLaunch = true;
            list.Update();
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating
            (SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;
            web.Lists["Students"].Delete();
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}

Web Event Receiver Example:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace WebEventReceiverExample1.PreventWebDeletion
{
    /// <summary>
    /// Web Events
    /// </summary>
    public class PreventWebDeletion :
        SPWebEventReceiver
    {
       /// <summary>
       /// A site is being deleted.
       /// </summary>
       public override void WebDeleting
           (SPWebEventProperties properties)
       {
properties.Status =
                SPEventReceiverStatus.CancelWithError;
          properties.ErrorMessage = "Dear " +
               properties.Web.CurrentUser.Name +
               " Can't delete the site " +
               properties.Web.Title;
          }


    }
}


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers >
      <Receiver>
        <Name>PreventWebDeletionWebDeleting</Name>
        <Type>WebDeleting</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>WebEventReceiverExample1.PreventWebDeletion.PreventWebDeletion</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
  </Receivers>
</Elements>

List Event Receiver Example:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace SharePointProject1.EnableVersioningAndFolderCreation
{
    /// <summary>
    /// List Events
    /// </summary>
    public class EnableVersioningAndFolderCreation :
        SPListEventReceiver
    {
       /// <summary>
       /// A list was added.
       /// </summary>
       public override void ListAdded
           (SPListEventProperties properties)
       {
            SPList list = properties.List;
            if (list.BaseTemplate ==
                SPListTemplateType.GenericList)
            {
                list.EnableAttachments = false;
                list.EnableFolderCreation = true;
                list.EnableVersioning = true;
                list.Update();
            }

       }


    }
}

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers >
      <Receiver>
        <Name>EnableVersioningAndFolderCreationListAdded</Name>
        <Type>ListAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>SharePointProject1.EnableVersioningAndFolderCreation.EnableVersioningAndFolderCreation</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>

  </Receivers>
</Elements>

Item Event Receiver Example :

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EREXample4.CustomerEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class CustomerEventReceiver :
        SPItemEventReceiver
    {
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding
            (SPItemEventProperties properties)
        {
            string phone;
            phone = properties.AfterProperties["Phone"].ToString();
            if (phone.Length < 10)
            {
                properties.Status = SPEventReceiverStatus.CancelWithError;
                properties.ErrorMessage = "Phone NO must contain 10 digits";
            }
        }

        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdating
            (SPItemEventProperties properties)
        {
            string phone;
            phone = properties.AfterProperties["Phone"].ToString();
            if (phone.Length < 10)
            {
                properties.Status = SPEventReceiverStatus.CancelWithError;
                properties.ErrorMessage = "Phone NO must contain 10 digits";
            }
        }

        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded
            (SPItemEventProperties properties)
        {
            EventFiringEnabled = false;
            string title;
            title = properties.ListItem["Title"].ToString();
            properties.ListItem["Title"] =
                title.ToUpper();
            properties.ListItem.Update();
            EventFiringEnabled = true;
        }

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated
            (SPItemEventProperties properties)
        {
            EventFiringEnabled = false;
            string title;
            title = properties.ListItem["Title"].ToString();
            properties.ListItem["Title"] =
                title.ToUpper();
            properties.ListItem.Update();
            EventFiringEnabled = true;
        }


    }
}
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers  ListUrl="Lists/Customers">
      <Receiver>
        <Name>CustomerEventReceiverItemAdding</Name>
        <Type>ItemAdding</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>EREXample4.CustomerEventReceiver.CustomerEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>CustomerEventReceiverItemUpdating</Name>
        <Type>ItemUpdating</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>EREXample4.CustomerEventReceiver.CustomerEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>CustomerEventReceiverItemAdded</Name>
        <Type>ItemAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>EREXample4.CustomerEventReceiver.CustomerEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
<Synchronization>Synchronous</Synchronization>
      </Receiver>
      <Receiver>
        <Name>CustomerEventReceiverItemUpdated</Name>
        <Type>ItemUpdated</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>EREXample4.CustomerEventReceiver.CustomerEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
<Synchronization>Synchronous</Synchronization>
      </Receiver>
  </Receivers>
</Elements>

No comments:

Post a Comment