Saturday, April 25, 2009

ItemAdding VS ItemAdded

ItemAdding - HttpContext

namespace MyEventHandler
{
  public class MyAction :SPItemEventReceiver
  {       
    HttpContext curr;     
      
    public MyAction()
    {           
      curr = HttpContext.Current;
    }
       
    public override void ItemAdding(SPItemEventProperties properties)
    {           
      base.ItemAdding(properties);
      if (properties.ListTitle.Equals("Name of the CustomList"))   
       {               
         if (curr != null)
         {
           ret = curr.User.Identity.Name.Trim();
         }
       }
     }
  }
}

ItemAdded - HttpContext

/?



ItemAdding - Update ListItem

namespace MyEventHandler
{
  public class MyActionSPItemEventReceiver
  {      
   public MyAction()
   {
   }
      
   public override void ItemAdding(SPItemEventProperties properties)
   {
    base.ItemAdding(properties);
    if (properties.ListTitle.Equals("Name of the CustomList"))
    {     
     //properties.ListItem is NULL
     properties.AfterProperties["internalname of the field"] = "some text";
    }
   }
  }
}

ItemAdded - Update ListItem
namespace MyEventHandler
{
 public class MyActionSPItemEventReceiver
 {       
  public MyAction()
  {
  }

   public override void ItemAdded(SPItemEventProperties properties)
   {
    base.ItemAdding(properties);
    if (properties.ListTitle.Equals("Name of the CustomList"))    
    {               
      properties.ListItem["internalname of the field"] = "some text";               
      properties.ListItem.Update();
    }
   }
  }
}

ItemAdding - Attachment

/?

ItemAdded - Attachment
namespace MyEventHandler 
{
 public class MyActionSPItemEventReceiver
 {      
  public MyAction()
  {
  }

  public override void ItemAdded(SPItemEventProperties properties)
  {
   base.ItemAdding(properties);
   if (properties.ListTitle.Equals("Name of the CustomList"))
   {              
     string filename = "Name of the file for attachment";
     FileStream fs = File.OpenRead("Address of the file for  attachment");               
     byte[] cv = new byte[fs.Length];
     fs.Read(cv, 0, cv.Length);
     string desturl = properties.ListItem.Attachments.UrlPrefix +  filename;
     properties.ListItem.Attachments.Add(desturl, cv);
     properties.ListItem.Update();
    }
   }
  }
}

No comments:

Post a Comment