Suppose I have an app and install it to "https://me.sharepoint.com/" (My Office 365 developer site). In my app, user can type a web url and get all lists under this web. And here is my code:
var webUrl = 'https://me.sharepoint.com/';
var context = SP.ClientContext.get_current();
var appContextSite = new SP.AppContextSite(context, webUrl);
var web = appContextSite.get_web();
var lists = web.get_lists();
context.load(lists);
context.executeQueryAsync(function (sender, args) {
// Do something
}, function (sender, args) {
// Error handling
});
I set app permission to "Web/Read", so it can only access "https://me.sharepoint.com/". During testing, I set webUrl
to "https://me.sharepoint.com/sub1" (sub1
exists), and I get Access denied
error, which is the correct behaviour. But if I set webUrl
to "https://me.sharepoint.com/sub2" (sub2
doesn't exist), it loads "https://me.sharepoint.com/" and doesn't throw an exception. How can I catch this exception to tell the user it doesn't exist? Thanks in advance!
I tried to load web
and compare web.get_url()
with webUrl
to see if they are equal (To make it simple, we assume webUrl
is a full url). But it doesn't work if user types the url like "https://me.sharepoint.com/_layouts/15/start.aspx#/SitePages/DevHome.aspx" or even "https://me.sharepoint.com/Shared%20Documents/Page1.aspx?PageView=Shared&InitialTabId=Ribbon.WebPartPage&VisibilityContext=WSSWebPartPage". We can still get web from those urls, but web.get_url()
only returns "https://me.sharepoint.com". So under this condition, the logic even doesn't work for existed web.