Search code examples
sharepointsharepoint-2010documentreceiptuser-tracking

Track if the users have read documents Sharepoint 2010 Document Center


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?


Solution

  • 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.