Monday, March 2, 2015

CRUD operation on Folders using REST

In the previous post we have seen CRUD operation on SharePoint Files. In this post we are going to discuss about the operations on folders in SharePoint Library using rest api.

Starting with the creation of Folder class in our"RESTApiHelper" namespace.

RESTApiHelper.Folders = function (SiteUrl,ListName) {
    this.SiteUrl = SiteUrl;
    this.ListName = ListName;


Get Folder By Url
   
    this.GetFolderByUrl = function (FolderUrl, doSuccess, doError) {
        jQuery.ajax({
            url: SiteUrl + "/_api/web/getfolderbyserverrelativeurl('" + FolderUrl + "')",
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }


Get Folder Property By Url

    this.GetFolderPropertiesByUrl = function (FolderUrl,PropertyName, doSuccess, doError) {
        jQuery.ajax({
            url: SiteUrl + "/_api/web/getfolderbyserverrelativeurl('" + FolderUrl + "')/"+PropertyName,
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }


Update Folder By Url

    this.UpdateFolderByUrl = function (FolderUrl, PropertyName, PropertyValue, doSuccess, doError) {
        jQuery.ajax({
            url: SiteUrl + "/_api/web/getfolderbyserverrelativeurl('" + FolderUrl + "')",
            type: "POST",
            data: JSON.stringify({ '__metadata': { 'type': 'SP.Folder' }, PropertyName: PropertyValue }),
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json; odata=verbose",
                "X-HTTP-Method": "MERGE"
            },
            success: doSuccess,
            error: doError
        });
    }


Delete Folder By Url

    this.DeleteFolderByUrl = function (FolderUrl, doSuccess, doError) {
        jQuery.ajax({
            url: SiteUrl + "/_api/web/getfolderbyserverrelativeurl('" + FolderUrl + "')",
            type: "POST",
            headers: {
                "X-HTTP-Method": "DELETE"
            },
            success: doSuccess,
            error: doError
        });
    }


Create Folder By passing data into the request

    this.CreateFolder = function (parentFolderUrl,NewFolderName,doSuccess, doError) {
        jQuery.ajax({
            url: SiteUrl + "/_api/web/getfolderbyserverrelativeurl('" + parentFolderUrl + "')/folders",
            type: "POST",
            data: JSON.stringify({ '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': NewFolderName }),
            headers: {
                "accept": "application/json; odata=verbose",
                "content-type": "application/json; odata=verbose"
            },
            success: doSuccess,
            error: doError
        });
    }


Create Folder By using Add method

    this.CreateFolderByAdd = function (NewFolderRelativeURL, doSuccess, doError) {
        jQuery.ajax({
            url: SiteUrl + "/_api/web/folders/add('"+NewFolderRelativeURL+"')",
            type: "POST",
            headers: {
                "accept": "application/json; odata=verbose",
                "content-type": "application/json; odata=verbose"
            },
            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 !!!!