前言

  最近,接到一个需求,用户希望通过自定义代码,获取SharePoint文档库中文档的版本,好的,安排!

  正文

  1.获取版本的REST API,通过Get方法进行调用,如下图:

1 $.ajax
2 ({
3 url:"https://aiacom.sharepoint.com/sites/TEST/_api/web/GetFileByServerRelativeUrl('/sites/TEST/Shared Documents/TestForVersion.docx')/versions",
4 type: "GET",
5 headers:
6 {
7 "Accept": "application/json;odata=verbose"
8 },
9 success: function (data, status, xhr) {
10 console.log(data);
11 var dataresults = data.d.results;
12 for (var i = 0; i < dataresults.length; i++) {
13 console.log("Version: " + dataresults[i]["VersionLabel"] + ", Url: " + dataresults[i]["Url"]);
14 }
15 },
16 error: function (xhr, status, error) {
17 console.log("Failed");
18 }
19 });

  2.恢复文档的对应版本为主版本,如下图:

1 $.ajax
2 ({
3 url:"https://aiacom.sharepoint.com/sites/TEST/_api/web/GetFileByServerRelativeUrl('/sites/TEST/Shared Documents/TestForVersion.docx')/versions/restorebylabel(versionlabel='1.0')",
4 type: "POST",
5 headers:
6 {
7 "Accept": "application/json;odata=verbose",
8 "Content-Type": "application/json;odata=verbose",
9 "IF-MATCH": "*",
10 "X-HTTP-Method": "PATCH",
11 "X-RequestDigest": $("#__REQUESTDIGEST").val()
12 },
13 success: function (data, status, xhr) {
14 console.log("success");
15 },
16 error: function (xhr, status, error) {
17 console.log("Failed");
18 }
19 });
20 }

  3.调用API返回的结果

SharePoint Online 通过API获取版本_json