Saturday, January 20, 2018

Chrono Web Part

Hi,

We had a requirement as below

1/ Add the new item to the target list
2/ After adding the item in the list, the value should be auto incremented under the Chrono Column
3/ Upload the document in the target document library
4/ After uploading the document, the value should be auto incremented under the Chrono Column
5/ In the Chrono Config list of the root site, the updated value should be visible under Chrono Increment Value column w.r.t target list/ library GUID
6/The Chrono field should be hidden in the target list new form and Editform.aspx
7/The Chrono field should be hidden in the target library Editform.aspx

For this the below java script has been developed

var oList, oListItem, oConfigList, oConfigListcount, count = 0, autoincrementcount = 0;
var MC2ChronoConfigList = 'MC2ChronoConfigList';
var ChronoIncrementValue = 'ChronoIncrementValue';
var ChronoFieldColumnName = 'ChronoFieldColumnName';
$(document).ready(function () {
    try {
        //Get the Chrono Field Auto Increment column details to hide
        getChronoColumnandHide(_spPageContextInfo.siteAbsoluteUrl, "{" + WPQ2FormCtx.ListAttributes.Id + "}");
    }
    catch (ex) {
        console.log(ex);
    }
});

function getChronoColumnandHide(webUrl, listGUID) {
    try {
        var fullUrl = webUrl + "/_api/web/lists/getByTitle('" + MC2ChronoConfigList + "')/items?$select=" + ChronoFieldColumnName + "&$filter= Title eq '" + listGUID + "'";
        $.ajax({
            url: fullUrl,
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
            },
            success: onQueryConfigChronoSucceeded,
            error: onQueryConfigChronoFailed
        });
    }
    catch (ex) {
        console.log(ex);
    }
}

function onQueryConfigChronoSucceeded(data) {
    if (data.d.results.length != 0) {
        SPUtility.GetSPField(data.d.results[0].ChronoFieldColumnName).Hide();
    }
}
function onQueryConfigChronoFailed() {
    alert("An error has occurred.");
}



// PreSaveAction Operation
// success: The function to execute if the call is sucessfull
// failure: The function to execute if the call fails
function PreSaveAction() {
    try {
        //WPQ2FormCtx.FormControlMode = 2 - Denotes current form is Edit Form
        //WPQ2FormCtx.FormControlMode = 3 - Denotes current form is New Form
        //WPQ2FormCtx.ListAttributes.ListTemplateType == 100 - Denotes current list is of type Document Library
        //WPQ2FormCtx.ListAttributes.ListTemplateType == 101 - Denotes current list is of type Generic List

        if (WPQ2FormCtx.ListAttributes.ListTemplateType == 101) {
            GetMC2ChronoConfigListData()
        }
        else if (WPQ2FormCtx.ListAttributes.ListTemplateType == 100) {
            if (WPQ2FormCtx.FormControlMode == 3) {
                GetMC2ChronoConfigListData()
            }
            else {               
                return true;
            }
        }
    }
    catch (ex) {
        console.log(ex);
    }

}

function GetMC2ChronoConfigListData() {
    //Get Client Context and Web object. 
    clientContext = new SP.ClientContext.get_current();
    var oWeb = clientContext.get_web();
    oConfigList = clientContext.get_site().get_rootWeb().get_lists().getByTitle(MC2ChronoConfigList);
    //CamlQuery to fetch records w.r.t List Guid
    var camlQuery = new SP.CamlQuery();
    var listGUID = "{" + WPQ2FormCtx.ListAttributes.Id + "}";
    camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Title\'/>' + '<Value Type=\'Text\'>' + listGUID + '</Value></Eq></Where></Query><RowLimit>10</RowLimit></View>');
    oConfigListcollListItemColl = oConfigList.getItems(camlQuery);
    clientContext.load(oConfigListcollListItemColl);
    clientContext.load(oConfigList);
    clientContext.executeQueryAsync(onQueryReadSucceeded, onQueryReadFailed);
}

// Auto Increment Operation
// success: The function to execute if the call is sucessfull
// failure: The function to execute if the call fails
//Method to Compare List Guid from Config List and update Auto Increment Value
function onQueryReadSucceeded(sender, args) {

    if (oConfigListcollListItemColl.get_count() != 0) {
        var oConfigListItem = oConfigListcollListItemColl.get_item(0);
        var strTitle = oConfigListItem.get_item('Title');
        var strChronoIncrementValue = oConfigListItem.get_item(ChronoIncrementValue);
        var strChronoFieldColumnName = oConfigListItem.get_item(ChronoFieldColumnName);
        var currValue = SPUtility.GetSPField(strChronoFieldColumnName).GetValue();
        if (!currValue) {
            SPUtility.GetSPField(strChronoFieldColumnName).SetValue(strChronoIncrementValue + 1)
            oConfigListItem.set_item(ChronoIncrementValue, strChronoIncrementValue + 1);
            oConfigListItem.update();
            clientContext.load(oConfigList);
            clientContext.executeQueryAsync(OnSuccess, OnFailure)
        }
        else
        {
            form.SubmitClientForm();
        }
    }
    else {
        alert("An error has occurred.");
    }
}

function onQueryReadFailed(sender, args) {
    alert('An error has occurred. ' + args.get_message() + '\n' + args.get_stackTrace());

}

function OnSuccess(sender, args) {
    form.SubmitClientForm();
}

function OnFailure(sender, args) {
    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Output
Chrono Configuration List
Target Document Library


Sunday, December 17, 2017

Patch Installation steps in SharePoint 2010

Hi,

1.What are the steps involved in Security update installation?

Below are the steps for installing any Cumulative Update/Service pack/Hot Fix/patch on SharePoint Farm (with multiple server scenario).
  • To be on safer side take copy of Inetpub and 14Hive (15Hive, 12Hive) folders.
  • Make note of Previous Upgrades from Central Admin(CA). Also review "Health Analyzer" reported issues on CA.
  • Make note of the BUILD number (farm version).
  • Install the patch on all servers in the Farm. You can do so simultaneously but don’t run the configuration wizard yet.
  • Ensure you restart all servers in the farm, after patch is installed.
  • Now run Configuration wizard first on Server hosting the CA site. Wait until it completes.
  • If wizard fails, try to fix the issue.
  • Run the configuration wizard on the next server in the farm, wait until it completes and repeat the same action on remaining servers.
  • After patching is done, verify the BUILD version if there is going to be a version number change.

2.Would there be any risk/ challenges during updating patch?

  • Config wizard may run for longer duration at step 9 as it can take some time for that step to process, especially if your content databases are large. There should not be any issue as long as it is progressing.
  • Config wizard may fail. Re-run it through CA once and if that too fails, try using ps command.

3.What are the precautions need to take before patching Security update?

  • It is always recommended to test the patch in test environment before doing the same on PROD. I hope you had already completed in QA & that should tell you about the discrepancies if any.
  • You can also work with your Database team to secure CDB backups before you install the patches and later you can restore them to the point when you took the backup should the patch installation fail.

Wednesday, May 13, 2015

Grid View Data Display on Image Button Click

Hi,

Through user control we are already displaying the data in the grid view.
Now the requirement is user should be able to see the arrow image button.
Once user clicks on the Image button, down arrow button should be displayed along with the grid view data as below

First we have written two functions to achieve this i.e.

1)ToggleRow function, it should toggle based by upon the user click on the image i.e. it should show grid view/hide.
2)Change Image function, by default it should display vertical arrow image, once user clicks on it, should display down arrow image along with grid view data
In the table, where grid view resides, we are calling them as below

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.