0

I have a list with items. I have added a button in the ribbon, which is opening a confirmation page(dialog window) if user is sure that he want to create a new item from the current one and redirect the user to the edit form of the new list item

 <button class="yesButton" onclick="insert()" id="yesButton">Yes</button>

 function insert() {
                SP.SOD.executeFunc('sp.js', 'SP.ClientContext', loadContext);
                      }

function loadContext() {
            clientContext = new SP.ClientContext.get_current();
            web = clientContext.get_web();


                oList = web.get_lists().getByTitle('Hiring Request');
                clientContext.load(oList);
                sourceItem = oList.getItemById(1);
                clientContext.load(sourceItem);
                clientContext.executeQueryAsync(createItem, copyFailed)
                }
    function createItem() {        
                itemCreateInfo = new SP.ListItemCreationInformation();
                targetItem = oList.addItem(itemCreateInfo);
                targetItem.set_item('Title', sourceItem.get_item('Title'));
                targetItem.set_item('CompanyName', sourceItem.get_item('CompanyName'));
                targetItem.set_item('OrganisationalUnit', sourceItem.get_item('OrganisationalUnit'));
                targetItem.set_item('Position', sourceItem.get_item('Position'));
                targetItem.set_item('DutiesAndResponsibilities', sourceItem.get_item('DutiesAndResponsibilities'));
                targetItem.set_item('Degree', sourceItem.get_item('Degree'));
                targetItem.set_item('Specialty', sourceItem.get_item('Specialty'));
                targetItem.set_item('Experience', sourceItem.get_item('Experience'));
                targetItem.set_item('ProfessionalExpertise', sourceItem.get_item('ProfessionalExpertise'));
                targetItem.set_item('ForeignLanguages', sourceItem.get_item('ForeignLanguages'));
                targetItem.set_item('PCSkills', sourceItem.get_item('PCSkills'));
                targetItem.set_item('Competences', sourceItem.get_item('Competences'));
                targetItem.set_item('CurrentRequestApprover', sourceItem.get_item('CurrentRequestApprover'));
                targetItem.set_item('ApprovedCandidate', sourceItem.get_item('ApprovedCandidate'));
                targetItem.set_item('Mentor', sourceItem.get_item('Mentor'));
                targetItem.set_item("BooleanSiteColumn", false);
                targetItem.update();

                closeDialog();
             }

        function closeDialog() {
            SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, 2);
        }

        function copyFailed() {
            console.log("failed");
        }

Above you can see my code, which is not working. I am guessing maybe it is not able to load some part of the context, but sadly there are no errors in the console and when debugging it is not even entering in the createItem function, but it is consol logging and closing the window, which is strange.

If this solution is not a well practice, I am open for new suggestions how to achieve the result using JSOM

  • it is console logging means it's failing right (going into copyFail)? – harshal gite Sep 12 at 10:42
  • nope, if I put console log in function createItem() it is showing the messages as well as alerts, but not always. But interesting is that breakpoint are not working. Can't debug, can't see any errors, it is a very strange behaviour – SharePointNewbie Sep 12 at 10:43
1

Have you tried this?

function createItem() {        
    itemCreateInfo = new SP.ListItemCreationInformation();
    targetItem = oList.addItem(itemCreateInfo);
    targetItem.set_item('Title', sourceItem.get_item('Title'));
    targetItem.set_item('CompanyName', sourceItem.get_item('CompanyName'));
    targetItem.set_item('OrganisationalUnit', sourceItem.get_item('OrganisationalUnit'));
    targetItem.set_item('Position', sourceItem.get_item('Position'));
    targetItem.set_item('DutiesAndResponsibilities', sourceItem.get_item('DutiesAndResponsibilities'));
    targetItem.set_item('Degree', sourceItem.get_item('Degree'));
    targetItem.set_item('Specialty', sourceItem.get_item('Specialty'));
    targetItem.set_item('Experience', sourceItem.get_item('Experience'));
    targetItem.set_item('ProfessionalExpertise', sourceItem.get_item('ProfessionalExpertise'));
    targetItem.set_item('ForeignLanguages', sourceItem.get_item('ForeignLanguages'));
    targetItem.set_item('PCSkills', sourceItem.get_item('PCSkills'));
    targetItem.set_item('Competences', sourceItem.get_item('Competences'));
    targetItem.set_item('CurrentRequestApprover', sourceItem.get_item('CurrentRequestApprover'));
    targetItem.set_item('ApprovedCandidate', sourceItem.get_item('ApprovedCandidate'));
    targetItem.set_item('Mentor', sourceItem.get_item('Mentor'));
    targetItem.set_item("BooleanSiteColumn", false);
    targetItem.update();

    clientContext.load(targetItem);
    clientContext.executeQueryAsync(copySuccessful, copyFailed);
}

