0
votes

Can anybody tell me why this code doesn't work? The "adding code" itself works, but unfortunately not in an ItemAdded Event. I need this code in the ItemAdded Event and therefor i cannot use ItemAdding.

Thanks for any help.

public override void ItemAdded(SPItemEventProperties properties)
    {
        SPSite site = new SPSite("http://air_sim:39167/");
        SPWeb web1 = site.RootWeb;
        SPList List = web1.Lists["Announcements"];
        SPListItem newitem = List.Items.Add();
        newitem["Title"] = "Example";
        newitem.Update();



    }
4
Are you able to step through the code using the debugger? Does it give you any error message or is this event handler not reached?Rich Ross
Unfortunately i have no experience in debugging Event Receivers. I searched the internet and found some posibilities, but i am not able to reproduce. I tried a similar code a month ago and there i had no problems therefore i don't understand this.Fabian
It is just like debugging any other code in SP2010. Just set a break point on the ItemAdded method and press F5 in Visual Studio.Rich Ross

4 Answers

3
votes
  1. Did you do any steps to attach event receiver to your list?

    If no, you can install a feature to manage event receivers and verify that the event receiver is added and if not, add it manually: http://chrissyblanco.blogspot.com/2007/08/event-receiver-management.html

  2. Maybe exception is thrown somwere? For example, if such site or list with such name doesn't exist, exception will be thrown. Also if you don't initialise required fields of your item, the Update() call will throw exception.

By the way the properties variable contains many useful properties:

SPListItem newitem = properties.List.Items.Add();
newitem["Title"] = "Example";
newitem.Update();
0
votes

Do you use Sharepoint 2010 or Sharepoint2007? Do you use VS2008 or VS2010? If you couldn’t use debugger, use EventLog:

public override void ItemAdded(SPItemEventProperties properties)
{
    EventLog.WriteEntry("DebugSharepoint", "ItemAdded fired");
    try
    {
        SPSite site = new SPSite("http://air_sim:39167/");
        SPWeb web1 = site.RootWeb;
        SPList List = web1.Lists["Announcements"];
        SPListItem newitem = List.Items.Add();
        newitem["Title"] = "Example";
        newitem.Update();

    }
    catch(Exception e)
    {
        EventLog.WriteEntry("DebugSharepoint", e.Message, EventLogEntryType.Error);        
    }
}
0
votes

Attach a debugger. Go to cmd and type iisapp. You would get the worker process id. Then open your event handler project and go to the tools and attach process and set the debug point on ItemAdded as well as ItemAddding event

0
votes

Try the below solutions:

  1. Check whether the Site exists with that name.
  2. Check whether user has the permission to insert item.
  3. Try using AllowUnsafeUpdates: SPSite site = new SPSite("site address"); SPWeb web1 = site.RootWeb; SPList List = web1.Lists["Announcements"]; web1.AllowUnsafeUpdates = true; SPListItem newitem = List.Items.Add(); newitem["Title"] = "Example"; newitem.Update(); web1.AllowUnsafeUpdates = false;