Downloads using SPAudit entry

Hi,

Our requirement is to get the filenames,number of downloads as a report under present/send/learn folders of doc library.

For this i am trying to get this using SPAudit entry as below.

But problem here is,if there downloads of a file,it is making each entry for each download.

So finally it is displaying as below

filename,1

filename,2

filename,3

But i want to display as filename,3

please let me know if i am making any mistake here.

using (SPSite site = new SPSite("http://servername:portno/sites/Nucleus/"))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists["doclibname"];

dt.Columns.Add("Folder");

dt.Columns.Add("No of Downloads");

SPAuditQuery spQuery = new SPAuditQuery(site);

spQuery.RestrictToList(list);

SPAuditEntryCollection auditCol = site.Audit.GetEntries(spQuery);

int noofdownloads = 0;

foreach (SPAuditEntry entry in auditCol)

{

 if (entry.ItemType == SPAuditItemType.Document && entry.Event == SPAuditEventType.View)

{

if ((entry.DocLocation.ToString().ToLower().IndexOf("/send/") > -1) || (entry.DocLocation.ToString().ToLower().IndexOf("/learn/") > -1) || (entry.DocLocation.ToString().ToLower().IndexOf("/present/") > -1))

{

dr = dt.NewRow();

dr["File Name"] = entry.DocLocation.Substring(entry.DocLocation.LastIndexOf("/") + 1);

if (entry.EventSource == SPAuditEventSource.SharePoint)

{

noofdownloads = noofdownloads + 1;

}dr["No of Downloads"] = noofdownloads.ToString();

}

}

}

DataTable uniqueCols = dt.DefaultView.ToTable(true"File Name","No of Downloads");          

ExportToExcel(uniqueCols); 

}

}

private void ExportToExcel(DataTable dt)

        {

            if (dt.Rows.Count > 0)

            {

                //excel file name         

                string filename = "Report"  + ".xls";

 

                DataGrid dgGrid = new DataGrid();

                dgGrid.DataSource = dt;

                dgGrid.DataBind();

 

                System.IO.StringWriter tw = new System.IO.StringWriter();

                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

 

                dgGrid.RenderControl(hw);

 

                HttpContext.Current.Response.Clear();

                HttpContext.Current.Response.AddHeader("content-disposition"string.Format("attachment; filename={0}", filename));

                HttpContext.Current.Response.ContentType = "application/ms-excel";

                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

                //render the htmlwriter into the response 

                HttpContext.Current.Response.Write(tw.ToString());

                HttpContext.Current.Response.End();             

            }

        }

Regards,

Sudheer

January 31st, 2015 12:26pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics