SharePoint2013Odata常用例項基本操作
阿新 • • 發佈:2018-12-25
SharePoint 2013 的Odata service的常用URI例項:
URI例項 | 解釋 |
_api/web/title | 返回web title |
_api/web/lists(guid'<list id>') | 返回列表 |
_api/web/lists/getByTitle('Announcements')/fields | 返回列表的所有欄位 |
_api/web/lists/getByTitle('Task')/items | 返回Tas列表的所有item |
_api/web/siteusers | 返回該站點所有使用者 |
_api/web/sitegroups | 返回該站點所有使用者組 |
_api/web/sitegroups(3)/users | 返回該站點所有在id為3的使用者組的使用者 |
_api/web/GetFolderByServerRelativeUrl('/Shared Documents') | 返回Shared Documents文件庫根目錄 |
_api/web/GetFolderByServerRelativeUrl('/Plans')/Files('a.txt')/$value | 返回Plans文件庫根目錄下的a.txt檔案 |
帶Query option的例項:
查詢選項 | 作用 |
$select | 指定返回欄位(列) |
$filter | 指定過濾條件 |
$expand | 用於關聯列表,指定返回關聯欄位(列) |
$top | 指定返回前N條記錄 |
$skip | 指定返回結果跳過前N行記錄 |
$orderby | 指定按某個欄位排序 |
URI例項 | 解釋 |
|
前提book裡面有個 |
|
返回Books列表的3~10項紀錄 |
|
返回Books列表的3~12項紀錄 |
|
返回Books列表的最後兩條記錄 |
|
返回Author 等於 |
|
返回Books列表的所有記錄並按Title升序排列 |
|
返回Books列表下記錄(只返回列 |
使用JQuery + Odata實現增刪改查
按Json格式查詢:
[javascript] view plaincopyprint?
- function onGetCustomer(customerId) {
- // begin work to call across network
- var requestUri = _spPageContextInfo.webAbsoluteUrl +
- "/_api/Web/Lists/getByTitle('Customers')/items(" + customerId + ")";
- // execute AJAX request
- $.ajax({
- url: requestUri,
- type: "GET",
- headers: { "ACCEPT": "application/json;odata=verbose" },
- success: function(data){
- alert(data.d.FirstName);
- },
- error: function(){
- alert("Failed to get customer");
- }
- });
- }
function onGetCustomer(customerId) {
// begin work to call across network
var requestUri = _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('Customers')/items(" + customerId + ")";
// execute AJAX request
$.ajax({
url: requestUri,
type: "GET",
headers: { "ACCEPT": "application/json;odata=verbose" },
success: function(data){
alert(data.d.FirstName);
},
error: function(){
alert("Failed to get customer");
}
});
}
新增:
[javascript] view plaincopyprint?
- function onAddCustomer() {
- var requestUri = _spPageContextInfo.webAbsoluteUrl +
- "/_api/Web/Lists/getByTitle('Customers')/items/";
- var requestHeaders = {
- "ACCEPT": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- }
- var customerData = {
- __metadata: { "type": "SP.Data.CustomersListItem" },
- FirstName: "Paul"
- };
- requestBody = JSON.stringify(customerData);
- $.ajax({
- url: requestUri,
- type: "Post",
- contentType: "application/json;odata=verbose",
- headers: requestHeaders,
- data: requestBody,
- success: function(){
- alert("Created customer");
- },
- error: function(){
- alert("Failed to create customer");
- }
- });
- }
- }
function onAddCustomer() {
var requestUri = _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('Customers')/items/";
var requestHeaders = {
"ACCEPT": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
}
var customerData = {
__metadata: { "type": "SP.Data.CustomersListItem" },
FirstName: "Paul"
};
requestBody = JSON.stringify(customerData);
$.ajax({
url: requestUri,
type: "Post",
contentType: "application/json;odata=verbose",
headers: requestHeaders,
data: requestBody,
success: function(){
alert("Created customer");
},
error: function(){
alert("Failed to create customer");
}
});
}
}
刪除:
[javascript] view plaincopyprint?
- function onDeleteCustomer(customerId) {
- var requestUri = _spPageContextInfo.webAbsoluteUrl +
- "/_api/Web/Lists/getByTitle('Customers')/items(" + customerId + ")";
- var requestHeaders = {
- "ACCEPT": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "If-Match": "*" // delete the item even if another user has updated it since we last fetched it
- }
- $.ajax({
- url: requestUri,
- type: "DELETE",
- contentType: "application/json;odata=verbose",
- headers: requestHeaders,
- success: function(){
- alert("Deleted customer");
- },
- error: function(){
- alert("Failed to delete customer");
- }
- });
- }
function onDeleteCustomer(customerId) {
var requestUri = _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('Customers')/items(" + customerId + ")";
var requestHeaders = {
"ACCEPT": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"If-Match": "*" // delete the item even if another user has updated it since we last fetched it
}
$.ajax({
url: requestUri,
type: "DELETE",
contentType: "application/json;odata=verbose",
headers: requestHeaders,
success: function(){
alert("Deleted customer");
},
error: function(){
alert("Failed to delete customer");
}
});
}
修改:
[javascript] view plaincopyprint?
- function onUpdateCustomer(customerId, firstName) {
- // first we need to fetch the eTag to ensure we have the most recent value for it
- var requestUri = _spPageContextInfo.webAbsoluteUrl +
- "/_api/Web/Lists/getByTitle('Customers')/items(" + customerId + ")";
- // execute AJAX request
- $.ajax({
- url: requestUri,
- type: "GET",
- headers: { "ACCEPT": "application/json;odata=verbose" },
- success: function(data){
- // got the eTag
- var etag = data.d.etag;
- var requestUri = _spPageContextInfo.webAbsoluteUrl +
- "/_api/Web/Lists/getByTitle('Customers')/items(" + customerId + ")";
- // set the MERGE verb so we only need to provide the update delta
- var requestHeaders = {
- "ACCEPT": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "X-HTTP-Method": "MERGE",
- "If-Match": etag
- }
- var customerData = {
- __metadata: { "type": "SP.Data.CustomersListItem" },
- FirstName: firstName
- };
- requestBody = JSON.stringify(customerData);
- $.ajax({
- url: requestUri,
- type: "POST",
- contentType: "application/json;odata=verbose",
- headers: requestHeaders,
- data: requestBody,
- success: function(data){
- alert("Updated customer");
- },
- error: function(data){
- alert("Failed to update customer");
- }
- });
- },
- error: function(data){
- alert("Failed to get customer eTag");
- }
- });
- }
function onUpdateCustomer(customerId, firstName) {
// first we need to fetch the eTag to ensure we have the most recent value for it
var requestUri = _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('Customers')/items(" + customerId + ")";
// execute AJAX request
$.ajax({
url: requestUri,
type: "GET",
headers: { "ACCEPT": "application/json;odata=verbose" },
success: function(data){
// got the eTag
var etag = data.d.etag;
var requestUri = _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('Customers')/items(" + customerId + ")";
// set the MERGE verb so we only need to provide the update delta
var requestHeaders = {
"ACCEPT": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": etag
}
var customerData = {
__metadata: { "type": "SP.Data.CustomersListItem" },
FirstName: firstName
};
requestBody = JSON.stringify(customerData);
$.ajax({
url: requestUri,
type: "POST",
contentType: "application/json;odata=verbose",
headers: requestHeaders,
data: requestBody,
success: function(data){
alert("Updated customer");
},
error: function(data){
alert("Failed to update customer");
}
});
},
error: function(data){
alert("Failed to get customer eTag");
}
});
}