.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Log Events in SharePoint

Posted By:Jean Paul       Posted Date: November 30, 2014    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

SharePoint through custom coding enables flexible control over events. We can capture site, list, item events through custom event receivers.
 

SharePoint through custom coding enables flexible control over events. We can capture site, list, item events through custom event receivers.

In this POC, I am going to demonstrate the Add/Edit/Event logging along with the properties.

Create 2 Lists

Please create the following 2 lists:

  1. Customer list of type Contacts
  2. Log list of type custom & with following fields:

 

The Log list stores all the events going through ALL LISTS within the site.  We can make this list hidden from quick launch too.

 

Create Event Handlers

Open Visual Studio, create a new SharePoint project & add the following event receiver.

 

Choose a list for the time being & select all the events as shown below.

 

Click the Finish button to create the event receiver.  You will get the following code window.

 

 

There are 2 types of events:

  • Asynchronous
  • Synchronous

 

For the time being, we are listening to the add/edit/delete events for both async/sync methods.

Following is the code.

 

public class ListEvents : SPItemEventReceiver
{
    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);

        if (properties.List.OnQuickLaunch)
            if (properties.List.Title != "Log")
                AddEvent("Add", "Item", properties.ListItem.Url, GetInformation(properties), properties);
    }

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

        if (properties.List.OnQuickLaunch)
            if (properties.List.Title != "Log")
                AddEvent("Edit", "Item", properties.ListItem.Url, GetInformation(properties), properties);
    }

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

        if (properties.List.OnQuickLaunch)
            if (properties.List.Title != "Log")
                AddEvent("Delete", "Item", properties.ListItem.Url, string.Empty, properties);
    }

    private void AddEvent(string action, string target, string targetUrl, string info, SPItemEventProperties properties)
    {
            SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPList list = properties.Web.Lists["Log"];
            if (list != null)
            {
                SPListItem item = list.Items.Add();
                item["Action"] = action;
                item["Target"] = target;
                item["TargetUrl"] = targetUrl;
                item["User"] = properties.Web.CurrentUser;
                item["Date"] = DateTime.Now;
                item["Information"] = info;
                item.Update();
            }
        });
    }

    private string GetInformation(SPItemEventProperties properties)
    {
        string result = string.Empty;

        foreach (SPField field in properties.ListItem.Fields)
            if (!field.Hidden)
                if (field.CanBeDisplayedInEditForm)
                    if (properties.ListItem[field.Id] != null)
                        result += field.Title + ":" + properties.ListItem[field.Id].ToString() + ";";

        return result;
    }        
}

 

About the Code

The code listens to events & add the Log item based on the type of events & properties of list items.

 

Make sure, you remove the ListTemplateId tag from the elements.xml.  This will make the event receiver generic & we should receive events on ALL LISTS & LIBRARIES within the site.

We can filter the list based on Quick Launch visibility property, to discard unwanted lists.

 

Run the Code

Execute the project, go to SharePoint site and perform the following:

  1. Create item in Customer
  2. Update the item
  3. Delete the item

 

Now, you should be able to see the log items from the Log list as shown below:

 

This concludes our logging information.

 

Report Viewer

In the perfect scenario, we need to have a web part where the user will be given options for:

  1. Select a list & view all events
  2. Select an item & view all events
  3. Select an item property & see WHO updated it
  4. Select a person & see WHAT was his/her recent operations based on DATE input

 

Note

Please note that we are skipping some of the lists & properties.  In real world scenario, some of these skipped ones might be useful.

 

Additionally, the asynchronous & synchronous events should be used appropriately in capturing meaningful logging information.

 

Reference

http://msdn.microsoft.com/en-us/library/gg749858(v=office.14).aspx

 

I have attached the associated project.

Please let me know if you have any queries on this.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend