CRUD operation on Web object using REST API in SharePoint

As I've already discussed about REST API, so I'm assuming you all know the basics of REST API, for those who haven't read my last post please go through with "REST API for SharePoint".

In this blog post, we'll discuss about the operations performed on web object. Accessing properties of web using REST api is very simple.

Creating JavaScript Namespace
In this blog series we will use "RESTApiHelper" namespace to avoid conflict with other javascript libraries. Below is the code to create javascript namespace.

var RESTApiHelper = window.RESTApiHelper || {};

Get SPWeb Properties
Now we will create a property for Web in the namespace.


RESTApiHelper.Web = function (SiteUrl) {
    this.SiteUrl = SiteUrl;

For getting spweb properties we have to build the url as "http://siteurl/_api/web". Below is the jquery code to call the url and get the response in json format.

this.GetWeb = function (doSuccess, doError) {
        $.ajax({
            url: SiteUrl + "/_api/web",
            type: "GET",
            headers: { "accept": "application/json;odata=verbose" },
            success: doSuccess,
            error: doError
        });
    }


Below is the code to call the function.

var restWeb = new RESTApiHelper.Web(siteUrl);
restWeb.GetWeb(doSuccess,doError);

function doSuccess(data) {
        $('#result').text(JSON.stringify(data));

}

function doError(err) {
    alert(JSON.stringify(err));
}


Here I'm displaying the response in a div with id "results".

You can also fetch single property of web using the url syntax "http://siteurl/_api/web/<propertyname>":

this.GetWebProperty = function (propertyName, doSuccess, doError) {
        $.ajax({
            url: SiteUrl + "/_api/web/" + propertyName,
            type: "GET",
            headers: { "accept": "application/json;odata=verbose" },
            success: doSuccess,
            error: doError
        });
    }


Create Web using REST
Next we are going to create subsite using add method of web object in REST. Remember to update anything using REST you need REQUESTDIGEST value that can be easily get using jQuery when you are on the site. Below is the function to create subsite.

this.CreateSubsite = function (Title, Url, Description, WebTemplate, UseUniquePermissions, doSuccess, doError) {
        $.ajax({
            url: SiteUrl + "/_api/web/webinfos/add",
            type: "POST",
            data: JSON.stringify({
                'parameters': {
                    '__metadata': { 'type': 'SP.WebInfoCreationInformation' },
                    'Title': Title,
                    'Url': Url,
                    'Description': Description,
                    'Language': 1033,
                    'WebTemplate': WebTemplate,
                    'UseUniquePermissions': UseUniquePermissions
                }
            }),
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "content-length": 1000
            },
            success: doSuccess,
            error: doError
        });
    }


Below is the code to call the function:
var restWeb = new RESTApiHelper.Web(siteUrl);
restWeb.CreateSubsite('Demo Site', 'demosite', 'Demo Description', 'STS#0', false, doSuccess,doError);

Click here to get the full script. The script contains update and delete methods also. This is it for the web object. In the next post we will see CRUD operations involving sharepoint list. So stay tuned and enjoy, have fun with REST and JQuery !!!!

Comments

Post a Comment

Popular posts from this blog

Installation and Configuration of Office Online Server with SharePoint 2019

Workflow Manager 1.0- Installation, Configuration and Issues

SharePoint 2019 Upgrade Part 6: SharePoint Upgrade Issues