0

If the "Title" parameter isn't passed via the url, then my initial api call fails, but even failure, I need it continue to the my item save routine.

Unfortunately, an error is thrown on siteIdFromParameter = data.d.results[0].Id (because there isn't a parameter to execute the query against). In that situation, how would I just skip that "failure" and then pass null values for SiteUrl and SiteID?

function getUrlParam(parameter, defaultvalue){
    var urlparameter = defaultvalue;
    if(window.location.href.indexOf(parameter) > -1){
        urlparameter = getUrlVars()[parameter];
    }
    return urlparameter;
}

function addFeatureSuggestion() {

   var siteUrlFromParameter = getUrlParam('Title', _spPageContextInfo.webAbsoluteUrl);

    $.ajax({
     url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName2 + "')/items?$filter=SitePath eq '/sites/" + siteUrlFromParameter + "'",

        type: "GET",
        contentType: "application/json;odata=verbose",
        async: false,
        headers: {
           "Accept": "application/json;odata=verbose",
           "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
    }).then(function (data) {
        siteIdFromParameter = data.d.results[0].Id

        item = {
        "__metadata": { "type": "SP.Data.FeatureSuggestionsListItem" },
        "SiteURL": siteUrlFromParameter,
        "SiteId": siteIdFromParameter,
        "Title": $('#inputTitle').val(),
        "Suggestion": $('#inputSuggestion').val()
        };
    });
}
  • your .then( still executes right? It just fails on data.d.results[0].Id? – willman Mar 10 at 17:57
  • Yes.. it still executes, but fails :-( – Susan T. Mar 10 at 17:58
1

In the case the SiteUrl is nonexistent or invalid, your then promise resolution should still fire, but the data parameter passed to it will likely either contain an error object, or at least not contain the siteID your were trying to retrieve. Inside your then, create your item without the Site parameters, then inspect each sub-level of the returned data (for truth-iness) to determine whether you got good results, and only add in the Site values if you have a good result set:

function addFeatureSuggestion() {
    var siteUrlFromParameter = getUrlParam('Title', _spPageContextInfo.webAbsoluteUrl);

    $.ajax({
     url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName2 + "')/items?$filter=SitePath eq '/sites/" + siteUrlFromParameter + "'",


        type: "GET",
        contentType: "application/json;odata=verbose",
        async: false,
        headers: {
          "Accept": "application/json;odata=verbose",
         "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
    }).then(function (data) {
        item = {
            "__metadata": { "type": "SP.Data.FeatureSuggestionsListItem" },
            "Title": $('#inputTitle').val(),
            "Suggestion": $('#inputSuggestion').val()
        };

        if (data && data.d && data.d.results && data.d.results.length && data.d.results[0].Id) {
            siteIdFromParameter = data.d.results[0].Id

            item.SiteURL = siteUrlFromParameter;
            item.SiteId = siteIdFromParameter;
        }

        // do something with `item` here
    });
}   
| improve this answer | |

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.