Creating Edit and View Buttons in SharePoint 2013 List View using JS Links

JS Links Intro in SharePoint 2013

  1. Client-side rendering is a new concept in SharePoint 2013. It’s provides you with a mechanism that allow you to use your own output render for a set of controls that are hosted in a SharePoint page (list views, display, add and Edit forms). This mechanism enables you to use well-known technologies, such as HTML and JavaScript, to define the rendering logic of custom and predefined field types.
  2. JSLink files have the ability to quickly and easily change how a list views and forms are rendered. More specifically how the fields in that list should be displayed.
  3. Note: I wrote those code samples to be easy to understand and to achieve learning purposes, because of that I avoided using complex code and controls. In the same time I tried to present real world examples as much as possible.
Creating Edit and View Buttons in SharePoint 2013 List View
  • First we want to create a list. For eg: I am creating the Employee as a custom List.
  • Fist we want to create two list columns like ItemEdit and ItemView  as Single line of Text we will show in the first two place in the list.
  • We will create html file as ViewEditOptions.html and we will write the code the in the file and we will insert these file in the List view page using the Content Editor Web Part.
  • In the html file we will use the button css as below under the Head section.
<style type="text/css">
        input.secondaryButton, input.secondaryButton:hover {
            min-width: 6em;
            padding: 7px 10px;
            border-top: 1px solid #666666;
            border-left: 1px solid #666666;
            background-color: #666666;
            margin-left: 10px;
            font-size: 13px;
            color: #ffffff;
            cursor: pointer;
            box-shadow: 0 1px 3px rgba(0,0,0,0.7);
        }
    </style>
1. Under script in the html file we will define the code for to create the View and Edit buttons for the list view.
2. Below script we will use to create the buttons 
<script>
"use strict";

jQuery(document).ready(function () {
    jQuery(":button").addClass('secondaryButton');
});

(function () {
    var fieldJsLinkOverride = {};
    fieldJsLinkOverride.Templates = {};
    fieldJsLinkOverride.Templates.Fields =
    {
        'ItemView': { 'View': GetPriorityFieldIconView },
        'ItemEdit': { 'View': GetPriorityFieldIconEdit }
    };

    // Register the rendering template
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldJsLinkOverride);

})();



function GetPriorityFieldIconView(clientContext) {

    var itemId = clientContext.CurrentItem.ID;//to Get current Item Id
    var viewButtonhtml = '<input type="button" id="btnItemView" value="View" style="cursor: pointer;margin-left:0px" onclick=\'EmployeeItemView(' + itemId + ');\'/> '//To bind the button
    return viewButtonhtml;
}

function GetPriorityFieldIconEdit(clientContext) {

    var itemId = clientContext.CurrentItem.ID;//to Get current Item Id
    var viewButtonhtml ='<input type="button" id="btnItemView" value="Edit" style="cursor: pointer;" onclick=\'EmployeeItemEdit(' + itemId + ');\'/>' //To bind the button
    return viewButtonhtml;
}

function EmployeeItemView(returnValue) {
    var link = _spPageContextInfo.webAbsoluteUrl + "/Lists/Employee/DispForm.aspx?ID=" + returnValue; //Current list item Display Form URL
    window.open(link, '_self');
}

function EmployeeItemEdit(returnValue) {
    var link = _spPageContextInfo.webAbsoluteUrl + "/Lists/Employee/EditForm.aspx?ID=" + returnValue; //Current list item Edit Form URL
    window.open(link, '_self');
}
</script>

3. Save the file with EmployeeEditView.html and upload it to any of the libraries like Site Assest/ Document Library.
4. Go to Employee list Default and edit the page insert the Content Editor Web Part and Add the html file url from the Uploaded file from the Library.
5. Finally below are the output  for the solution.



I Hope the code will help you... :) 

Happy Coding....Sharing is caring .... Keep Smiling...

Comments

Popular posts from this blog

SharePoint 2013 Keyword Query (KQL) Content Class Property Restrictions

Filtering the Sharepoint List Taxonomy Column using Rest API

SharePoint CSOM to Create Folders and Sub Folders based on Excel Data