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
Hi Manjunath,
There are few lines which may be the culprit
1- base.ItemAdding(properties); -> In IteAdded Event there must be base.ItemAdded(properties); instead of base.ItemAdding(properties);
2- string empTitle = properties.AfterProperties[“Title”].ToString(); -> Properties.AfterProperties always gives result in ItemAdding, ItemUpdating and ItemUpdated not in ItemAdded.
check this out:-http://technologykhabar.wordpress.com/2013/05/23/how-value-return-i…
Hope this will help you out.
One more suggestion:-
Try to use SPUser privilegedAccount = properties.Web.EnsureUser(@”SPSERVER0\user1″); instead of
SPUser privilegedAccount = properties.Web.AllUsers[@”SPSERVER0\user1″];