EventReceiver not Firing on SharePoint List

event handlingevent-receiverlistsharepoint

I am trying to create an EventReceiver for a blog site (for the Posts list) and am having some trouble getting it working. I want to change the Created By column to Anonymous. Basically I have this whole thing working in a console application, however, that will only change the Created By column names when the console application is executed.

I need it to change the Created By whenever a new item is added to the list. My code is below….how do I modify this to use in an EventReceiver project??? Since I already tell the EventReceiver project the URL I want the EventReceiver attached to, I'm not sure what I can remove from this code, right now it just doesn't do anything, no error and no changing of the Created By column when I debug.

   using (SPSite site = new SPSite("http://test-sharepoint/subsite/")) 
   { 
       using (SPWeb web = site.OpenWeb()) 
       { 
           SPList list = web.Lists["Posts"]; 
           SPListItemCollection listItemCollection = list.Items; 

           foreach (SPListItem listItem in listItemCollection) 
           { 
               SPFieldUserValue userName = new SPFieldUserValue(web, 22, "Anonymous");
                listItem["Author"] = userName; 
               listItem["Editor"] = userName; 

               listItem.Update(); 
           } 
           web.Update(); 
       } 
    } 

EDIT: Code is in ItemAdded method

EDIT #2: This is trying the same code except without the loop and using properties.ListItem, this was my attempt in a Event Recevier project but no luck. It just doesn't change the Created By field, or any field for that matter (I tried the Title as well)

       SPSite site = new SPSite("http://test-sharepoint/subsite/");
       SPWeb web = site.OpenWeb();
       SPFieldUserValue userName = new SPFieldUserValue(web, 22, "Anonymous");

       properties.ListItem["Author"] = userName;
       properties.ListItem["Editor"] = userName;
       properties.ListItem.Update();

*Also from my understanding the SPFieldUserValue will grab either a User or a SharePoint User Group (Permissions) so in my code, the 22 grabs the SharePoint User Group that I want and "Anonymous" is the user from that group…


EDIT #3: More progress, this code works without issues for a list, however, not for the Posts or Comments lists, for those it does not change the Created By field. Could it be because of the approve/reject for all items??? Whether approved orpending it still does not show annonymous, BUT like I mentioned, it works fine in a different list.

   public override void ItemAdded(SPItemEventProperties properties)
   {
       base.ItemAdded(properties);

       SPSite site = new SPSite("http://test-sharepoint/hr/blog/"); //SPContext.Current.Site;
       SPWeb web = site.OpenWeb();
       SPFieldUserValue userName = new SPFieldUserValue(web,22,"Anonymous");

       SPListItem currentItem = properties.ListItem;
       //currentItem["Title"] = userName;  //DateTime.Now.ToString();
       currentItem["Author"] = userName;
       currentItem["Editor"] = userName;
       currentItem.SystemUpdate();
   }

**EDIT #4: Alright I found my issue, when creating the project I chose Custom List as my list to attach to but I needed to choose Posts or Comments and now the above code works!!!

But now I have another problem, all posts on the blog are first submitted for approval…and due to this the event receiver doesn't seem to work for users other than the admin. It works fine for the admin account where I can just directly publish a post or comment but for a user with Contribute permissions whose posts are submitted for approval still shows their name on the Manage Posts page…what could I do about this? Any ideas?**

The code that works:

   public override void ItemAdded(SPItemEventProperties properties)
   {
       base.ItemAdded(properties);

       SPSite site = new SPSite("http://test-sharepoint/hr/blog/"); //SPContext.Current.Site;
       SPWeb web = site.OpenWeb();
       SPFieldUserValue userName = new SPFieldUserValue(web, 23, "Anonymous");

       SPListItem currentItem = properties.ListItem;
       currentItem["Author"] = userName;
       currentItem["Editor"] = userName;
       currentItem.SystemUpdate();
   }

Best Answer

In response to edit #4, when working with SharePoint, if code works when executed by the administrator account, but does not work when executed by a "normal" account, permissions are likely to blame.

See the answer to the question SharePoint/WSS: Modify “created by” field? for an example of an SPItemEventReceiver that modifies the Author field.

Note: Many SharePoint developers recommend against the use of RunWithElevatedPrivileges and suggest using impersonation instead. See my answer to the question In which situation use SPSecurity.RunWithElevatedPrivileges with superusertoken? for more details.