【问题标题】:How to update and create item using same method using sharepoint rest api如何使用 sharepoint rest api 使用相同的方法更新和创建项目
【发布时间】:2020-01-10 02:28:31
【问题描述】:

我有一个要求,如果列表项已经存在,我需要更新它,如果该项目不存在,我需要创建一个新的。当我从自定义表单获取数据以更新项目时,我需要从单一方法管理的一切。有没有办法在sharepoint online rest api中做到这一点? 我正在使用以下方法来更新项目

public static UpdateSaveSectionC(formData: any,id:any): PromiseLike<string> {

    // Return a promise
    const siteURL= "https://abc.sharepoint.com/sites/process";
    return new Promise((resolve, reject) => {
        for (var i = 0; i < formData.Category.length; i++) {

        const restUrl = siteURL + `/_api/web/lists/getbytitle('List')/items(${id[i]})`;
        const headers = { 'accept': 'application/json;odata=verbose', 'content-Type': 'application/json;odata=verbose','X-HTTP-Method': 'MERGE','IF-MATCH': '*'};
        const listTitle = "List";
        const data = {
                        '__metadata': { 'type': 'SP.Data.' + listTitle + 'ListItem','results':[] },
                        Category: formData.Category[i],
                        Recommendationsuggestion: formData.Recommendationsuggestion[i],

                    }  

            Helper.executeJson(restUrl, "POST", headers, JSON.stringify($.extend(true,{}, data)))
            .then((response) => {
                // Resolve the request

                resolve("success");
            }).catch( (e) => {
                if(e.responseJSON.error.message.value.indexOf("The request ETag value") != -1)
                {
                    resolve("Please refresh the page, and resubmit your changes");
                }

    });

}

`

【问题讨论】:

标签: rest sharepoint sharepoint-online office365api sharepoint-rest-api


【解决方案1】:

使用rest api过滤列表,根据唯一键确认item不存在,

/_api/web/lists/getbytitle('list title')/items?$filter=UniqueField eq 'value'

根据退回的商品数知道结果。

【讨论】:

    【解决方案2】:

    Sharepoint rest api 中没有针对列表的 Upsert 模式。

    选项1:推荐的方法是在用户填写数据时根据填写的条目给出自动建议。因此,在提交时您会知道有问题的项目是否有id 以便插入或更新。

    选项2:在您的提交方法中,您可以快速查找项目是否存在或不链接它以更新或插入方法

    注意插入和更新有两个不同的端点。

    【讨论】:

      【解决方案3】:

      这是对@Lee_MSFT 帖子的补充,所以请先阅读他的帖子。

      您查询的方式也可以简化。我建议您使用 jQuery Promise 或 PNP.js,因为它会让您的生活更轻松。 jQuery 示例:

      function getItem(name) {
              var query = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('list title')/items?$filter=UniqueField eq " +name;
              return $.ajax({
                  url: query,
                  method: "GET",
                  headers: {
                      Accept: "application/json;odata=verbose"
                  }
              });
          }
      
      getItem("hi.docx").done(function (result) { if(!result){uploadFile()}};
      

      如果您可以研究,PNP.js 是 IMO 更好的选择。

      【讨论】:

        【解决方案4】:

        正如@joyBlanks 所说,OOTB 在 Sharepoint API 中没有这样的 UpdateOrInsert 功能。 但是使用SPOHelper Utility可以最小化代码来实现功能。 SPOHelper 是用于 Sharepoint Online 的轻量级 REST 实用程序。

        var reqUrl="https://tenant.sharepoint.com/sites/ABCSite/_api/Lists/getbytitle('SPO List')/items";
        var id=2;
        var formdata={Title :"POST test update",Number:1234};
        if(id){
        var tempReqUrl=`${reqUrl}(${id})`
        var result= await SPUpdate({url:tempReqUrl,payload:formdata})
        // If request fails
        if(result && result.ok){
        console.log("Request Success with Update",result)
        }else{
         result =await SPPost({url:reqUrl,payload:formdata});
        console.log("Request Success with Insert",result)
        
        }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2015-01-03
          • 2019-06-06
          • 1970-01-01
          • 2015-10-23
          • 1970-01-01
          • 1970-01-01
          • 2023-04-06
          • 2021-08-08
          • 2021-03-05
          相关资源
          最近更新 更多