CRUD operation on Files using REST
In the previous post we have seen CRUD operation on SharePoint ListItems. In
this post we are going to discuss about the operations on files in SharePoint Library
using rest api.
RESTApiHelper.Files = function (SiteUrl,ListName,FolderUrl) {
this.SiteUrl = SiteUrl;
this.ListName = ListName;
this.FolderUrl = FolderUrl;
Get All Files in Folder
this.GetAllFiles = function (doSuccess, doError) {
jQuery.ajax({
url: this.SiteUrl + "/_api/web/getfolderbyserverrelativeurl('" + this.FolderUrl + "')/Files",
type: "GET",
headers: {
"accept": "application/json;odata=verbose"
},
success: doSuccess,
error: doError
});
}
Get File in folder by FileName
this.GetFileMetadataByUrl = function (FileName, doSuccess, doError) {
jQuery.ajax({
url: this.SiteUrl + "/_api/web/getfolderbyserverrelativeurl('" + this.FolderUrl + "')/Files('" + FileName + "')",
type: "GET",
headers: {
"accept": "application/json;odata=verbose"
},
success: doSuccess,
error: doError
});
}
Get File Content
this.GetFileContentByUrl = function (FileName, doSuccess, doError) {
jQuery.ajax({
url: this.SiteUrl + "/_api/web/getfolderbyserverrelativeurl('" + this.FolderUrl + "')/Files('" + FileName + "')/$value",
type: "GET",
binaryStringResponseBody: true,
headers: {
"accept": "application/json;odata=verbose"
},
success: doSuccess,
error: doError
});
}
Get File by Server relative url
this.GetFileByServerRelativeUrl = function (FileUrl, doSuccess, doError) {
jQuery.ajax({
url: this.SiteUrl + "/_api/web/getfilebyserverrelativeurl('" + FileUrl + "')",
type: "GET",
headers: {
"accept": "application/json;odata=verbose"
},
success: doSuccess,
error: doError
});
}
Get File Property By Server relative url
this.GetFilePropertyByServerRelativeUrl = function (FileUrl, PropertyName, doSuccess, doError) {
jQuery.ajax({
url: this.SiteUrl + "/_api/web/getfilebyserverrelativeurl('" + FileUrl + "')/" + PropertyName,
type: "GET",
headers: {
"accept": "application/json;odata=verbose"
},
success: doSuccess,
error: doError
});
}
Update File Content by Server Relative Url
this.UpdateFileContentByServerRelativeUrl = function (FileUrl, FileContent) {
jQuery.ajax({
url: this.SiteUrl + "/_api/web/getfilebyserverrelativeurl('" + FileUrl + "')/$value",
type: "POST",
body:FileContent,
headers: {
"accept" : "application/json;odata=verbose",
"X-HTTP-Method" : "PUT"
},
success: doSuccess,
error: doError
});
}
Upload File into Folder
this.AddFile = function (arrayBuffer, fileUrl, doSuccess, doError) {
var fileData = '';
var byteArray = new Uint8Array(arrayBuffer);
for (var i = 0; i < byteArray.byteLength; i++) {
fileData += String.fromCharCode(byteArray[i]);
}
// Send the request.
jQuery.ajax({
url: this.SiteUrl + "/_api/web/getfolderbyserverrelativeurl('" + this.FolderUrl + "')/Files/add(overwrite=true, url='" + fileUrl + "')",
method: "POST",
body: fileData,
binaryStringRequestBody: true,
headers: { "accept": "application/json;odata=verbose" },
success: doSuccess,
error: doError
});
}
Delete File using Server relative url
this.DeleteFileByServerRelativeUrl = function (FileUrl, doSuccess, doError) {
jQuery.ajax({
url: this.SiteUrl + "/_api/web/getfilebyserverrelativeurl('" + FileUrl + "')",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
"X-HTTP-Method": "DELETE"
},
success: doSuccess,
error: doError
});
}
Click here to get the full script. This is it for the file object in SharePoint. In the next post we will work on folders. So stay tuned and enjoy, have fun with REST and JQuery !!!!
This comment has been removed by a blog administrator.
ReplyDelete