Wednesday, April 22, 2015

Most Viewed Documents Web Part

Hi,

There is a document library called Sales Workshop.

Now our customer wants as web part in which top 10 documents can be scrolled which are viewed or downloaded by users.
First, have created a list with name MostViewed, added two columns with names 1)Count as type Number 2)Url as type string.
Post this, created a view TopView to display items based upon Count column in descending order.
Item Limit has been set to 10 so that to display top 10 documents
For this, console application has created to fetch the count from SharePoint logs and to insert the same into the list.
private static void InsertDataintoList()
        {
            //Change Url
            using (SPSite site = new SPSite(Constants.NucleusProdSiteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    //For QA
                    //SPList list = web.Lists[Constants.SalesWorkshopLibraryQA];
                    //For Prod
                    SPList list = web.Lists[Constants.SalesWorkshopLibraryProd];
                    SPList MostViewedList = web.Lists[Constants.MostViewedListName]; ;
                    SPAuditQuery spQuery = new SPAuditQuery(site);
                    spQuery.RestrictToList(list);
                    SPAuditEntryCollection auditCol = site.Audit.GetEntries(spQuery);
                    // Getting Audits 
                    try
                    {
                        foreach (SPAuditEntry entry in auditCol)
                        {

                            if (entry.ItemType == SPAuditItemType.Document && entry.Event == SPAuditEventType.View && (entry.EventSource ==SPAuditEventSource.ObjectModel || entry.EventSource == SPAuditEventSource.SharePoint))
                            {
                                SPListItem itemToAdd = MostViewedList.Items.Add();
                                if (((entry.DocLocation.ToString().ToLower().IndexOf("/learn/") > -1) || (entry.DocLocation.ToString().ToLower().IndexOf("/present/") > -1) || (entry.DocLocation.ToString().ToLower().IndexOf("/send/") > -1)) && !(entry.DocLocation.ToString().ToLower().IndexOf("/learn/corporate/market trigger") > -1) && !(entry.DocLocation.ToString().ToLower().IndexOf("/learn/corporate/water cooler chatter") > -1) && !(entry.DocLocation.ToString().ToLower().IndexOf(".aspx") > -1))
                                {
                                    SPQuery query = new SPQuery();
                                    query.Query = "<Where><Eq><FieldRef Name='LinkTitle'/>" +
                                            "<Value Type='Text'>" + entry.DocLocation.Substring(entry.DocLocation.LastIndexOf("/") + 1).ToString() + "</Value></Eq></Where>"
                                            + "<OrderBy><FieldRef Name='Count' Ascending='FALSE' /></OrderBy>";
                                    query.RowLimit = 5;
                                    SPListItemCollection items = MostViewedList.GetItems(query);
                                    if (items.Count == 1)
                                    {
                                        foreach (SPListItem item in items)
                                        {
                                            item["Count"] = Convert.ToInt32(item["Count"]) + 1;
                                            item.Update();

                                        }

                                    }
                                    else if (items.Count == 0)
                                    {
                                        itemToAdd["Title"] = entry.DocLocation.Substring(entry.DocLocation.LastIndexOf("/") + 1);
                                        itemToAdd["Url"] = entry.DocLocation.ToString();
                                        itemToAdd["Count"] = 1;
                                        itemToAdd.Update();

                                    }
                                }
                            }
                           

                        }
                        Console.WriteLine("All Items have been inserted into Most Viewed List");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }                   
                   
                }
            }
        }
Now in the web part, we are just fetching the items from the list and displaying items.
We are displaying the list items in the scrolling manner through marquee tag as below
In the below cs we are just fetching the list items from list view with Hyperlink control and
Binding the hyperlink control on the panel in the user control

Of course, I could have done the console application stuff in the  web part i.e. getting data from SharePoint logs.
But as I am deleting the items, getting the consolidated count from SharePoint logs and updating the column Count.
It is taking too much to load the web part page.

That is why I have created console application to handle the same.
Created scheduler in the production server to run the console application exe daily.

No comments:

Post a Comment