Checkin / Checkout Sharepoint REST API

时间:2018-03-13 07:26:53

标签: javascript rest azure sharepoint microsoft-graph

这很简单:

  • 用于身份验证的OAuth协议

  • 列出documentLibrary中的文件并从列表中签出文件

  • 用javascript编写

到目前为止,我一直在为此奋斗几天而没有这样的运气。

结帐选项1 - 图表API

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

即使它是OneDrive API,它也应该与SharePoint doc.libraries一起运行 - RESt APIs部分:“在OneDrive,OneDrive for Business,SharePoint文档库和Office之间共享REST API小组,允许......

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

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

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

结帐选项2 - Sharepoint加载项

https://docs.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://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/authorization-code-oauth-flow-for-sharepoint-add-ins

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

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

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

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

1 个答案:

答案 0 :(得分:0)

在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/