Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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.load(items);
ctx.executeQueryAsync(function() {
  var fileFound = (items.get_count() > 0);
}, 
function(sender, args) {
  console.log(args.get_message());
});

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.load(file);
   ctx.executeQueryAsync(function() {
      complete(true);
   }, 
   function(sender, args) {
     if (args.get_errorTypeName() === "System.IO.FileNotFoundException") {
         complete(false);
     }
     else {
       error(args);
     }  
   });
}

Usage

getFileExists('/project/shared documents/orders/order.docx',
  function(fileFound){
     console.log(fileFound);
  },
  function(error)
  {
     console.log(args.get_message());
  });
share|improve this answer

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.load(myFile);


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
    }
})
share|improve this answer

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

share|improve this answer

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.

    
Thanks for your response. –  user2598808 Aug 1 '14 at 13:43
    
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
1  
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  Karthik Jaganathan Aug 1 '14 at 14:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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