cross site collection file property update in SPFx
-
08-02-2021 - |
题
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?
解决方案
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)
})
})
})
});
});
}
}