I am using javascript sharepoint 2013.

I want to know how to check if a file exists in a folder, before i upload.

Here's my code

// Folder Name - folder into which I need to upload
   var FolderName = "Images";
// Root Folder Name
   var rootfolder = '/Image Library/' + FolderName;
// Get the context and Web
   var clientContext = new SP.ClientContext.get_current();
   var oWebsite = clientContext.get_web();
// These are the files which I need to upload into the "Images" folder of the document library
    var files = document.getElementById(UploadFiles).files;

Now I need to check if the file already exists in the "Images" folder. If it exists , i need to throw a message , "Äre you sure to overwrite?"

I am pretty new to Sharepoint, please suggest if I need to write a CAML query or some other way to achieve this.

Please suggest. Thanks in advance


You could consider the following approaches to determine whether file exists or not.

Query based

You could construct CAML query to find list item by its Url as demonstrated below:

var listTitle = 'Documents';
var fileUrl  = '/project/shared documents/orders/order.docx';

var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var qry = new SP.CamlQuery();
qry.set_viewXml('<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FileRef"/><Value Type="Url">' + fileUrl + '</Value></Eq></Where></Query></View>');
var items = list.getItems(qry);
ctx.executeQueryAsync(function() {
  var fileFound = (items.get_count() > 0);
function(sender, args) {

SP.Web.getFileByServerRelativeUrl Method

Use SP.Web.getFileByServerRelativeUrl Method to return the file object located at the specified server-relative URL.

If file does not exists the exception System.IO.FileNotFoundException will be thrown:

function getFileExists(fileUrl,complete,error)
   var ctx = SP.ClientContext.get_current();
   var file = ctx.get_web().getFileByServerRelativeUrl(fileUrl);
   ctx.executeQueryAsync(function() {
   function(sender, args) {
     if (args.get_errorTypeName() === "System.IO.FileNotFoundException") {
     else {


getFileExists('/project/shared documents/orders/order.docx',

Just try getting the file and use the success/error callback in executeQueryAsync...

var myCtx = SP.ClientContext.get_current();
var myFile = myCtx.get_web().getFileByServerRelativeUrl(/<site>/<lib>/<filename w/ extension>);

myCtx.executeQueryAsync(function() {
    // success handler, if you made it here, the file exists
}, function(sender, args) {
    // error handler, if you made it here something else went wrong...
    if (args.get_message() == "File Not Found.") {
        //if you get here, the file didn't exist

Here are some option:

Uncheck overwrite by default http://planet-sharepoint.blogspot.co.uk/2013/10/sharepoint-2010-set-overwrite-existing.html

jQuery option http://vegardstromsoy.blogspot.se/2011/05/jquery-to-override-sharepoint-ootb.html

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • I just want to check if a specific file already exists. Using Javascript code. Please suggest. Thanks again! – user2598808 Aug 1 '14 at 13:44

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.