问题
I am using SP.Web.getFileByServerRelativeUrl(fileurl)
function of sp.js library. My site hierarchy is like this
Host Web
App1 Web
DocumentLibrary1
App2 Web
wher App1 Web is a web created by my sharepoint 2013 app 1, and App2 Web is a web created by my sharepoint 2013 app 2. In app2 i try to get a file from App1 Web->DocumentLibrary1. here is my code
var ctx = SP.ClientContext.get_current();
var appctx = new SP.AppContextSite(ctx, 'hostweburl/app1');
var file = appctx.get_web().getFileByServerRelativeUrl("/DocumentLibrary1/myfile.xml");
ctx.load(file);
ctx.executeQueryAsync(function () {
var title = file.get_title();
}, this.onError);
but i get an error "Value doesn't fall within the expected range". Seems "/DocumentLibrary1/myfile.xml" is not a server relative url of the file. I also tried "/app1/DocumentLibrary1/myfile.xml" which is not working too. Who can help
回答1:
Put /sites//DocumentLibrary1/myfile.xml as getFileByServerRelativeUrl parameter takes
relative url with respect to server url.
for eg: "http:///sites/sitename/list/folder"
give "sites/sitename/list/folder" in server relative url.
来源:https://stackoverflow.com/questions/21383707/sp-web-getfilebyserverrelativeurlfileurl-not-working