SOLVED

SharePoint file upload using API

%3CLINGO-SUB%20id%3D%22lingo-sub-382595%22%20slang%3D%22en-US%22%3ESharePoint%20file%20upload%20using%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-382595%22%20slang%3D%22en-US%22%3E%3CP%3EIs%20there%20any%20API%20available%20to%20upload%20file%20in%20SharePoint%3F%20Sample%20code%20will%20be%20helpful.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20we%20upload%20same%20file%20using%20API%20will%20it%20replace%20the%20old%20file%20or%20create%20a%20new%20version%20of%20the%20file%3F%20Please%20advise.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-382595%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPIs%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-382687%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20file%20upload%20using%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-382687%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F261701%22%20target%3D%22_blank%22%3E%40Pulak2409%3C%2FA%3E%26nbsp%3BMultiple%20APIs%20to%20do%20this.%3CBR%20%2F%3E%3CBR%20%2F%3EREST%20API%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsp-add-ins%2Fworking-with-folders-and-files-with-rest%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsp-add-ins%2Fworking-with-folders-and-files-with-rest%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EYou%20can%20also%20use%20CSOM%20using%20FileCreationInformation%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fupload-large-files-sample-app-for-sharepoint%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fupload-large-files-sample-app-for-sharepoint%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EYou%20have%20the%20option%20to%20overwrite%20files%20in%20both%20scenarios%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-382637%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20file%20upload%20using%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-382637%22%20slang%3D%22en-US%22%3E%3CP%3EHey%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F261701%22%20target%3D%22_blank%22%3E%40Pulak2409%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20recommend%20you%20use%20PnPJs%2C%20since%20it%20makes%20handling%20REST%20calls%20a%20lot%20easier%20for%20you.%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-JS-Core%2Fwiki%2FWorking-With%3A-Files%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EThis%3C%2FA%3E%20page%20shows%20how%20to%20work%20with%20files%20using%20it.%20A%20good%20point%20is%20that%2C%20after%20uploading%20the%20file%2C%20you%20may%20want%20to%20update%20it%20to%20set%20the%20Title%20of%20the%20Item%20you%20just%20created.%20Here%20is%20a%20piece%20of%20my%20code%2C%20which%20uploads%20from%20an%20input%20button.%20Any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2FDIV%3E%3CDIV%3E%3CSPAN%3Eitem%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Eupdate%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3CSPAN%3Efile%3C%2FSPAN%3E%3CSPAN%3E).%3C%2FSPAN%3E%3CSPAN%3Ethen%3C%2FSPAN%3E%3CSPAN%3E(()%20%3C%2FSPAN%3E%3CSPAN%3E%3D%26gt%3B%3C%2FSPAN%3E%3CSPAN%3E%20%7B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3Ereturn%20true%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EEdit%3A%20Sorry%20for%20the%20indenting.%20They%20won't%20let%20me%20fix%20it.%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
Contributor

Is there any API available to upload file in SharePoint? Sample code will be helpful.

 

If we upload same file using API will it replace the old file or create a new version of the file? Please advise.

 

2 Replies

Hey @Pulak2409 ,

 

I recommend you use PnPJs, since it makes handling REST calls a lot easier for you. This page shows how to work with files using it. A good point is that, after uploading the file, you may want to update it to set the Title of the Item you just created. Here is a piece of my code, which uploads from an input button. Any problem, let me know!

 

$("input").change(e => {
e.preventDefault();
let input: HTMLInputElement = <HTMLInputElement>jQuery(e.target)[0];
let file = input.files[0];
if (!file || file.size == 0)
alert("Arquivo Inválido");
else{
sp.web.lists.getByTitle("Documents").rootFolder.files.add(file.name, file, true).then(f => {
f.file.getItem().then(item => {
let file = new Object();
file.Title = "MyTitle";
item.update(file).then(() => {
return true;
});
});
});
}
});
 
Edit: Sorry for the indenting. They won't let me fix it.
best response confirmed by Juan Carlos González Martín (MVP)
Solution

@Pulak2409 Multiple APIs to do this.

REST API https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest

You can also use CSOM using FileCreationInformation https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/upload-large-files-sample-app-for-...

You have the option to overwrite files in both scenarios