public class CustomEventReceiver : SPItemEventReceiver
{
private HttpContext _currentContext = null;
public CustomEventReceiver () : base ()
{
if (null != HttpContext.Current)
{
_currentContext = HttpContext.Current
}
}
public override void ItemAdding (SPItemEventProperties properties)
{
using(SPSite site = new SPSite(properties.SiteId))
{
using(SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
{
SPList list = web.Lists[properties.ListId];
DisableEventFiring();
SPListItem itemToAdd = list.Items.Add();
foreach (SPField field in itemToAdd.Fields)
{
if (!field.Hidden && !field.ReadOnlyField && field != null && field.InternalName != "Attachments")
{
itemToAdd[field.InternalName] = properties.AfterProperties[field.InternalName];
}
}
itemToAdd.Update();
EnableEventFiring();
// Redirect
SPUtility.Redirect("/cn/Lists/Bills%20of%20Lading/DispForm.aspx?ID=" + itemToAdd.ID, SPRedirectFlags.Default, _currentContext);
}
}
}
}
Subscribe to:
Post Comments (Atom)
2 comments:
Where do you place this code?
This is event receiver on item adding
Post a Comment