0
  $(document).ready(function () {
    getUserName();
    AddData("title from JS", "CRJ");



    function AddData(newItemTitle, school_locationn) {
        var listName = "School Name";            
        CreateListItemWithDetails(listName, _spPageContextInfo.webAbsoluteUrl, newItemTitle, school_locationn, function () {
            console.log("New Item has been created successfully.");
        }, function () {
            console.log("Ooops, an error occured. Please try again.");
        });
    }

    // CREATE Operation
    // listName: The name of the list you want to get items from
    // weburl: The url of the web that the list is in. 
    // newItemTitle: New Item title.
    // success: The function to execute if the call is sucesfull
    // failure: The function to execute if the call fails
    function CreateListItemWithDetails(listName, webUrl, newItemTitle, school_locationn, success, failure) {
        var itemType = GetItemTypeForListName(listName);
        var item = {
            "__metadata": { "type": itemType },
            "Title": newItemTitle,
            "School Location": school_locationn
        };

        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(item),
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function (data) {
                success(data);
            },
            error: function (data) {
                failure(data);
            }
        });
    }

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

    var fun1 = function () {


    }


});

Sharepoint Site COllection URL - http://xxxxxxxxx.win-xxxxx:47699/

After pressing F5 (Visual studion ) getting error in browser >

List 'School Name' does not exist at site with URL 'http://app-xxxxxxxxx.win-xxxxx:47699/SharePointAddIn1'.

I think the error is due to app url is different from site collection url , but my question is if so , how to fix it ? As my app will be in different url .

Please help . Thank you .

1 Answer 1

0

First of all this is not a JSOM code, this is a REST call.

And _spPageContextInfo.webAbsoluteUrl return the URL of current site in whose context this code is running.

So in order to run this code successfully you need to have this list on your current site on which this code is running.

Note: List name should be the display name of list and not the internal name(name from url).

3
  • Thanks for the analysis . I am new in jsom, previously I was working with server side code using visual webpart . All we develop Kipling in mind of sitecollection . Does here the concept is different ? As you said need to have list in app website .. so how sitecollection and app website related ? Please help me to understand the concept . Thank you . Commented Jan 5, 2019 at 11:37
  • For clarification on site vs site collection, check this link. Commented Jan 5, 2019 at 12:06
  • Thanks . I want to differentiate sitecollection & SharePoint app . If I cannot access sitecollection data through SharePoint app then what is the use of it ? Please guide . Commented Jan 5, 2019 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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