0

I have a Mandatory field in a list. I need to save the item and not create the item.

I need to implement the it using custom JavaScript and not use the save button on the new form.

Also need to check if the mandatory fields are set and stop the item from being saved and show the validation messages.

The following does save the item but does not check if the field is manadatory. It also redirects the page to the edit form as required.

$("input[id='DummyButton']").bind("click", function() {
    var clientContext = new SP.ClientContext(_spPageContextInfo.siteAbsoluteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Test');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    var oListItem = oList.addItem(itemCreateInfo);

    var title = $("input[id^='Title']").val();

    var Teststandard = $("div[id^='testStandard'] p").html();
    oListItem.set_item('Title', title);
    oListItem.set_item('Tenures', tenures);
    oListItem.update();

    clientContext.load(oListItem);
    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
    function onQuerySucceeded() {
        console.log('Item created: ' + oListItem.get_id());
        $(location).attr('href',_spPageContextInfo.siteAbsoluteUrl +    '/Lists/test/EditForm.aspx?ID=' + oListItem.get_id() + '&newedit=1'); 
    }
    function onQueryFailed(sender, args) {
        console.log('Request failed. ' + args.get_message() +'\n' + args.get_stackTrace());
    }
});

Is there a way to use the default save javascript or use the PreSaveAction() function?

  • Please give me more information 1. Are you creating a custom web part? 2. Can you manage to write server side code for saving the list item and do validations at client side? – Naveen May 26 '15 at 10:05
  • No I am writing custom JavaScript and adding it to a content editor web part. – user1339913 May 26 '15 at 10:18

Your Answer

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

Browse other questions tagged or ask your own question.