如何在 SharePoint 2013 中使用 REST API + JQuery 访问文档的 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21467159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:35:09  来源:igfitidea点击:

How to access a doc's URL using REST API + JQuery in SharePoint 2013?

jqueryapisharepoint

提问by Josh

I am doing basic OData/REST calls to a SP2013 doc library. I am trying to get down to the item's URL and can't determine how to do this. I am very familiar with the server-side object model and understand that the file object is one level deeper than the item. Can someone point me in the right direction or share documentation on how to get down to the file level? I have scoured google. Here's my code that works for simply getting access to all the items in doc library and any metadata columns I wish to target:

我正在对 SP2013 文档库进行基本的 OData/REST 调用。我正在尝试查看该项目的 URL,但无法确定如何执行此操作。我对服务器端对象模型非常熟悉,并且理解文件对象比项目深一层。有人可以指出我正确的方向或分享有关如何进入文件级别的文档吗?我已经搜索了谷歌。这是我的代码,用于简单地访问文档库中的所有项目和我希望定位的任何元数据列:

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
</html>

<script>
    // workaround for access error
    jQuery.support.cors = true;

    // create REST query
    var requestUri = "http://sp2013/_api/Web/Lists/getByTitle('Documents')/items"; 

    // execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function(data){
            alert(data.d.results);
            $.each(data.d.results, function(index, item){
                if (item["Meta1"] == null) {
                    $("body").append("<h1>No Title</h1>");
                }
                else {
                    $("body").append("<h1>" + item["Meta1"] + "</h1>");
                }
            });
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert(textStatus);
        }
    });
</script>

回答by athom

For the full url, try:

对于完整网址,请尝试:

"http://sp2013/_api/Web/Lists/getByTitle('Documents')/items?$select=EncodedAbsUrl"

回答by Vadim Gremyachev

Use the $select query option with FileRefparameter to return document Url:

使用带FileRef参数的 $select 查询选项返回文档 Url:

https://contoso.sharepoint.com/_api/web/lists/getbytitle('Documents')/items?$select=FileRef

References

参考

Use OData query operations in SharePoint REST requests

在 SharePoint REST 请求中使用 OData 查询操作

回答by AymKdn

I think you could use SharepointPlusto do that -- but it's a third party library. In the documentationthere is this example:

我认为你可以使用SharepointPlus来做到这一点——但它是一个第三方库。在文档中有这个例子:

// if you want to list all the files and folders for a Document Library
$SP().list("My Shared Documents").get({
  fields:"BaseName,FileRef,FSObjType", // "BaseName" is the name of the file/folder; "FileRef" is the full path of the file/folder; "FSObjType" is 0 for a file and 1 for a folder (you need to apply $SP().cleanResult())
  folderOptions:{
    show:"FilesAndFolders_Recursive"
  }
});

At least that could give you some ideas.

至少这可以给你一些想法。