Need your help! Am Uploading a document to the document library in SPFx webpart using REST API. I am not using PNP and React for file upload and updating metadata.
Post uploading the document, I am trying to update the metadata properties by getting the ID and updating the item.
File upload is done successfully. Able to get the ID of the Item added. While updating the metadata property - it is giving me Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A type named 'SP.Data.SubmitxyzfileListItem' could not be resolved by the model. When a model is available, each type name must resolve to a valid type."}
Below is the code I am using
//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));
});
});
}
I did come through one post of PowerAutomate which says the solution was to check out the file then update the properties. Link to the PowerAutomate issue - https://powerusers.microsoft.com/t5/Building-Flows/Update-list-item-via-SharePoint-REST-API/m-p/534538#M69186
For SPFx webart also - would I have to use same approach ?
Any help is much appreciated.
Thanks.