首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Checkin/Checkout Sharepoint REST

Checkin/Checkout Sharepoint REST
EN

Stack Overflow用户
提问于 2018-03-13 07:26:52
回答 1查看 4.5K关注 0票数 2

太简单了:

  • 用于认证的OAuth协议
  • 从documentLibrary中列出文件,并从列表中签出文件
  • 用javascript写

几天来,我一直在为这件事而挣扎,但到目前为止还没有这样的运气。

签出选项1-图API

checkout

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

结果是什么呢?看看这里: 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://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest

代码语言:javascript
复制
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访问控制服务(ACS),根据该链接https://learn.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的正确方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2018-03-13 09:37:27

在SharePoint外接程序中,我们可以使用跨域库来实现它.

检查下面的代码:

代码语言:javascript
复制
'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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49250476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档