2

I want to insert data into SharePoint hosted app list through SharePoint hosted app,for that I am using bellow code.

I followed plenty of articles and many among them show same code,but this code is not working for me,so can any one tell me what's wrong with this code?

because I am alway getting failed message after making many changes by referring many articles but still it is not able to insert the record in hosted app list.

Code:

var hostWebUrl;
var appWebUrl;
var appCtxSite;
hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));

function manageQueryStringParameter(paramToRetrieve) {
    var params =
    document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve) {
            return singleParam[1];
        }
    }
}
$(document).ready(function () {
    $(".click").click(function () {
        Insert();
    });
});

var listItem;
function Insert() {
    var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context object based upon the URL
    var context = new SP.AppContextSite(ctx, hostWebUrl);
    // var web = context.get_web(); //Get the Site 
    var list = context.get_web().get_lists().getByTitle("List"); //Get the List based upon the Title
    var listCreationInformation = new SP.ListItemCreationInformation(); //Object for creating Item in the List
    listItem = list.addItem(listCreationInformation);
    listItem.set_item('Title', $("#txt_Title").val());
    listItem.set_item('Name', $("#txt_Name").val());
    listItem.update();
    ctx.load(listItem);
    ctx.executeQueryAsync(
        Function.createDelegate(this, success),
        Function.createDelegate(this, fail)
       );
}

function success() {
    alert("Completed");
}
function fail() {
    alert("failed -- " + appWebUrl);
}
8
  • please specify which error are you getting. Code looks ok. Aug 8, 2016 at 12:05
  • function fail() { alert("failed -- " + appWebUrl); },this function is geeting executed
    – Madhav
    Aug 8, 2016 at 12:11
  • write alert('Error:' + args.get_message()); this line in fail message and post the error message. The line you said is kept by you and it will not give any information about error Aug 8, 2016 at 12:14
  • failed--"app-282b73e2c5f9a2.app71.com/sites/2018/App1"
    – Madhav
    Aug 8, 2016 at 12:20
  • thiat is simply web app url
    – Madhav
    Aug 8, 2016 at 12:21

1 Answer 1

1
var hostWebUrl;
var appWebUrl;
var appCtxSite;
hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));

function manageQueryStringParameter(paramToRetrieve) {
    var params =
    document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve) {
            return singleParam[1];
        }
    }
}
$(document).ready(function () {
    $(".click").click(function () {
        Insert();
    });
});

var listItem;
function Insert() {
    var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context object based upon the URL
   // var context = new SP.AppContextSite(ctx, hostWebUrl);
    // var web = context.get_web(); //Get the Site 
    var list = ctx.get_web().get_lists().getByTitle("List"); //Get the List based upon the Title
    var listCreationInformation = new SP.ListItemCreationInformation(); //Object for creating Item in the List
    listItem = list.addItem(listCreationInformation);
    listItem.set_item('Title', $("#txt_Title").val());
    listItem.set_item('Name', $("#txt_Name").val());
    listItem.update();
    ctx.load(listItem);
    ctx.executeQueryAsync(
        Function.createDelegate(this, success),
        Function.createDelegate(this, fail)
       );
}

function success() {
    alert("Completed");
}
function fail() {
    alert("failed -- " + appWebUrl);
}



var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context object based upon the URL
    var context = new SP.AppContextSite(ctx, hostWebUrl);
    // var web = context.get_web(); //Get the Site 
    var list = context.get_web().get_lists().getByTitle("List"); //Get the List based upon the Title


This was mistake i was passing host url for accessing hosted web app list.

Your Answer

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

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