Script editor Help Sharepoint

Majedix 1 Reputation point
2022-11-02T14:11:44.737+00:00

Hi All,

Need your help , i got stucked to find a way to force users to write something to when they Click Process or Reject Btn , since the current script can be bypassed when click Submit, Any thoughts to do it.

<script>  
        let Permission_Groups = ["Approvers"],  
        sp, context;  
  
        hide_Progress();  
  
        $(window).bind("load", function (){  
            initiate_Script()  
        });  
        async function initiate_Script () {  
  
            sp = pnp.sp;  
            context = _spPageContextInfo;  
  
			$("input[value*='Close']:visible").before(`<input type='button' class="action_btn" id="btn_Reject" onClick='Change_Status(this)' value='Reject'>`)  
  
			$("input[value*='Close']:visible").before(`<input type='button' class="action_btn" id="btn_Process" onClick='Change_Status(this)' value='Process'>`)  
  
            $("input[value*='Close']:visible").before(`<input type='button' class="action_btn" id="print_Page" onClick='window.print()' value='Print'>`)  
  
            if (await check_Permission()) {  
  
                if ($('a[name="SPBookmark_Processed"]').closest("td").next().text().trim() === "Yes") {  
                    $("#btn_Reject, #btn_Process").attr("disabled", true)  
                }  
  
            }  
  
            let action_btn_tbl = $(".action_btn").closest("table").clone();  
  
            $("table.ms-formtable").next().find("table").remove();  
  
            $(action_btn_tbl).addClass("action_btn_tbl")  
  
            $("table.ms-formtable").after(action_btn_tbl);  
        }  
  
        async function check_Permission() {  
  
            let res = await sp.web.currentUser.groups.get();  
  
            let groups = res.map(item => item.LoginName);  
  
            let permission = false;  
  
            groups.forEach(g => {  
  
                Permission_Groups.includes(g) ? permission = true :"";  
  
            });  
  
            return permission;  
  
        }  
  
        function Change_Status(status){  
  
            let item_ID = getUrlParameter("ID");  
  
            let selected_Option = $(status).val();  
  
            if (selected_Option === "Process") {  
  
                alertify.prompt( 'Provide Authorized Inspector No', '', '',  
                    function(evt, value) {  
  
                        show_Progress();  
  
                        let item_Obj = {  
                            Status : "Processed",  
                            Processed_x0020_ById : context.userId,  
                            Processing_x0020_Time : new Date().toISOString(),  
                            Processed : "Yes",  
                            Inspector_No : value,  
                        };    
  
  
                        sp.web.lists.getByTitle(context.listTitle).items.getById(item_ID).update(item_Obj).then(res => {  
  
                            hide_Progress();  
  
                            $("#SPFieldChoice").text("Processed");  
  
                            $("a[name='SPBookmark_Inspector_No']").closest("td").next().text(value)  
  
                            alertify.success("Request has been processed.", 3, function () {  
                                $("input[value='Close']").click();  
                            });  
                        }).catch(e => {  
  
                            hide_Progress();  
  
                            alertify.error("There is some issue occurs while updating the <strong>Status</strong>");  
                        });  
                    },  
                    function() {  
                        alertify.error('Cancel')  
                    }  
                ).set('labels', {ok:'Submit', cancel:'Cancel'});  
  
            } else if (selected_Option === "Reject") {  
  
                alertify.prompt( 'Provide valid reason to reject this request.', 'Reason', 'Enter reason here..',  
                    function(evt, value) {  
  
                        show_Progress();  
  
                        let item_Obj = {  
                            Status : "Rejected",  
                            Processed_x0020_ById : context.userId,  
                            Processing_x0020_Time : new Date().toISOString(),  
                            Processed : "Yes",  
                            Rejection_x0020_reason : value  
                        };  
  
                        sp.web.lists.getByTitle(context.listTitle).items.getById(item_ID).update(item_Obj).then(res => {  
  
                            hide_Progress();  
  
                            $("#SPFieldChoice").text("Rejected");  
  
                            $("a[name='SPBookmark_Rejection_x0020_reason']").closest("td").next().text(value)  
  
                            alertify.success("Request has been Rejected.", 3, function() {  
                                $("input[value='Close']").click();  
                            });  
                        }).catch(e => {  
  
                            hide_Progress();  
  
                            alertify.error("There is some issue occurs while updating the <strong>Status</strong>");  
                        });  
                    },  
                    function() {  
                        alertify.error('Cancel')  
                    }  
                ).set('labels', {ok:'Submit', cancel:'Cancel'});  
            }  
        }  
  
        function getUrlParameter(name) {  
  
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");  
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),  
            results = regex.exec(location.search);  
  
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));  
        }  
  
        function show_Progress() {  
            $("#loader").show();  
        }  
  
        function hide_Progress() {  
            $("#loader").hide();  
        }  
  
    </script>  
SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,298 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,810 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2022-11-03T05:24:17.987+00:00

    Hi @Majedix
    You can use the required attribute without JS such as

    <input id="email" type="email" required>  
    <input id="user_name" type="user_name" required>  
    

    Or you can use following JS

    $('#form').submit(function() {  
        if ($.trim($("#email").val()) === "" || $.trim($("#user_name").val()) === "") {  
            alert('you did not fill out one of the fields');  
            return false;  
        }  
    });  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. Limitless Technology 44,121 Reputation points
    2022-11-03T08:36:13.64+00:00

    Hello there,

    Have you tried using REST API ?

    We can create a POST request to the following endpoint in order to add a comment on SharePoint list item:

    https://<tenant>.sharepoint.com/sites/<site>/_api/web/lists/getbytitle('<list-name>')/items(<item-id>)/Comments()
    with comment properties (payload):

    var commentProperties = {
    "__metadata": {
    "type": "Microsoft.SharePoint.Comments.comment"
    },
    "text": "Comment added using SharePoint REST API"
    }

    ------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--


  3. Majedix 1 Reputation point
    2022-11-05T18:40:17.817+00:00

    Any help guys

    0 comments No comments