tags:

views:

141

answers:

3

Hi evereybody,

i'm working on a sharepoint publishing site and i want to "log" all the changes made by users in a sharepoint list. Some researches on the web guided me to the EventReceiver. But my EventReceiver must be generic and attached to all lists in the site collection. The "log list" has several columns like the name of the list in which the event occured, the name of the modified item, its old value and its new value. Can someone guides me on how to achieve this.

Thank you vey much.

+1  A: 

It sounds possible. Create a class that inherits from SPItemEventReceiver and override ItemUpdating. You can use the following code to get the list:

    using (SPWeb web = properties.OpenWeb())
    {
        SPList list = web.Lists[properties.ListId];
    }

You can then use list to grab the list's title and URL. Next, compare each DictionaryEntry entry in properties.AfterProperties to the corresponding value in properties.ListItem to get your differences. Then save it to your log list. The trick would be automatically attaching your Event Receiver to each newly created list. Perhaps a timer job would work.

That said...

Before trying any of that, go to your site collection's Site Settings. Under Site Collection Administration, click Site collection audit settings. Under Specify the Document and Item events to audit, check Editing items. Only go with a custom solution if that does not give you what you need.

Rich Bennema
He is actually talking about a solution where "my EventReceiver must be generic and attached to all lists in the site collectio".
Janis Veinbergs
His question can be simplified as follows: "i want to "log" all the changes made by users in a sharepoint list. Can someone guides me on how to achieve this." The Event Receiver might be a wild goose chase, but I addressed a generic implementation just in case. The first answer to his overall question should be Audit Settings whether OOTB or custom. Event Receivers are a secondary solution only if Audit is deemed insufficient.
Rich Bennema
+1  A: 

Would Version history not achieve the same functionality?

You can see what fields were changed by who and when. Though you would have to look at a list by list basis.

You can also access the version history information via the object model if you want to generate reports web part.

Otherwise using Janis Veinbergs link on how to attach a event handler to all lists and Rich Bennema method of getting the values from the updated item in a generic way, though I would use ItemUpdated as you don’t want to change the data that was updated only copy it to another location so there is no need to catch the data before it is submitted to the SharePoint database

JC Vivian
I chose ItemUpdating in case you want to log only the fields that have changed. In ItemUpdated, the AfterProperties values will be the same as those in the ListItem. And BeforeProperties are only available for Document Libraries. For more info, see http://www.synergyonline.com/blog/blog-moss/Lists/Posts/Post.aspx?ID=25
Rich Bennema
I see so you cant check to see that AfterProperties are diffrent to the ListItem in ItemUpdated, Thanks for clearing that up.
JC Vivian