gpt4 book ai didi

javascript - checkin / checkout Sharepoint REST API

转载 作者:行者123 更新时间:2023-12-03 00:29:22 27 4
gpt4 key购买 nike

就这么简单:

  • 用于身份验证的 OAuth 协议(protocol)

  • 列出 documentLibrary 中的文件并从列表中 check out 文件

  • 用 JavaScript 编写

我已经为此苦苦挣扎了几天,但到目前为止还没有这样的运气。

检查选项 1 - 图形 API:

https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_checkout

即使它是 OneDrive API,它也应该与 SharePoint doc.libraries 一起运行 - RESt API 部分:“REST API 在 OneDrive、OneDrive for Business、SharePoint 文档之间共享图书馆和办公室组,以允许...

结果呢?在这里查看:Sharepoint `Unsupported segment type` when checkin/chekout file

好消息,OAuth 的作用就像一个魅力 - 我从 https://apps.dev.microsoft.com/ 获得了客户端 ID ,身份验证端点:

https://login.microsoftonline.com/common/oauth2/v2.0/authorizehttps://login.microsoftonline.com/common/oauth2/v2.0/token

结帐选项 2 - Sharepoint 插件

https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest

url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name')/CheckOut(),
method: POST
headers:
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value

这个工作完美,但在这种情况下,OAuth 是问题......

这个链接很有希望: https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/authorization-code-oauth-flow-for-sharepoint-add-ins

但是,在此过程中,涉及 Microsoft Azure 访问控制服务 (ACS),该服务(根据此链接 https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-acs-migration)即将关闭。

解决方案似乎是切换到 Azure 应用程序( https://portal.azure.com -> Azure Active Directory -> 应用程序注册)。无论如何,使用这些设置的访问 token 与 Sharepoint API 所需的访问 token 不兼容,例如:

https://mindjet2.sharepoint.com/_api/contextinfo抛出异常'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException'

我的图表 API 做错了什么?使用 OAuth 验证 Sharepoint API 的正确方法是什么?

最佳答案

在SharePoint加载项中,我们可以使用跨域库来实现。

检查下面的代码:

'use strict';  
var hostweburl;
var appweburl;

// This code runs when the DOM is ready and creates a context object which is
// needed to use the SharePoint object model
$(document).ready(function () {

//Get the URI decoded URLs.
hostweburl =
decodeURIComponent(
getQueryStringParameter("SPHostUrl"));
appweburl =
decodeURIComponent(
getQueryStringParameter("SPAppWebUrl"));
// Resources are in URLs in the form:
// web_url/_layouts/15/resource
var scriptbase = hostweburl + "/_layouts/15/";

// Load the js file and continue to load the page with information about the list top level folders.
// SP.RequestExecutor.js to make cross-domain requests

// Load the js files and continue to the successHandler
$.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
});

// Function to prepare and issue the request to get
// SharePoint data
function execCrossDomainRequest() {
// executor: The RequestExecutor object
// Initialize the RequestExecutor with the app web URL.
var executor = new SP.RequestExecutor(appweburl);

var metatdata = "{ '__metadata': { 'type': 'SP.Data.TestListListItem' }, 'Title': 'changelistitemtitle'}";

// Issue the call against the app web.
// To get the title using REST we can hit the endpoint:
// appweburl/_api/web/lists/getbytitle('listname')/items
// The response formats the data in the JSON format.
// The functions successHandler and errorHandler attend the
// sucess and error events respectively.
executor.executeAsync({
url:appweburl + "/_api/SP.AppContextSite(@target)/web/GetFileByServerRelativeUrl('/Shared Documents/a.txt')/CheckOut()?@target='" +
hostweburl + "'",
method: "POST",
body: metatdata ,
headers: { "Accept": "application/json; odata=verbose", "content-type": "application/json; odata=verbose", "content-length": metatdata.length, "X-HTTP-Method": "MERGE", "IF-MATCH": "*" },
success: function (data) {
alert("success: " + JSON.stringify(data));
},
error: function (err) {
alert("error: " + JSON.stringify(err));
}
});
}
// This function prepares, loads, and then executes a SharePoint query to get
// the current users information
//Utilities

// Retrieve a query string value.
// For production purposes you may want to use
// a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
var params =document.URL.split("?")[1].split("&");
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}

引用:https://www.c-sharpcorner.com/UploadFile/472cc1/check-out-files-in-sharepoint-library-2013-using-rest-api/

关于javascript - checkin / checkout Sharepoint REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49250476/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com