none
Delete list Items version using javascript in sharepoint 2013 RRS feed

  • Question

  • Hi,

    How to Delete list Items version using javascript in sharepoint 2013.

    Thanks

    Mukesh


    Thanks Mukesh

    Tuesday, July 17, 2018 7:11 AM

All replies

  • Hi,

    List Items can be deleted using content editor web part. Insert the web part and link the content link with the javascript file.

    Add following code in the javascript file to delete the items and save the javascript file in SiteAssets.

    <script type="text/javascript">
    function DeleteItem() {
     debugger;
        this.itemId = 1;
     
        var ctx = new SP.ClientContext();
     
        var List = ctx.get_web().get_lists().getByTitle('CustomList');
     
        this.ListItem = List.getItemById(itemId);
     
        ListItem.deleteObject();
     
     
        ctx.executeQueryAsync(
     
            Function.createDelegate(this, this.onQuerySucceeded),
     
            Function.createDelegate(this, this.onQueryFailed)
     
        );
     
    }
     
     
    function onQuerySucceeded() {
     
        alert('List Item deleted: ' + itemId);
     
    }
     
     
    function onQueryFailed(sender, args) {
     
        alert('Request failed. ' + args.get_message() +
     
            '\n' + args.get_stackTrace());
     
    }
     
    </script>

    Tuesday, July 17, 2018 10:22 AM
  • Hi,

    Simple code to delete listitem versions for your reference.

    <script type="text/javascript">
    
            SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function () {
                var objClientCtx = new SP.ClientContext.get_current();
                if (objClientCtx) {
                    var oWeb = objClientCtx.get_web();
                    //list
                    var oList = oWeb.get_lists().getByTitle("ListA");
                    var query = new SP.CamlQuery();
                    //comment for test purpose
                    //query.set_viewXml(xmlQuery);
                    var objlistItems = oList.getItems(query);
                    objClientCtx.load(objlistItems);
                    objClientCtx.executeQueryAsync(function (sender, args) {
                        //that.DataSet = [];
                        var objlistEnumerator = objlistItems.getEnumerator();
                        while (objlistEnumerator.moveNext()) {
                            var objListItem = objlistEnumerator.get_current();
                            var id = objListItem.get_item('ID');
                            var filePath =  '/Lists/ListA/' + id + '_.000'
                            var web = objClientCtx.get_web();
                            var listItemInfo = web.getFileByServerRelativeUrl(filePath)
                            var listItemFields = listItemInfo.get_listItemAllFields()
                            objClientCtx.load(web);
                            objClientCtx.load(listItemInfo);
                            objClientCtx.load(listItemFields);
                            //objClientCtx.load(versions1);
                            objClientCtx.executeQueryAsync(
                                function (sender, args) {
                                    var fileVersions = listItemInfo.get_versions();
                                    fileVersions.deleteAll();
                                    debugger;
                                    objClientCtx.executeQueryAsync(
                                       function () {
                                           console.log('Versions have been deleted');
                                       },
                                       function (sender, args) {
                                           console.log('error')
                                       });
    
                                },
                                function (sender, args) {
                                    console.log('error')
                                });
    
                        }
                        //onGetSuccess(that.DataSet);
    
                    }, function (sender, args) {
                        that._onGetFail(sender, args);
                    });
                }
            })
        </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, July 18, 2018 2:48 AM