【问题标题】:InvalidClientQueryException when updating Document Library Metadata SPFx webpart更新文档库元数据 SPFx Web 部件时出现 InvalidClientQueryException
【发布时间】:2021-08-01 08:43:45
【问题描述】:

需要你的帮助! 我正在使用 REST API 将文档上传到 SPFx Webpart 中的文档库。我没有使用 PNP 和 React 进行文件上传和更新元数据。

上传文档后,我正在尝试通过获取 ID 并更新项目来更新元数据属性。

文件上传成功。能够获取添加的项目的 ID。在更新元数据属性时 - 它给了我 Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A type named 'SP.Data.模型无法解析 SubmitxyzfileListItem'。当模型可用时,每个类型名称都必须解析为有效类型。"}

下面是我正在使用的代码

//Code for File Upload
 private uploadFile(FinalName:string): Promise<any>{
 
      try {      
          return new Promise((resolve) => {      
                  var files = (<HTMLInputElement>document.getElementById('userFile')).files;
                  const spOpts: ISPHttpClientOptions = {      
                      body: files[0]      
                  };      
                  var url=`${this.context.pageContext.web.absoluteUrl}${this.properties.primarylist}/_api/Web/Lists/getByTitle('${this.properties.fileuploadlist}')/RootFolder/Files/Add(url='${FinalName}', overwrite=true)`
                  
                  const response = this.context.spHttpClient.post(url, SPHttpClient.configurations.v1, spOpts).then((response: SPHttpClientResponse) => {        
    
                        response.json().then(async (responseJSON: any) => {   
                        console.log("File Uploaded");       
                        var uniqueGuid = await responseJSON.UniqueId;   
                        this.updateDocLibMetadata(uniqueGuid);
                        console.log("uniqueGuid"+uniqueGuid); 
                        resolve(uniqueGuid);          
                      });        
                    });    
          });      
      } catch (error) {      
          console.log("Error in uploadFile " + error);      
      }
  }
\\Code for updating Document Library Metadata
  private updateDocLibMetadata(uniqueGuid:string) {

    var geturl=`${this.context.pageContext.web.absoluteUrl}${this.properties.primarylist}/_api/Web/Lists/getByTitle('${this.properties.fileuploadlist}')/GetItemByUniqueId(guid'${uniqueGuid}')?$select=id,name,UniqueId,Title,File/ServerRelativeUrl&$expand=File`;

    this.context.spHttpClient.get(geturl,SPHttpClient.configurations.v1)
    .then((response: SPHttpClientResponse) => {  
      response.json().then((responseJSON: any) => {  
        console.log("Id in updateDocLibMetadata :"+responseJSON["Id"]);
        var itemId=responseJSON["Id"];
        
        var posturl = this.context.pageContext.web.absoluteUrl +this.properties.primarylist+ `/_api/web/lists/GetByTitle('${this.properties.fileuploadlist}')/items(${itemId})`;
        var payload = JSON.stringify({
          "__metadata": {
            'type': this.getListItemType(this.properties.fileuploadlist)
          },
          "Id_SubmitxyzQuestionId": this._requiredListProps.Id
        });
      
        var option = {
          headers: {
            'IF-MATCH': '*',
            'Content-type': 'application/json;odata=verbose',
            "accept": "application/json;odata=verbose",
            "odata-version":"3.0",
            'X-HTTP-Method': 'PATCH'
          },
          body: payload
        };
      
       //THIS FINAL CALL IS GIVING ERROR
       return this.context.spHttpClient.post(posturl, SPHttpClient.configurations.v1, option).then((UpdateResponse: SPHttpClientResponse) => {
          console.log(UpdateResponse.status + ':' + response.ok);
          UpdateResponse.text().then(function (text) {
            // do something with the text response 
            console.log("text:"+text);
          });
        })
        .catch(reject => console.error('Error :', reject));
      });  
    });
  }

我确实看过 PowerAutomate 的一篇文章,其中说解决方案是检查文件然后更新属性。链接到 PowerAutomate 问题 - https://powerusers.microsoft.com/t5/Building-Flows/Update-list-item-via-SharePoint-REST-API/m-p/534538#M69186

对于 SPFx webart 也是 - 我必须使用相同的方法吗?

非常感谢任何帮助。

谢谢。

【问题讨论】:

    标签: sharepoint-online rest spfx


    【解决方案1】:

    找到以上问题的答案! 更改了请求的有效负载和标头,并像魅力一样工作。

    在查询中指定的 updateDocLibMetadata 方法中更改为以下代码。

     var payload = JSON.stringify({         
              "Id_SubmitxyzQuestionId": this._requiredListProps.Id
            });
          
            var option = {
              headers: {
                'Accept': 'application/json;odata=nometadata',  
                'Content-type': 'application/json;odata=nometadata',  
                'odata-version': '',  
                'IF-MATCH': '*',  
                'X-HTTP-Method': 'MERGE'  
              },
              body: payload
            };
    

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 2021-04-14
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2013-02-28
      • 2011-10-23
      相关资源
      最近更新 更多