本部落格已搬遷, 3秒後跳轉...

關於 SharePoint REST API | Laplace's Lab

關於 SharePoint REST API

這是一段關於沒有伺服器權限的開發者於 SharePoint 進行網站開發的恐怖故事。

這段故事實在是太可怕了,伺服器權限在資訊單位手裡,而完全不懂 SharePoint 也不會 .NET 的我只有使用者權限帳號(黑人問號.jpg),看著一點幫助都沒有的 training kit 文件,我必須想辦法把它寫成一個入口網站…

經過摸索研究最終想出了解決方法,我自製母版定義了導航列與頁腳,並利用內容編輯器這個 Web Part 在每個頁面「填入」我要的 HTML 內容,但不得不放棄內建的其他 Web Part,因為我根本無法完全控制那些頑固的東西,它們讓整個頁面佈局看起來相當糟糕!!!

但如此我便擁有整個頁面內容的控制權了,前端頁面佈局樣式就交給 Bootstrap 去搞定啦,而後端就只能依賴 SharePoint REST API 去處理 CRUD。

雖然很荒謬但我還是硬著頭皮上了,最後寫了個多功能的 SharePoint 企業內網 :

  • 公告
  • 行事曆(支援批次匯入事件)
  • 會議室預約
  • 工時追蹤填報(支援統計圖表)
  • 出勤狀況發佈(系統通知信)
  • 討論區
  • Smart Chat Bot(答覆公司系統與網站相關問題)

這任務至此告一段落了,紀錄一下 SharePoint REST API 如何使用 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var api_create = "https://server/site/_api/web/lists('{ your_list_guid }')/items",
api_read = "https://server/site/_api/web/lists('{ your_list_guid }')/items( { your_list_item_id } )",
api_update = "https://server/site/_api/web/lists('{ your_list_guid }')/items( { your_list_item_id } )",
api_delete = "https://server/_api/web/lists('{ your_list_guid }')/items( { your_list_item_id } )",
data ={
__metadata: { 'type': 'SP.Data.{ your_list_item_entity_type_fullname }' },
{ list_column_name } : { value }
};

function sp_create(api_create){
$.ajax({
url: api_create,
method: "POST",
data: JSON.stringify(data),
contentType: "application/json; odata=verbose",
headers:{
"Accept": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(res){
let data = res.d; // the data you create
// do something
},
error: function(error){
console.log(JSON.stringify(error));
}
});
}

function sp_read(api_read){
$.ajax({
url: api_read,
type: "GET",
headers: {
"accept":"application/json; odata=verbose"
},
success: function(res){
let data = res.d; // the data you read
// do something
},
error: function(error){
console.log(JSON.stringify(error));
}
});
}

function sp_update(api_update){
$.ajax({
url: api_update,
method: "POST",
data: JSON.stringify(data),
contentType: "application/json; odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*",
"X-Http-Method": "MERGE"
},
success: function(res){
let data = res.d; // the data you update
// do something
},
error: function(error){
console.log(JSON.stringify(error));
}
});
}

function sp_delete(api_delete){
$.ajax({
url: api,
method: 'DELETE',
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*"
},
success: function(res){
// do something
},
error: function(error){
console.log(JSON.stringify(error));
}
});
}

/* filter & select & order(asc / desc)
"https://server/_api/web/lists('{ your_list_guid }')/items?$filter={ list_column_name } eq { keyword }&$select=ID,AuthorId,Created,...&$orderby={ list_column_name } desc"
*/

特別留意 SharePoint REST API 返回的資料筆數預設值是100筆,這在我寫的會議室預約功能所依賴的資料庫筆數超過100之後,因為使用者反應預約完成的會議卻沒有顯示在頁面上而發現。此問題只要在 API 裡頭加上一個 “TOP”參數並指定返回的資料筆數即可解決,例如「TOP=5000」,而 API 能返回的最大值為5000筆。如果需要返回更多筆資料,印象中看過網路上相關討論,但我用在查詢會議室預約這樣有時效性的資料,估計5000筆已經綽綽有餘了XDD

0%