0

is there a way to update SharePoint Library file properties from different site collection? I have file uploaded to https://sharepoint.com/sites/site1 & I need to update the file property from https://sharepoint.com/sites/site2. I looked in PNPjs library but get no success. If anyone can help me with this?

0

Sample code based on previous code.

protected UploadFile() {
    var files = (document.getElementById('uploadFile') as HTMLInputElement).files;

    var file = files[0];
    if (file != undefined || file != null) {
      let spOpts: ISPHttpClientOptions = {
        headers: {
          "Accept": "application/json",
          "Content-Type": "application/json"
        },
        body: file
      };

      var url = `https://xxx.sharepoint.com/sites/site/_api/Web/Lists/getByTitle('MyDoc')/RootFolder/Files/Add(url='${file.name}', overwrite=true)`

      this.props.context.spHttpClient.post(url, SPHttpClient.configurations.v1, spOpts).then((response: SPHttpClientResponse) => {
        console.log(`Status code: ${response.status}`);
        console.log(`Status text: ${response.statusText}`);
        response.json().then((responseJSON: JSON) => {
          console.log(responseJSON);

          let readOpts: ISPHttpClientOptions = {
            headers: {
              "Accept": "application/json"
            }
          }

          this.props.context.spHttpClient.get(responseJSON["@odata.id"] + "/ListItemAllFields", SPHttpClient.configurations.v1, readOpts).then((readResponse: SPHttpClientResponse) => {
            readResponse.json().then((metadataJSON: JSON) => {
              console.log(metadataJSON);
              var metadataType = metadataJSON["@odata.type"].replace('#', '');

              //SP.Data.MyDocItem metadata
              var metaDataBody: string = JSON.stringify({
                '__metadata': {
                  'type': metadataType
                },
                'Title': 'testupdate'
              });
              let updateOpt: ISPHttpClientOptions = {
                headers: {
                  "content-type": "application/json;odata=verbose",
                  "If-Match": "*",
                  "X-HTTP-Method": "MERGE",
                  'odata-version': '3.0'
                },
                body: metaDataBody
              }

              this.props.context.spHttpClient.post(responseJSON["@odata.id"] + "/ListItemAllFields", SPHttpClient.configurations.v1, updateOpt).then((reqResponse: any) => {
                console.log(reqResponse.status)
              })
            })
          })

        });
      });

    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.