Hi,
I got error as “Please try again.<nativehr>0x80004005</nativehr><nativestack></nativestack>”
I have tried with this below code,when I’m adding item to list.
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace EventReceivers1.EventReceiver1
{
/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
/// <summary>
/// An item is being updated.
/// </summary>
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
}
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdding(properties);
try
{
bool allowed = checkItem(properties);
if (!allowed)
{
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = “The Job is defined in the Employee Definition List”;
properties.Cancel = true;
}
}
catch (Exception ex)
{
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = ex.Message;
properties.Cancel = true;
}
}
bool checkItem(SPItemEventProperties properties)
{
string empTitle = properties.AfterProperties[“Title”].ToString();
bool allowed = false;
SPWeb empweb = null;
SPList empdeflist;
SPUser privilegedAccount = properties.Web.AllUsers[@”SPSERVER0\user1″];
SPUserToken privilegedToken = privilegedAccount.UserToken;
try
{
using (SPSite site = new SPSite(properties.Web.Url, privilegedToken))
{
using (SPWeb web = site.OpenWeb())
{
empweb = web.Webs[“practice”];
empdeflist = empweb.Lists[“Employement”];
foreach (SPListItem item in empdeflist.Items)
{
if (item[“Title”].ToString() == empTitle)
{
allowed = true;
break;
}
}
}
} return allowed;
}
finally
{
empweb.Dispose();
}
}
}
}
I don’t know where I’ve done mistake.Give me if any ideas.
Thanks,
Manju
Well, there are lots of issues with the current code. To mention, empweb musy not be disposed at all, and this is just the beginning.
Probably, it is might be considered to test the code inside the console application, understanding how it works with checking some books or MSDN articles and, finally, implementing event receiver at all? 🙂