SharePoint events: modify SPListItem fields with vars from the Session in the itemAdding or itemAdded events

Some time ago I tried to alter a field of a SPListItem right after the moment that the item has been created. A var was stored in the Session so it could be used on the newly created SPListItem (itemAdded).

NOT.

I figured out that the HttpContext isn't available when during the ItemAdded event. When using the itemAdding event, the HttpContext is available, but the item that will be created, isnt. The SPFeatureReceiverProperty.SPListItem is null, because the item is not yet created.  After reading the MSDN documentation I figured out that the SPFeatureReceiverProperties Afterproperties can be used to store a value, which will be applied right after the item is created.

below is an example:

 

    1 public class ItemEventReceiver : SPItemEventReceiver

    2 {   

    3     public ItemEventReceiver()

    4     {

    5         hContext = HttpContext.Current;

    6     }

    7 

    8     /// <summary>

    9     /// overrides default ItemAdded

   10     /// </summary>

   11     /// <param name="properties"></param>

   12     public override void ItemAdded(SPItemEventProperties properties)

   13     {

   14         Debug.WriteLine("ItemAdded");

   15         CheckHContextAndSPListItem(properties.ListItem);

   16         base.ItemAdded(properties);

   17     }

   18 

   19     /// <summary> 

   20     /// overrides the standard ItemAdding

   21     /// </summary> 

   22     /// <param name="properties"></param> 

   23     public override void ItemAdding(SPItemEventProperties properties)

   24     {

   25         string internalFieldName = null;

   26         string FIELD_NAME = "Foo";

   27         SPFieldLookupValue lookupField = null;

   28 

   29         Debug.WriteLine("ItemAdding");       

   30         CheckHContextAndSPListItem(properties.ListItem);

   31 

   32         using (SPWeb web = properties.OpenWeb())

   33         {

   34             internalFieldName = web.Lists[properties.ListId].Fields[FIELD_NAME].InternalName;

   35         }

   36 

   37         // example for adding a value to a SPListItem property

   38         string tempValue = "foo";

   39         properties.AfterProperties[internalFieldName] = tempValue;

   40     }

   41 

   42     /// <summary> 

   43     /// checks the values of hContext and SPListItem 

   44     /// </summary> 

   45     /// <param name="SPListItem">SPListItem out of the SPItemEventProperties</param> 

   46     private static void CheckHContextAndSPListItem(SPListItem listItem)

   47     {

   48         if (HttpContext.Current == null)       

   49             Debug.WriteLine("hContext is null");       

   50         else       

   51             Debug.WriteLine("hContext is available");       

   52 

   53         if (listItem == null)       

   54             Debug.WriteLine("listItem is null");       

   55         else       

   56             Debug.WriteLine("listItem is available");       

   57     }

   58 }    

Published Fri, Jan 30 2009 9:59 AM by Bas

Comments

# re: SharePoint events: modify SPListItem fields with vars from the Session in the itemAdding or itemAdded events

Sunday, December 27, 2009 11:03 AM by Mohammed Barakat

I faced the same issue when I was tring to update some custom fields of my document library when uploading new documents, the field was ( ProjectID ) which I put it inside a session in my webpart ( the step before uploading the document).

What I did is : I put the projectID into the cache ( per user ) inside the custom webpart which acts as a session as follows :

if (Request.QueryString["ProjectID"] != null)

{

HttpRuntime.Cache.Remove(SPContext.Current.Web.CurrentUser.LoginName);

               HttpRuntime.Cache.Add(SPContext.Current.Web.CurrentUser.LoginName, ProjectID, null, DateTime.UtcNow.AddMinutes(60), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);

}

then I implemented the ItemAdded event and I get the value of the cached projectId through :

public override void ItemAdded(SPItemEventProperties properties)

       {

           try

           {

               string ProjID = "";

               string CreatedBy = null;

               if (properties.ListItem["Created By"] != null)

                   CreatedBy = properties.ListItem["Created By"].ToString().Split(';')[1].Replace("#","");

               if (HttpRuntime.Cache[CreatedBy] != null)

               {  

                   //SPContext.Current.Web.CurrentUser.LoginName;

                   ProjID = HttpRuntime.Cache[CreatedBy].ToString();

                   if (properties.ListItem["Project"] == null)

                   {

                       properties.ListItem["Project"] = new SPFieldLookupValue(ProjID);

                       properties.ListItem.SystemUpdate();

                   }

                   base.ItemAdded(properties);

               }

           }

           catch (Exception ex)

           { }

       }

# re: SharePoint events: modify SPListItem fields with vars from the Session in the itemAdding or itemAdded events

Sunday, December 27, 2009 11:04 AM by Mohammed Barakat

I faced the same issue when I was tring to update some custom fields of my document library when uploading new documents, the field was ( ProjectID ) which I put it inside a session in my webpart ( the step before uploading the document).

What I did is : I put the projectID into the cache ( per user ) inside the custom webpart which acts as a session as follows :

if (Request.QueryString["ProjectID"] != null)

{

HttpRuntime.Cache.Remove(SPContext.Current.Web.CurrentUser.LoginName);

               HttpRuntime.Cache.Add(SPContext.Current.Web.CurrentUser.LoginName, ProjectID, null, DateTime.UtcNow.AddMinutes(60), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);

}

then I implemented the ItemAdded event and I get the value of the cached projectId through :

public override void ItemAdded(SPItemEventProperties properties)

       {

           try

           {

               string ProjID = "";

               string CreatedBy = null;

               if (properties.ListItem["Created By"] != null)

                   CreatedBy = properties.ListItem["Created By"].ToString().Split(';')[1].Replace("#","");

               if (HttpRuntime.Cache[CreatedBy] != null)

               {  

                   //SPContext.Current.Web.CurrentUser.LoginName;

                   ProjID = HttpRuntime.Cache[CreatedBy].ToString();

                   if (properties.ListItem["Project"] == null)

                   {

                       properties.ListItem["Project"] = new SPFieldLookupValue(ProjID);

                       properties.ListItem.SystemUpdate();

                   }

                   base.ItemAdded(properties);

               }

           }

           catch (Exception ex)

           { }

       }

# re: SharePoint events: modify SPListItem fields with vars from the Session in the itemAdding or itemAdded events

Wednesday, August 25, 2010 4:03 PM by nn

nn

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 6 and 4 and type the answer here: