我有一個 SharePoint 2013 檔案庫,其中一些影像存盤在名為“名稱”的列中。
我正在嘗試獲取影像的 URL,但它一直說“未定義”。我還嘗試獲取另一列“MyCol”(單行文本),它顯示未定義。我可以從檔案庫中獲取其他資訊(例如 JobTitle - 它是單行文本)
有任何想法嗎?
編輯:進一步挖掘告訴我,當我這樣做
console.log(JSON.stringify(data.d.results[result]));
好像沒有拉回 MyCol 值?
謝謝P
<script>
$.ajax ({
url: _spPageContextInfo.webAbsoluteUrl "/_api/web/lists/getbytitle('photos')/items?expand=Name",
dataType: 'json',
async: false,
headers: { "Accept": "application/json;odata=verbose" },
success: function(data) {
var html = '';
var count = 0;
for(var result in data.d.results) {
html ='Name:' data.d.results[result].Name '<br>'; //this gives undefined
html ='MyCol:' data.d.results[result].MyCol '<br>'; //this gives undefined
html ='Job Title:' data.d.results[result].JobTitle '<br><br>'; //this gets the data from the Document Library
count ;
if(count >= 6)
{
break;
}
}
$('#staffInformation').append(html);
},
error: function ajaxError(response){
console.log(response.status ' ' response.statusText);
}
});
</script>
uj5u.com熱心網友回復:
$expand 還用于從相關專案(通常是查找或用戶欄位)中獲取資料。
您想要的是$select指定要回傳的列。使用FileLeafRef獲取檔案名(也看一看FileRef或FileDirRef:
url: _spPageContextInfo.webAbsoluteUrl "/_api/web/lists/getbytitle('photos')/items?$select=FileLeafRef"
參考:在 SharePoint REST 請求中使用 OData 查詢操作。
作為旁注,您可以通過更改標頭來減少網路占用空間:
"Accept": "application/json;odata=nometadata"
回應將只包含資料,不包含元資料。只需調整回傳的物件屬性即可(沒有中間d)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/347670.html
