作为使用 SPO REST API 的一部分,要为 "ViewFields" 参数发送的有效负载是什么?
What is the payload to be sent for "ViewFields" parameter as part of consuming the SPO REST API?
我正在尝试通过 SharePoint REST API 创建 SharePoint 列表视图,并将一组定义的列作为视图的一部分。我正在使用的端点如下:
POSTMAN API 请求:
HTTP 方法:POST
URL: https://tenantname.sharepoint.com/sites/SPSite/_api/web/lists/getbytitle('ListName')/views
Headers:
- 'Accept' - 'application/json;odata=verbose'
- 'Content-Type' - 'application/json;odata=verbose'
Body (JSON):
{
"__metadata":{
"type":"SP.View"
},
"Title":"TestView",
"ViewFields":["Title","Name"]
}
我收到 JSON 错误,因为此负载似乎不正确。在了解如何通过 SharePoint REST 创建具有特定字段的视图方面需要帮助 API。
谢谢,
是的
创建视图时不支持添加viewFields,需要在创建列表视图后添加。
所以请先创建这样的视图:
var viewQuery = "<OrderBy><FieldRef Name=\"ID\" /></OrderBy>";
$.ajax
({
// _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
// You can replace this with other site URL where you want to apply the function
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyList')/views",
type: "POST",
data: "{'__metadata':{'type': 'SP.View'},'ViewType': 'HTML','Title':'New View Created From REST','PersonalView':false,'ViewQuery':'" + viewQuery + "'}",
headers:
{
// Accept header: Specifies the format for response data from the server.
"Accept": "application/json;odata=verbose",
//Content-Type header: Specifies the format of the data that the client is sending to the server
"Content-Type": "application/json;odata=verbose",
// X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
alert(data.d.Id);
},
error: function (xhr, status, error) {
console.log("Failed");
}
});
然后像这样为新创建的列表视图设置Viewfield:
$.ajax
({
// _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
// You can replace this with other site URL where you want to apply the function
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyList')/Views(guid'58cfaaa2-107c-4a94-8490-38d1df195e5b')/ViewFields/addviewfield('Created')",
type: "POST",
headers:
{
// Accept header: Specifies the format for response data from the server.
"Accept": "application/json;odata=verbose",
//Content-Type header: Specifies the format of the data that the client is sending to the server
"Content-Type": "application/json;odata=verbose",
// X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
console.log("Success");
},
error: function (xhr, status, error) {
console.log("Failed");
}
});
所以上面的示例将“已创建”字段添加到 viewFields 中,并且 View Guid 在第一个请求中处于警报状态,在第二个请求中使用它。
我正在尝试通过 SharePoint REST API 创建 SharePoint 列表视图,并将一组定义的列作为视图的一部分。我正在使用的端点如下:
POSTMAN API 请求:
HTTP 方法:POST
URL: https://tenantname.sharepoint.com/sites/SPSite/_api/web/lists/getbytitle('ListName')/views Headers:
- 'Accept' - 'application/json;odata=verbose'
- 'Content-Type' - 'application/json;odata=verbose'
Body (JSON):
{
"__metadata":{
"type":"SP.View"
},
"Title":"TestView",
"ViewFields":["Title","Name"]
}
我收到 JSON 错误,因为此负载似乎不正确。在了解如何通过 SharePoint REST 创建具有特定字段的视图方面需要帮助 API。
谢谢, 是的
创建视图时不支持添加viewFields,需要在创建列表视图后添加。
所以请先创建这样的视图:
var viewQuery = "<OrderBy><FieldRef Name=\"ID\" /></OrderBy>";
$.ajax
({
// _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
// You can replace this with other site URL where you want to apply the function
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyList')/views",
type: "POST",
data: "{'__metadata':{'type': 'SP.View'},'ViewType': 'HTML','Title':'New View Created From REST','PersonalView':false,'ViewQuery':'" + viewQuery + "'}",
headers:
{
// Accept header: Specifies the format for response data from the server.
"Accept": "application/json;odata=verbose",
//Content-Type header: Specifies the format of the data that the client is sending to the server
"Content-Type": "application/json;odata=verbose",
// X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
alert(data.d.Id);
},
error: function (xhr, status, error) {
console.log("Failed");
}
});
然后像这样为新创建的列表视图设置Viewfield:
$.ajax
({
// _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
// You can replace this with other site URL where you want to apply the function
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyList')/Views(guid'58cfaaa2-107c-4a94-8490-38d1df195e5b')/ViewFields/addviewfield('Created')",
type: "POST",
headers:
{
// Accept header: Specifies the format for response data from the server.
"Accept": "application/json;odata=verbose",
//Content-Type header: Specifies the format of the data that the client is sending to the server
"Content-Type": "application/json;odata=verbose",
// X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
console.log("Success");
},
error: function (xhr, status, error) {
console.log("Failed");
}
});
所以上面的示例将“已创建”字段添加到 viewFields 中,并且 View Guid 在第一个请求中处于警报状态,在第二个请求中使用它。