SP.Web.getFileByServerRelativeUrl(fileUrl) not working

落花浮王杯 提交于 2019-12-13 02:48:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!