1

I have developed a SharePoint Hosted App in SharePoint Server 2013. Now to access lists in Host Web I pass in my code

_sppagecontextinfo.siteabsoluteurl

then all works fine and I can access the lists as the server returns

http://demo-server/mysite/

But my client can install the app in any subsite. That means for this I need to change the code to

_sppagecontextinfo.webabsoluteurl

but due to this change, the server returns

http://demo-server/mysite/SPHostedApp

Now this SPHostedApp at the end of URL is the name of SharePoint Hosted App, due to this it is not able to find the list and the App throws error. I am just confused why SharePoint is returning App name at the end of URL?

0

Usually, we get the hostweburl by handling url in JavaScript code like this:

var appWebContext;  
var listResult;  
var hostweburl;  

$(document).ready(function () {  
    getListData();  
});  

function getListData() {  
    appWebContext = new SP.ClientContext.get_current();  
    hostweburl = decodeURIComponent($.getUrlVar("SPHostUrl"));  
    var hostwebContext = new SP.AppContextSite(appWebContext, hostweburl);  

    var list = hostwebContext.get_web().get_lists().getByTitle('TestList');  
    alert(list);  
    var query = new SP.CamlQuery();  
    query.set_viewXml("<View><Query><Where><Contains><FieldRef Name='Title' /><Value Type='Text'>Test</Value></Contains></Where></Query></View>");  
    listResult = list.getItems(query);  
    appWebContext.load(listResult);  
    appWebContext.executeQueryAsync(onSucceded, onFailed);  
}  

function onSucceded(sender, args) {  
    var enumerator = listResult.getEnumerator();  
    while (enumerator.moveNext()) {  
        $('#message').append(" " + enumerator.get_current().get_item("Name"));  
    }  
}  

function onFailed(sender, args) {  
    alert('Failed to get host web: ' + args.get_errorDetails());  
}  
// jQuery plugin for fetching querystring parameters  
jQuery.extend({  
    getUrlVars: function () {  
        var vars = [], hash;  
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');  
        for (var i = 0; i < hashes.length; i++) {  
            hash = hashes[i].split('=');  
            vars.push(hash[0]);  
            vars[hash[0]] = hash[1];  
        }  
        return vars;  
    },  
    getUrlVar: function (name) {  
        return jQuery.getUrlVars()[name];  
    }  
});  

Get Host Web List data in SharePoint-Hosted App

0

Context information is passed to the SharePoint-hosted app on the query string. One of these query string parameters, SPHostUrl, is the URL to the host web. You can get the value of this parameter using the built-in function GetUrlKeyValue.

var hostUrl = GetUrlKeyValue("SPHostUrl");

The URL to the app web is also passed on query string as a parameter named SPAppWebUrl. This means you have two ways to get the URL to the app web: using the technique shown above or using properties of the _spPageContextInfo object.

var appUrl = GetUrlKeyValue("SPAppWebUrl");
var appUrl = _spPageContextInfo.webAbsoluteUrl;

Here's an example of how to use these values to get data from the host web in code running in the app web. Please note that to make this call you must request the appropriate permissions (e.g. Web.Read) in the app manifest.

function callToHostWeb() {
    var appUrl = GetUrlKeyValue("SPAppWebUrl");
    var hostUrl = GetUrlKeyValue("SPHostUrl");

    var url = appUrl + "/_api/SP.AppContextSite(@target)" + 
        "/Web/Lists/getByTitle('Products')/Items?" +
        "$select=Title&$top=15&@target='" + hostUrl + "'";

    var call = jQuery.ajax({
        url: url,
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }
    });
    call.done(function (data, textStatus, jqXHR) {
        var message = jQuery("#message");
        message.text("First 15 products in the host web list:");
        message.append("<br/>");
        jQuery.each(data.d.results, function (index, value) {
            message.append(value.Title);
            message.append("<br/>");
        });
    });
    call.fail(failHandler);
}
  • Thanks Rob. Using _sppageContextInfo.webAbsoluteUrl is not getting me to the host web's list, this variable returns me a URL that contains App name in end so it is not able to find list. I will try using GetUrlKeyValue("SPHostUrl") to access my lists in host web. Rob, the var url in your code will try finding list in app web or host web? – Zakir HC Sep 22 '18 at 16:35

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.