2

I want to track if the users have read sharepoint 2010 document center's documents, currently user infos are not stored in audit logs, is there any way to do it?

1
  • If you found a solution to your problem, it's better to add it as an answer (which you can then come back and mark as answered) rather than editing your question.
    – lgaud
    Commented Jul 5, 2013 at 14:27

2 Answers 2

6

On the Site Collection that you want to track reading, go toSite Actions –> Site Settings –> Site Collection Administration –> Site Collection audit settings. Under the Document and Items section you can enable the events you would like to audit:

enter image description here

Reference: How to enable Audit functionality in SharePoint 2010

2

I found the solution. Here is the steps.

1- Create a class library.

2- Right click the library and add new item.

3- Select ASP.NET Module under Web node.

4- Add PreRequestHandlerExecute event handler inside Init.Here is my code.

public void Init(HttpApplication context)    
{
   context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
}

5- Methods

    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            var app = sender as HttpApplication;
            if (app != null)
            {
                string requesturl = app.Request.Url.ToString();
                string file = string.Empty;
                // if document opens in browser
                if (app.Request.QueryString["Source"] != null && app.Request.QueryString["id"] != null && app.Request.QueryString["Source"].StartsWith(documentSiteUrl))
                {
                    file = app.Request.QueryString["id"].Remove(0, app.Request.QueryString["id"].LastIndexOf('/'));
                    Worker(file);
                }
                // if document opened installed office apps or downloaded the document
                if (requesturl.StartsWith(SiteUrl))
                {
                    requesturl = requesturl.Remove(0, requesturl.LastIndexOf('/') + 1);
                    Worker(requesturl);
                }
            }
        });

    }

    private void Worker(string requesturl)
    {

        #region ext control
        List<string> extList = new List<string>(Exts.Split(';'));
        bool result = false;
        foreach (string item in extList)
        {
            if (requesturl.EndsWith(item))
            {
                result = true;
                break;
            }
        }
        #endregion

        if ((!requesturl.Contains(".aspx")) && (!requesturl.EndsWith("/")) && result)
        {
            SPWeb web = SPContext.Current.Web;

            String fileName = requesturl.Substring(requesturl.LastIndexOf("/") + 1);

            // Add log
            web.AllowUnsafeUpdates = true;
            AddReadInfo(web.CurrentUser, fileName, web);
            web.AllowUnsafeUpdates = false;
        }
    }

    private void AddReadInfo(SPUser sPUser, string fileName, SPWeb web)
    {

        #region Logging

            SPList logList = web.Lists.TryGetList("LogList");
            if (logList != null)
            {
                SPListItem item = logList.Items.Add();
                item["User"] = sPUser.Name;
                item["Document"] = fileName;
                item["Read"] = "Read";
                item.Update();

            }

        #endregion

    }

6- Don't forget signing the project.

7- Build Project.

8- Add dll to GAC and BIN folder under the C:\inetpub\wwwroot\wss\VirtualDirectories\80\ folder.

9- Open IIS Manager.

10- Find your site nod and select.

11- Open Modules.

12- Right click under modules page and select Add Managed Module option.

13- Give a name and select your module under dropdownlist.

14- IIS reset.

15- That's it

2
  • 1
    Why did you opt to do this rather than use the built in auditing features? Commented Jul 8, 2013 at 13:09
  • 1
    Because of Auditing Features logs unnecessary content too. And i am using this module for user policy agreement page.
    – Yılmaz
    Commented Jul 9, 2013 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.