Enabling and Using site information in an ItemAdding event of a document library

4 minute read

In SharePoint 2007 you are able to write custom event handlers for a document library. Some of the different events you can rewrite are:
  • ItemAdding: Before an item is added to the list.
  • ItemAdded: When an item is added to the list.

Besides these item events there are also field events. When you would like to use your own custom events you have to populate the document library with your custom event handler. But first we will try to write an itemAdding event with information from the session and put that information in a Lookup field. In visual studio you have to create a class file and name it however you like. In this example I have called it ItemEventReceiver. The class has to inherit from SPItemEventReceiver.

 public class ItemEventReceiver : SPItemEventReceiver
 {
    private HttpContext hContext = null;

    public ItemEventReceiver()
         : base()
    {
       hContext = HttpContext.Current;
    }
 }

 

In the base class you have the hContext to retrieve the current HttpContext. This I have done so that you can retrieve information from the session. The next step is to create the event for the ItemAdding event like below.

 //ItemAdding event
 public override void ItemAdding(SPItemEventProperties properties)
 {
    try
    {
       //Retrieve the value from the Session and Convert it to an integer
       int ID = Convert.ToInt32(hContext.Session["SessionValue"]);

       //If the Value is 0 make the value 1
       if (ID == 0)
       {
          ID = 1;
       }

       string InternalName = ““;
       using (SPWeb web = properties.OpenWeb())
       {
          //Retrieve the internalname of you field were you would like to put the information in.
          InternalName = web.Lists[properties.ListId].Fields["InternalFieldName"].InternalName;
       }

       //Create a new SPFieldLookupValue
       SPFieldLookupValue Value = new SPFieldLookupValue(ID, ID.ToString());

       //Save the SPFieldLookupValue.ToString() in the afterproperties
       properties.AfterProperties[InternalName] = Value.ToString();
       base.ItemAdding(properties);
    }
    catch(Exception x)
    {
        //Write to the EventLog with my own logging class
        Logging.logmessage(x.Message, System.Diagnostics.EventLogEntryType.Error, “Sharepoint“);
    }
 }

Place you code in a try catch block like the code above. When something goes wrong the error is written to the EventLog. The integer ID is used to save the ID that is saved in the session under the name ‘SessionValue’. The value of ID is checked if it is not 0 if it is 0 the value is changed to 1. After that the field where you want to input an item is retrieved from the SPWeb. The next step is to make a new SPFieldLookupValue with the ID of the item you want to use and the String Value of that item. The field values for the list item are saved in the SPItemEventProperties.afterproperties.

They are saved by the internal name of the columns. This means you have to use the Internal name of the field item were in you want to fill in the information and use the SPFieldLookupValue you made to fill this item. You only have to convert it to a string. After that you perform the base actions of the event.

Now we have created the event you have to add the event to the document library. You can do this trough code and make it into a feature. To do this as a feature you will have to create a SPFeatureReceiver and override the FeatureActivated and FeatureDeactivating. Besides that you also have to implement the FeatureInstalled and FeatureDeinstalled and leave them empty.

 //Feature activating and feature deactivating
 public override void FeatureActivated(SPFeatureReceiverProperties properties)
 {
    SPSite siteCollection = SPContext.Current.Site;
    SPWeb web = (SPWeb)properties.Feature.Parent;
    SPList list = web.Lists["DocumentLibrary"];

    //AssemblyName
    string ReceiverAssembly = “AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e31c161dfa4a406af“;

    //ClassName
    string ReceiverClass = “ClassName“;

    //Add the EventReceiver to the list
    list.EventReceivers.Add(SPEventReceiverType.ItemAdding, ReceiverAssembly, ReceiverClass);

    //Update the list
    list.Update();
 }

 public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
 {
    SPSite siteCollection = SPContext.Current.Site;
    SPWeb web = (SPWeb)properties.Feature.Parent;
    SPList list = web.Lists["DocumentList"];

    //AssemblyName
    string ReceiverAssembly =”AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e31c161dfa4a406af“;

    //Loop trough the EventReceivers