Friday, January 4, 2019

Javascript code - Copy selected Items from One SP list webpart to another SP LIst

var sourceListName="CustomerDetails";
var targetListName="WelcomeList";
var sourceListURL="https://.sharepoint.com/sites/m/vms/Lists/CustomerDetails/AllItems.aspx";
var targetListURL="https://your site/sites/SecurityTeam/Lists/WelcomeList/AllItems.aspx";
var siteUrl ="https://Target site URL/";
var sourceSiteUrl="yoursite/VisitorManagement/";
//var userName,Company;

function clickMethod() {             
                var ctx = SP.ClientContext.get_current();
                var items = SP.ListOperation.Selection.getSelectedItems(ctx);
                var mySeletedItems = '';
                var i=0;
             
                    for (i in items) {
                        mySeletedItems += '|' + items[i].id;
                        var itemID = items[i].id;
                        //createListItem();
                         GetRequestItem(itemID);
                    }
                //alert(mySeletedItems);
                return false;
        }

     function GetRequestItem(rItemID) {
           // alert("GetRequestItem called");
            // Getting our list items
            var ItemId = rItemID;
            var currentSiteUrl = "https://your site name/";
            var requestUri = currentSiteUrl + "/_api/web/lists/getbytitle('CustomerDetails')/items(" + ItemId + ")";

            $.ajax({
                url: requestUri,
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },
                success: function (data) {
                 
                    // Returning the results
                    //console.log(requestUri);
                    var userName = data.d.Title;
                    var userCompany = data.d.Visitor_x0020_Company;
                    var uDesignation = data.d.UserDesignation;
                    var salutation= data.d.Salutation;             
                    createListItem(userName,userCompany,uDesignation,salutation);

                },
                error: function (data) {
                    console.log(data.message);
                }
            });
        }

//ShowOnDisplayBoard
function createListItem(uName,uCompany,uDesig,salutation) {
//alert("createListItem starts now :"+siteUrl);
var userArray = uName.split('.');
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle(targetListName);
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title', uName);
    oListItem.set_item('Gender', salutation);
    oListItem.set_item('PersonName', uName);
    oListItem.set_item('UserCompanyName', uCompany);
    oListItem.set_item('UserDesignation', "DemoDesignation");
     oListItem.set_item('OrderBy', "1");
     oListItem.set_item('UserImage', "https://(yoursite)/sites/ST/SiteAssets/WelcomePage/DemoImage.png");
    oListItem.update();
    clientContext.load(oListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
    );
    window.location.href = window.location.href;
}

function onQuerySucceeded() {
    alert('Selected Item is copied to welcome list: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
}



No comments:

Power Automate + Excel data

Extract excel data, get manager and update row with user details which will get from GET MANAGER Concepts and examples Key column:  Column i...