Share via


SharePoint 2010: A Complete list of SPFile Operations using ECMA Script

Introduction

This article demonstrates how to use various methods/properties associated with SPFile object using client object model  JavaScript variation (ECMA Script).
The examples shows how to do file operations in document library using ECMA script. 

We have a document library called “Shared Documents” and its having one file which is having id =1. We will do all operation considering this file only.

So first we will try to checkout file using ECMA

File Checkout:

 Code Sample

  <script language="ecmascript" type="text/ecmascript"> 
  ExecuteOrDelayUntilScriptLoaded(CheckOutFile, "sp.js");
 ​var list;
  var item;
   var file;           function CheckOutFile() {
            var clientContext = SP.ClientContext.get_current();            
                var webSite = clientContext.get_web();
                this.list = webSite.get_lists().getByTitle("Shared Documents");
                this.item = list.getItemById(1);
                this.file = this.item.get_file();
                this.file.checkOut();
                clientContext.load(this.file)
                clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));            
        }
        function OnLoadSuccess(sender, args) {
            alert(this.file.get_title() + " File checked out successfully");
        }
        function OnLoadFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }</script>

File Check In

this.file = this.item.get_file();
this.file.checkIn(“Comments”,1);         

Enumeration for CheckIn type available @
http://msdn.microsoft.com/en-us/library/ee553393.aspx

CopyTo

 this.file = this.item.get_file();
 this.file.copyTo("/Shared%20Documents/1/TestFile.htm","1");

 Method parameter
 copyTo(strNewUrl , bOverWrite )
 strNewUrl : It must be a URL of relative or absolute form.
 bOverWrite:  Specifies whether a file with the same name is overwritten.

MoveTo

 this.file = this.item.get_file();
 this.file.moveTo("/Shared%20Documents/1/ TestFile.htm",1);

This operation moves file with complete version information/history.

Publish File

 this.file = this.item.get_file();
 this.file.publish("Published Major Version");

 

Delete File

 this.file = this.item.get_file();
 this.file.deleteObject();

Get File Path

 this.file = this.item.get_file();
 this.file.get_path(); 

Get document Author Name

 this.file = this.item.get_file();
 this.file.get_author();
 clientContext.load(this.file.get_author());

It provides author login name,title,email  etc

Get document Modified By Name

 this.file = this.item.get_file();
  this.file.get_modifiedBy();
 clientContext.load(this.file.get_modifiedBy());

It provides login name,title,email and other details of the user who has modified the file.

Here is the complete list of methods provided by SharePoint JavaScript client object model API for SPFile object.