Search This Blog

Thursday, August 2, 2018

How to add Custom Action Menu In SharePoint ListItem using Jquery

 The below code is used to add Custom Action Menu in SharePoint ListItem using JSOM.












Add a CEWP/Script Editor webpart in the "/sites/TeamSite/List/EmployeeDetails/AllItems.aspx" Page and paste the below code.

JSOM Code:

<script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
    $(document).ready(function() {
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', AddCustomUserActionToListItem);
    });

function AddCustomUserActionToListItem() {
    var clientContext = new SP.ClientContext();
    var oWeb = clientContext.get_web();
    var oList = oWeb.get_lists().getByTitle('EmployeeDetails');
    var userCustomActionColl = oList.get_userCustomActions();
    clientContext.load(oList, 'UserCustomActions', 'Title');
    clientContext.executeQueryAsync(function() {
        var customActionEnumerator = userCustomActionColl.getEnumerator();
        var foundAction = 0;
        while (customActionEnumerator.moveNext()) {
            var oUserCustomAction = customActionEnumerator.get_current();
            if (oUserCustomAction.get_title() == 'Custom Edit Page') {
                foundAction = 1;
                break;
            }
        }
        if (foundAction == 0) {
            var oUserCustomAction = userCustomActionColl.add();
            oUserCustomAction.set_location('EditControlBlock');
            oUserCustomAction.set_sequence(100);
            oUserCustomAction.set_title("Custom Edit Page");
            oUserCustomAction.set_url("/sites/TeamSite/Lists/Customer/dispform.aspx?ID={1}&Source=/sites/TeamSite/Lists/EmployeeDetails/AllItems.aspx");
            oUserCustomAction.update();
            clientContext.load(userCustomActionColl);
            clientContext.executeQueryAsync();
        }
    }, function(sender, args) {
        console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    });
}
</script>