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
Attached source adds items to the List.
Strongly encourage to refer number of Microsoft’s free Training resources for SharePoint, Nik has composed of them at http://nikpatel.net/2013/10/29/best-free-online-video-training-available-for-sharepoint-2013-and-office-365-professionals/
*Note that the attached source has been tested successfully.
Â
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? 🙂
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″];
Okay, what is the exactly line of the code which produces the current error?