Search This Blog

Wednesday, August 2, 2017

One List to Another List Insert multiple item with Folder Sharepoint 2013

Step 1: Add Script Editor in your page
Step 2: Change ListName ,Site Name
Step 3: Paste Below code

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>One List to Another List Inser</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script type="text/javascript">

        //We need to run our function only after sp.js file loads
        ExecuteOrDelayUntilScriptLoaded(ViewItem, "sp.js");
        function ViewItem() {
            var context = new SP.ClientContext.get_current();
            var web = context.get_web();
            var list = web.get_lists().getByTitle('testList');

            //It will fetch all the items from the list
            var myquery = new SP.CamlQuery.createAllItemsQuery();
            myquery.set_viewXml("<View><Query><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy></Query><RowLimit>5</RowLimit></View>");
            myItems = list.getItems(myquery);
            context.load(myItems);
            //If the items load correctly then success function will be called
            context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
        }
        var newObj = { data: [] };
        function success() {
            var ListEnumerator = this.myItems.getEnumerator();
            while (ListEnumerator.moveNext()) {
                //Will get the current row from the SP List
                var currentItem = ListEnumerator.get_current();
                newObj.data.push({
                    "ID": currentItem.get_item('ID'),
                    "Title": currentItem.get_item('Title')
                });
            }         
        }

        function failed(sender, args) {
            alert("failed. Message:" + args.get_message());
        }
        function createListItem() {

            var contextz = new SP.ClientContext.get_current();
            var web = contextz.get_web();
            contextz.load(web);
            var oList = web.get_lists().getByTitle('testList2');
            contextz.load(oList);        
            var itemCreateInfo = new SP.ListItemCreationInformation();
            // need to specify full path after site domain , ex:         
            itemCreateInfo.set_folderUrl('/sites/test/DGNS/Lists/testList2/testFolder');
            var newItem = null;
            $.each(newObj.data, function (key, item) {
                newItem =  oList.addItem(itemCreateInfo);             
                newItem.set_item('Title', item.Title);
                //set values to other columns of the list here
                newItem.update();
            });
            contextz.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
        function onQuerySucceeded() {

        }
        function onQueryFailed(sender, args) {
            alert("failed. Message:" + args.get_message());
        }


    </script>
</head>
<body>
    <div class="ui-widget">      
        <input id="Submit1" type="submit" onclick="createListItem();" value="Submit Item in Folder" />
    </div>
</body>

</html>