0

I want to create a document library in o365, when an item is created on the list, but i cant get rid of the error: Unable to get property "apply" of undefined or null reference. Here is my code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript"></script> 
<script src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script> 
<script type="text/ecmascript">

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', spReady);

function spReady (){
    window.onload = Load;

    function Load() {
        var saveButton = document.getElementById("ctl00_ctl34_g_a64d8ee5_2ecd_4e41_badf_2d821f49a109_ctl00_toolBarTbl_RightRptControls_ctl00_ctl00_diidIOSaveItem");
        saveButton.onclick = createList;
    }

    function createList() {
        var clientContext = new SP.ClientContext.get_current();
        var oWebsite = clientContext.get_web();

        var listCreationInfo = new SP.ListCreationInformation();
        var ListTitle = document.getElementById("Title_fa564e0f-0c70-4ab9-b863-0177e6ddd247_$TextField").value; // takes the name
        listCreationInfo.set_title(ListTitle); // list name
        listCreationInfo.set_description(ListTitle + 'documents'); // list description
        listCreationInfo.set_templateType(SP.ListTemplateType.documentLibrary); //list type

        oWebsite.get_lists().add(listCreationInfo);

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded),// when success
            Function.createDelegate(this, this.onQueryFailed) // when failed
            );
    }

    function onQuerySucceeded() {
        alert("List Created");
    }

    function onQueryFailed(sender, args) {
        alert("List Failed" + args.get_message());
    }


}

</script> 

I placed the code in NewForm.aspx, when I click "Save" the library is created, but the item is not and the form isnt closing.

Im a beginner with js so please be patient with my noob questions :)

0

Can't remember doing async calls at save before, so not sure its the best solution, but it works:

    function createList() {
        this.dfd = $.Deferred();

        var clientContext = new SP.ClientContext.get_current();
        var oWebsite = clientContext.get_web();

        var listCreationInfo = new SP.ListCreationInformation();
        var ListTitle = $(":input[title='Title Required Field']").val();  // takes the name
        listCreationInfo.set_title(ListTitle); // list name
        listCreationInfo.set_description(ListTitle + 'documents'); // list description
        listCreationInfo.set_templateType(SP.ListTemplateType.documentLibrary); //list type

        oWebsite.get_lists().add(listCreationInfo);

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded),// when success
            Function.createDelegate(this, this.onQueryFailed) // when failed
            );

        return this.dfd.promise();
    }

    function onQuerySucceeded() {
        alert("List Created");
        this.dfd.resolve(true);
    }

    function onQueryFailed(sender, args) {
        alert("List Failed" + args.get_message());
        this.dfd.resolve(false);
    }



 function PreSaveAction() { 

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { 
       createList().done(function(val) {
            if(val === true)
            {
                 WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions($('input[id$="diidIOSaveItem"]').attr("Name"), "", true, "", "", false, true));
            }
       });
   });
}

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.