0

I'm developing Sharepoint Add-ins using the Visual Studio 2015 environment. I'm using the JavaScript Object Model to develop this add-in.

I want to get list of files from the Sharepoint website where this add-in will run.

When I run the add-in for testing out my code, it gets automatically hosted on a subdomain with naming convention <tenancy_name>-<autogenerated_number>

Note that

  1. Tenancy name is suffixed with some auto-generated number and
  2. The URI has the development site name followed by addin name.

In my project properties, the Site URL property is set to a URL with subdomain <tenancy_name>

When I browse to documents from web UI, the URL I see has <tenancy_name> as subdomain. There is no addin name that appears in URI here (for obvious reasons). The URI indicates existance of Shared Documents document library: /_layouts/15/start.aspx#/Shared%20Documents/Forms/AllItems.aspx.

Note that Shared Documents is reachable in such a URL that hasn't got auto-generated number suffixed to tenancy name in subdomain.

I've written a JavaScript code that first retrieves all lists from the website. I wanted to check whether "Shared Documents" appears in that list. But it seems "Shared Documents" doesn't appear in the lists I retrieve.

Please find below my JavaScript code.

// Function to execute basic operations.

function execOperation() {

    //ExecuteOrDelayUntilScriptLoaded(getAllFiles, "sp.js");//not prefered way
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAllFiles);//more prefered way

}
function getAllFiles() {
    try {

        var clientContext = new SP.ClientContext.get_current();
        alert("Context URL: " + clientContext.get_url());//Gives /sites/&lt;development_site&gt;/&lt;addin_name&gt;
        this.oWebsite = clientContext.get_web();

        this.allLists = this.oWebsite.get_lists();

        clientContext.load(this.oWebsite);
        clientContext.load(this.allLists);

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.successHandler),
            Function.createDelegate(this, this.errorHandler)
            );
    }
    catch (e) {
        alert("message:" + e.message);
    }
    alert("successful completion");

}

function successHandler(sender, args) {
    $('#message').text('success ');
    alert('Title: ' + this.oWebsite.get_title() +
' Description: ' + this.oWebsite.get_description()
+ " url: " + this.oWebsite.get_url());//Gives entire url with &lt;tenancy_name&gt;-&lt;autogenerated_number&gt; in subdomain
    var listInfo = '';
    var listEnumerator = this.allLists.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += 'Title: ' + oList.get_title() + ' Created: ' +
            oList.get_created().toString() + '\n';
    }
    alert(listInfo);//Doesn't give "Shared Documents" here

}

function errorHandler() {
    //$('#message').text('error ');
    document.getElementById("message").innerHTML = "Failure";
}

My question is

  1. Is my understanding correct about fact that Shared Documents is accessible only if website.geturl() returns subdomain as simple <tenancy_name> and not as <tenancy_name>-<autogenerated_number>?
  2. Is it possible to access Shared Documents irrespective of what URL gets generated from website.get_url()?

What is missing in my code and understanding relationship between URL viz Document Library and lists.

2
var clientContext = new SP.ClientContext.get_current();
this.oWebsite = clientContext.get_web();

gives you the web https://<tenancy_name>-<autogenerated_number>.sharepoint.com/sites/<development_site>/<addin_name>, so the code is actually querying the add-in web, not https://<tenancy_name>.sharepoint.com/sites/<development_site>/ (the host site), that's why it cannot display Shared Documents.

In order to query host site, we need SP.AppContextSite.

var hostSiteUrl = 'host site url' // This value is passed to your add-in as a url parameter called SPHostUrl, you can get it from document.URL
var clientContext = SP.ClientContext.get_current();
var appContextSite = new SP.AppContextSite(clientContext, hostSiteUrl);
var web = appContextSite.get_web(); // This is the web object points to host site
var lists = web.get_lists();

clientContext.load(lists);
...

And don't forget to set 'Read' permission for 'Web' scope in Visual Studio.

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.