Tuesday, April 19, 2011

attach Event Receivers to content types in sharepoint

using System;
using System.Collections;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace SuryaFeature
/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.

/// The GUID attached to this class may be used during packaging and should not be modified.

public class FeatureEventReceiver : SPFeatureReceiver
private string sAssemblyFullName = string.Empty;
private string sClassName = string.Empty;
private string sOperationMode = string.Empty;
private SPEventReceiverDefinitionCollection oEventRecvDefColl = null;
private string[] sArrContentTypeNames = { "Leave Calendar", "Event Calendar", "Editorial Calendar" };

// Uncomment the method below to handle the event raised after a feature has been activated.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
sOperationMode = "Activate";

// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
sOperationMode = "Deactivate";

private void GetAssemblyParameters(SPFeaturePropertyCollection properties)
sClassName = properties["AssemblyQualifiedName"].Value;

sAssemblyFullName = properties["AssemblyFullName"].Value;

private void PerformOperation(SPFeatureReceiverProperties _oProperties)
using (SPWeb oWeb = _oProperties.Feature.Parent as SPWeb)
SPListCollection oIntranetListColl = oWeb.Lists;

for (int iCount = 0; iCount < oIntranetListColl.Count; iCount++) { if (CheckForContentType(oIntranetListColl[iCount])) { // call this procedure to delete any existing event handlers DeleteEventHandlerToContentType(oIntranetListColl[iCount]); if (sOperationMode == "Activate") { SPEventReceiverDefinition oEventRecvDef = null; foreach (string _sArrItem in sArrContentTypeNames) { AddEventHandlerToContentType(oIntranetListColl[iCount], oEventRecvDef, "ItemAdding" + _sArrItem.Replace(" ", ""), SPEventReceiverType.ItemAdding, 1000, SPEventReceiverSynchronization.Synchronous); AddEventHandlerToContentType(oIntranetListColl[iCount], oEventRecvDef, "ItemUpdating" + _sArrItem.Replace(" ", ""), SPEventReceiverType.ItemUpdating, 1001, SPEventReceiverSynchronization.Synchronous); AddEventHandlerToContentType(oIntranetListColl[iCount], oEventRecvDef, "ItemDeleting" + _sArrItem.Replace(" ", ""), SPEventReceiverType.ItemDeleting, 1002, SPEventReceiverSynchronization.Synchronous); } } } } } } catch (Exception eEX) { SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("TravelCalendarFeatureReceiver", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, eEX.Message, eEX.StackTrace); } } private bool CheckForContentType(SPList _oListToUpdate) { if (_oListToUpdate.ContentTypes.Count > 0)
foreach (SPContentType _oContentType in _oListToUpdate.ContentTypes)
if (sArrContentTypeNames.Contains(_oContentType.Name)) { return true; }
return false;

private void DeleteEventHandlerToContentType(SPList _oListToUpdate)
// get all event receviers as definition collection
oEventRecvDefColl = _oListToUpdate.EventReceivers;
SPEventReceiverDefinition oEventRecvToDelete;
// create an array to store event receivers chosen for deletion
ArrayList oArrEventRecvToDelete = new ArrayList();
for (int i = 0; i < oEventRecvDefColl.Count; i++) { oEventRecvToDelete = oEventRecvDefColl[i]; if (oEventRecvToDelete.Type == SPEventReceiverType.ItemAdding) { oEventRecvToDelete.Delete(); } if (oEventRecvToDelete.Type == SPEventReceiverType.ItemUpdating) { oEventRecvToDelete.Delete(); } if (oEventRecvToDelete.Type == SPEventReceiverType.ItemDeleting) { oEventRecvToDelete.Delete(); } } } private void AddEventHandlerToContentType(SPList _oListToUpdate, SPEventReceiverDefinition _oEventRecvDef, string _sEventRecvDefName, SPEventReceiverType _oEventRecvType, int _iEventRecvSeq, SPEventReceiverSynchronization _oEventRecvSyn) { _oEventRecvDef = _oListToUpdate.EventReceivers.Add(); _oEventRecvDef.Assembly = sAssemblyFullName; _oEventRecvDef.Class = sClassName.Substring(0, sClassName.IndexOf(',')); _oEventRecvDef.Name = _sEventRecvDefName; _oEventRecvDef.Type = _oEventRecvType; _oEventRecvDef.SequenceNumber = _iEventRecvSeq; _oEventRecvDef.Synchronization = _oEventRecvSyn; _oEventRecvDef.Update(); } // 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 parameters)


  1. I have a month column which is a choice column in my document library in the format specified below.. For eg. Jan 2013 I have the following requirements 1. The month dropdown should always have only those months which start from 6 months previous of the current month.For eg.If the current month is March 2013 then only the months starting from oct 2012 to march 2013 should be populated in the dropdown 2.There is another column in my document library called 'date' of date format.It is not to be shown in the default view.The date field should automatically be set as the 1st date of the month selected from the month column.For example. IF the month selected is March 2013 .The date Should b 1/3/2013. Please Help!!Its urgent..Can This be done with the help of Event Recievers?? If YEs...Can U Please Post A Step By Step explanation along with the code.. Any Help is welcome...Thanks in Advance..

    1. Nivee,

      This one you can do it different ways by using jquery or javascript or client object model.

      just add a content editor webpart to the document library page and place your javascript or jquery code over there.

      you can call javscript this way as well


      The date field should automatically be set as the 1st date of the month selected from the month column

      example: http://blogs.solidq.com/sharepoint/post.aspx?id=86

      google like this

      populate dropdown using javascript or jquery sharepoint 2010