function copySuccessful(){
    closeDialog();
}

Try this code. I have added promise pattern (jQuery deferred) for 1 function.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<button class="yesButton" onclick="insert()" id="yesButton">Yes</button>

function insert() {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', loadContext);
}
var dfd = $.Deferred();

function loadContext() {
    clientContext = new SP.ClientContext.get_current();
    web = clientContext.get_web();
    oList = web.get_lists().getByTitle('Hiring Request');
    clientContext.load(oList);
    sourceItem = oList.getItemById(1);
    clientContext.load(sourceItem);
    clientContext.executeQueryAsync(callSuccessful, callFailed);
}

function createItem() {        
    itemCreateInfo = new SP.ListItemCreationInformation();
    targetItem = oList.addItem(itemCreateInfo);
    targetItem.set_item('Title', sourceItem.get_item('Title'));
    targetItem.set_item('CompanyName', sourceItem.get_item('CompanyName'));
    targetItem.set_item('OrganisationalUnit', sourceItem.get_item('OrganisationalUnit'));
    targetItem.set_item('Position', sourceItem.get_item('Position'));
    targetItem.set_item('DutiesAndResponsibilities', sourceItem.get_item('DutiesAndResponsibilities'));
    targetItem.set_item('Degree', sourceItem.get_item('Degree'));
    targetItem.set_item('Specialty', sourceItem.get_item('Specialty'));
    targetItem.set_item('Experience', sourceItem.get_item('Experience'));
    targetItem.set_item('ProfessionalExpertise', sourceItem.get_item('ProfessionalExpertise'));
    targetItem.set_item('ForeignLanguages', sourceItem.get_item('ForeignLanguages'));
    targetItem.set_item('PCSkills', sourceItem.get_item('PCSkills'));
    targetItem.set_item('Competences', sourceItem.get_item('Competences'));
    targetItem.set_item('CurrentRequestApprover', sourceItem.get_item('CurrentRequestApprover'));
    targetItem.set_item('ApprovedCandidate', sourceItem.get_item('ApprovedCandidate'));
    targetItem.set_item('Mentor', sourceItem.get_item('Mentor'));
    targetItem.set_item("BooleanSiteColumn", false);
    targetItem.update();

    clientContext.load(targetItem);
    clientContext.executeQueryAsync(copySuccessful, copyFailed);

    return dfd;
}

function callSuccessful(){
    $.when(createItem())
       .done(function (data) {

          closeDialog();

       })
       .fail(function (sender, args) {
          alert('Failed');
       });
}

function callFailed(){
    console.log("Call Failed!");
}

function copySuccessful(){
    dfd.resolve(sender, args);
}

function copyFailed() {
    dfd.reject(sender, args);
}

function closeDialog() {
    SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, 2);
}
  • Well, still not working. Sometimes it is creating new item, sometimes not. It is not closing the window now neither logging the function failed.. – SharePointNewbie Sep 12 at 10:59
  • Try adding console in copySuccessful – harshal gite Sep 12 at 11:04
  • strange behaviour again, it is creating item, but not always alerting and closing the window. Let's say from 10 clicks maybe and we will have closed window – SharePointNewbie Sep 12 at 11:16
  • It is because jsom is ASYNC. – harshal gite Sep 12 at 11:25
  • 1
    Ok will help you in sometime. – harshal gite Sep 12 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.