0

I am trying to replace a column's content with a javascript button in SharePoint. By following http://buildsharepointconcepts.blogspot.com/2018/05/go-to-workflow-initiation-form-on.html

This seems to be working fine.

    function StartWorkflowJsLink(overrideCtx) {
        /*If you have multiple list views on page better hard code this ListID Variable*/
        var ListID = overrideCtx.listName;
        var ItemGuid = '';
        /*Replace Template ID*/
        var TemplateID = '{D184A9EF-2DFB-4D67-828C-9C2FAF85B3B9}'; /* Get it from workflow start page URL. It will be changed everytime you republish the workflow.*/
        var SourceURL = window.location.href;
        var ItemID = overrideCtx.CurrentItem.ID;
        var webAbsoluteURL = _spPageContextInfo.webAbsoluteUrl;
        var WFPageURL = '/wfsvc/9e06e2e17b974ce99c06ad30abf9229d/WFInitForm.aspx?';

        debugger;
        var workflowURL = webAbsoluteURL + WFPageURL + 'List=' + ListID + '&ID=' + ItemID + '&TemplateID=' + TemplateID + '&Source=' + SourceURL;
        var btnHTML = btnHTML = "<input type=\"button\" onclick=\"GotoWFStartPage('" + workflowURL + "');\" value=\"Start Workflow\"></input>";
        return btnHTML;

    }
    function GotoWFStartPage(workflowURL) {


    window.location.href = workflowURL;

}


function registerListRenderer() {

    var overrideCtx = {};
    overrideCtx.Templates = {};
    overrideCtx.Templates.Fields = {
        'Review': {
            'View': StartWorkflowJsLink
        }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
}

ExecuteOrDelayUntilScriptLoaded(registerListRenderer, 'clienttemplates.js');

However, I am trying to change the function to only display StartWorkflowJsLink if the Review column equals Start.

I tried following this article but it seems to not be working.

https://social.technet.microsoft.com/wiki/contents/articles/37332.sharepoint-2013-working-with-field-template-using-jslink.aspx

Any help that can be provided would be greatly appreciated.

Thank you.

2 Answers 2

0

Refer below code. This should work for you.

Here we are checking value of Review field in StartWorkflowJsLink function. And if it is equal to Start then only our logic to create a button will be executed.

function StartWorkflowJsLink(overrideCtx) {
        if(overrideCtx.CurrentItem.Review == "Start"){
           /*If you have multiple list views on page better hard code this ListID Variable*/
              var ListID = overrideCtx.listName;
              var ItemGuid = '';
              /*Replace Template ID*/
              var TemplateID = '{D184A9EF-2DFB-4D67-828C-9C2FAF85B3B9}'; /* Get it from workflow start page URL. It will be changed everytime you republish the workflow.*/
              var SourceURL = window.location.href;
              var ItemID = overrideCtx.CurrentItem.ID;
              var webAbsoluteURL = _spPageContextInfo.webAbsoluteUrl;
              var WFPageURL = '/wfsvc/9e06e2e17b974ce99c06ad30abf9229d/WFInitForm.aspx?';

              //debugger;
              var workflowURL = webAbsoluteURL + WFPageURL + 'List=' + ListID + '&ID=' + ItemID + '&TemplateID=' + TemplateID + '&Source=' + SourceURL;
              var btnHTML = btnHTML = "<input type=\"button\" onclick=\"GotoWFStartPage('" + workflowURL + "');\" value=\"Start Workflow\"></input>";
              return btnHTML;
        }else{}
    }
    function GotoWFStartPage(workflowURL) {


    window.location.href = workflowURL;

}


function registerListRenderer() {

    var overrideCtx = {};
    overrideCtx.Templates = {};
    overrideCtx.Templates.Fields = {
        'Review': {
            'View': StartWorkflowJsLink
        }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
}

ExecuteOrDelayUntilScriptLoaded(registerListRenderer, 'clienttemplates.js');
1

Updated answer based on the updated code in original post:

function StartWorkflowJsLink(overrideCtx) {
    var _value = overrideCtx.CurrentItem[overrideCtx.CurrentFieldSchema.Name];
    if (_value == 'Start')
    {
        /*If you have multiple list views on page better hard code this ListID Variable*/
        var ListID = overrideCtx.listName;
        var ItemGuid = '';
        /*Replace Template ID*/
        var TemplateID = '{D184A9EF-2DFB-4D67-828C-9C2FAF85B3B9}'; /* Get it from workflow start page URL. It will be changed everytime you republish the workflow.*/
        var SourceURL = window.location.href;
        var ItemID = overrideCtx.CurrentItem.ID;
        var webAbsoluteURL = _spPageContextInfo.webAbsoluteUrl;
        var WFPageURL = '/wfsvc/9e06e2e17b974ce99c06ad30abf9229d/WFInitForm.aspx?';

        //debugger;
        var workflowURL = webAbsoluteURL + WFPageURL + 'List=' + ListID + '&ID=' + ItemID + '&TemplateID=' + TemplateID + '&Source=' + SourceURL;
        var btnHTML = btnHTML = "<input type=\"button\" onclick=\"GotoWFStartPage('" + workflowURL + "');\" value=\"Start Workflow\"></input>";
        return btnHTML;
    }
    else
    return _value;
}

This will render a link only if the value of the Review field is "Start". Otherwise it will render just the text value of the Review field.

4
  • Hi Damjan, thanks for the reply. I updated my post to better explain what I am trying to do. Thanks for all of the help.
    – cackerman
    Commented May 22, 2018 at 20:01
  • Please see the updated answer. Commented May 22, 2018 at 20:06
  • Hi Damjan, unfortunately the script isnt working. Where does it specify the column name as "Review"?
    – cackerman
    Commented May 23, 2018 at 1:54
  • This should return the name of the current field ("Review"): overrideCtx.CurrentFieldSchema.Name, but you could also hardcode it Commented May 23, 2018 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.