0

Overnight, with the help of this forum, I figured out how to save a parameter passed via a query string to my Sharepoint api call for "Name" field. I'd like to take this one step further, and instead of saving "Name", I want to save the "Id" that corresponds to the "Name". I know that "Name" is a Lookup field in my list.

The line of code I'm struggling with is this:

var siteIdFromParameter =   getSiteParam('Site', _spPageContextInfo.webAbsoluteUrl)?$Id="/_api/web/lists(guid'<guid>')"

Here's all my code:

        function getSiteVars() {
             var vars = {};
                var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
                vars[key] = value;
                });
            return vars;
        }

        function getSiteParam(parameter, defaultvalue){
            var siteparameter = defaultvalue;
            if(window.location.href.indexOf(parameter) > -1){
            siteparameter = getSiteVars()[parameter];
        }
            return getSiteParam;
    }


    function addFeatureSuggestion() {

        var siteNameFromParameter = getSiteParam('Site', _spPageContextInfo.webAbsoluteUrl);
        var siteIdFromParameter =   getSiteParam('Site', _spPageContextInfo.webAbsoluteUrl)?$Id="/_api/web/lists(guid'<guid>')"

        var item = {
            "__metadata": { "type": "SP.Data.AddListItem" },
            "SiteId": siteIdFromParameter,
            "Title": $('#inputTitle').val()
        };



  • Your code doesn't quite match up with your description. Do you want the ID of the Site specified by the SiteUrl querystring param? Or do you want a second querystring param called "Name" that will be used to find the ID of an item in a List? – willman Mar 5 at 15:50
  • Hi @willman I'm passing in one parameter (SiteUrl) and want to find the Id that corresponds with SiteUrl. – Susan T. Mar 5 at 15:54
1

To get the Id of the Site, you will need to make a separate web service call to /_api/Web before you save your new Item. If you are using .ajax to make your service calls, make sure to do your next step inside the .then or .done method since it is an asynchronous call. Note, this presumes that SiteUrl is a valid SharePoint site that the current user has at least read access to.

function getSiteVars() {
     var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
        });
    return vars;
}

function getSiteParam(parameter, defaultvalue){
    var siteparameter = defaultvalue;
    if(window.location.href.indexOf(parameter) > -1){
    siteparameter = getSiteVars()[parameter];
}
    return getSiteParam;
}


function addFeatureSuggestion() {
    var siteUrlFromParameter = getSiteParam('Site', _spPageContextInfo.webAbsoluteUrl);

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

        var item = {
            "__metadata": { "type": "SP.Data.AddListItem" },
            "SiteId": siteIdFromParameter,
            "Title": $('#inputTitle').val()
        };

        // do the rest of your item saving in here
    });
}   

| improve this answer | |
  • Hi @willman. I'll be trying this out later tonight. The .then is new to me. Reading up on that now. Will report back later. – Susan T. Mar 5 at 18:48
  • Well, I' haven't got it to work yet :-( When I use debugger in Chrome, it seems to "skip" right over this line of code var siteIdFromParameter = data.d.Id; – Susan T. Mar 6 at 13:32
  • Set a breakpoint inside the then function. It is an asynchronous call, and the then is the callback. If you are stepping through the code synchronously, you won't get to an asynchronous callback. – willman Mar 6 at 14:03
  • 1
    If you typically have used the .ajax method by passing in a success and error handler functions, you can certainly use that technique here too, just move the code that I put in the then method into your success handler. – willman Mar 6 at 14:38
  • 1
    oops, sorry for the syntax error, I have updated the answer with your discovery. – willman Mar 6 at 20:55

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.