1
var hostWebUrl;
var appWebUrl;
var appCtxSite;
hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
alert(hostWebUrl);
alert(appWebUrl);
function manageQueryStringParameter(paramToRetrieve) {
// Geting Error here-Unable to get property 'split' of undefined or null reference
    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 () {
    $(".BtnSumit").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('StartDate', $("#txtSDate").val());
    listItem.set_item('EndDate', $("#txEDate").val());
    listItem.set_item('Purpose', $("#txtPurpose").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);
}
Ravindra
  • 436
  • 2
  • 10
  • It seems you have blank value in string and you are trying to perform split operation. can you check what you get in document.URL.split("?")[1] ? – Ravindra Aug 11 '16 at 11:45

2 Answers2

2

Everyone copies this code from blogs and it has a serious bug when there are no parameters.

And you do not need the code..

What does this code getQueryStringParameter do?

Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79
1

There are only three "splits" in your code - and the error says you're trying to split on "undefined or null". So either

  • document.URL is undefined (unlikely) or
  • document.URL.split("?")[1] is undefined (I'd guess this is it..) or
  • one of the params[i] is undefined

(However, personally I'd remove that code and go for the first answer!)

Nils
  • 2,327
  • 15
  • 32