0
SP.SOD.executeFunc("SP.js", "ClientContext", initilise_function);

function initilise_function() {
    //_url = _spPageContextInfo.webAbsoluteUrl;
    _url = _spPageContextInfo.webServerRelativeUrl;
}


function CreateListItem_Rest()
 {
    var listName = "RESTList2";
    var newItemTitle = "New Title Item";
    CreateListItemWithDetails(listName, newItemTitle, function () {
        console.log("New Item has been created successfully.");
    }, function () {
        console.log("Ooops, an error occured. Please try again.");
    });
}

function CreateListItemWithDetails(listName, newItemTitle, success, failure) {
    var dt = new Date();
    var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();

    var itemType = GetItemTypeForListName(listName);
    var item = {
        "__metadata": { "type": itemType },
        "Title": newItemTitle + String(time),
        "Test1": String(time),
        "Test2": "Rest data insert 2"
    };

    $.ajax({
        url: _url + "/_api/web/lists/getbytitle('" + listName + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "IF-MATCH": "*"
        },
        success: function (data) {
            //success(data);
            //console.log(data);
            alert(data.d);
        },
        error: function (data) {
            //console.log(data);
            alert(data.d);
        }
    });
}

// Get List Item Type metadata
function GetItemTypeForListName(name) {
    return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}

Now I have published it to SharePoint online. Added the App part in page.

Please help.

Error

SCRIPT5007: Unable to get property 'executeFunc' of undefined or null reference

The same JS working perfectly in Default.aspx page. When I am doing it as web part getting this error.

SEC7120: [CORS] The origin 'https://xxxxx.sharepoint.com' failed to allow a cross-origin document resource at 'ms-appx-web:///assets/errorpages/forbidframingedge.htm#https://login.microsoftonline.com/common/oauth2/authorize?response_type=id_token&client_id=89bee1f7-5e6e-4d8a-9f3d-ecd601259da7&redirect_uri=https%3A%2F%2Fwebshell.suite.office.com%2Fiframe%2FTokenFactoryIframe&state=8d63f13b-4539-4fe4-98f7-60b9f0ed8912&client-request-id=0f9915f5-24ed-4404-a0c4-55ccefabbee4&x-client-SKU=Js&x-client-Ver=1.0.15&nonce=6c61e5c7-9520-4ad5-8c04-4ce9966ea0d0'.

SEC7120: [CORS] The origin 'https://xxxxxxx.sharepoint.com' failed to allow a cross-origin document resource at 'ms-appx-web://microsoft.microsoftedge/assets/errorpages/forbidframingedge.htm#https://login.microsoftonline.com/common/oauth2/authorize?response_type=id_token&client_id=89bee1f7-5e6e-4d8a-9f3d-ecd601259da7&redirect_uri=https%3A%2F%2Fwebshell.suite.office.com%2Fiframe%2FTokenFactoryIframe&state=8d63f13b-4539-4fe4-98f7-60b9f0ed8912&client-request-id=0f9915f5-24ed-4404-a0c4-55ccefabbee4&x-client-SKU=Js&x-client-Ver=1.0.15&nonce=6c61e5c7-9520-4ad5-8c04-4ce9966ea0d0'

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.