簡體   English   中英

如何在SharePoint 2013中使用REST API + JQuery訪問文檔的URL?

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

我正在對SP2013 doc庫進行基本的OData / REST調用。 我試圖找到項目的URL,但無法確定如何執行此操作。 我對服務器端對象模型非常熟悉,並且理解文件對象比項目更深一級。 有人能指出我正確的方向或分享如何進入文件級別的文檔? 我已經搜索谷歌了。 這是我的代碼,僅用於訪問doc庫中的所有項目以及我希望定位的任何元數據列:

<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>

對於完整的網址,請嘗試:

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

使用帶有FileRef參數的$ select查詢選項返回文檔Url:

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

參考

在SharePoint REST請求中使用OData查詢操作

我認為你可以使用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"
  }
});

至少那可以給你一些想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM