SharePoint 2016 general availability was announced in the Future Of SharePoint conference in May 2016. The series that discusses the installation of SharePoint 2016 in Azure can be found at C# Corner from the below links.
In this article, we will see how to interact with Sites in SharePoint, using REST API. As an initial prerequisite, ensure that the user, who is running the below scripts, has Site Collection administrator privileges.
We will use REST to connect to the SharePoint for performing the Site operations.The Scope of the article involves the following operations, using REST:
- Create a new SharePoint 2016 Site
- Delete an existing SharePoint 2016 Site
Create a new SharePoint Web
We can create the SharePoint site by issuing a POST AJAX request. The REST URL endpoint used for the operation is:
/_api/web/webinfos/add
We will be creating a key value pair of information which will be used to create the Sharepoint site. The site creation information property is as shown below. It will be send as json in the ‘data’ attribute of the ajax rest call.
- {
- 'parameters': {
- '__metadata': {
- 'type': 'SP.WebInfoCreationInformation'
- },
- 'Url': 'NewSubWeb',
- 'Title': 'SubWeb Created using REST',
- 'Description': 'REST created web',
- 'Language': 1033,
- 'WebTemplate': 'sts',
- 'UseUniquePermissions': false
- }
- }
Within the _metadata attribute we have to specify the value for ‘type’ which will specify what object is being created. In our case it is site, hence we will be specifying ‘SP.WebInfoCreationInformation’. Along with this we will be specifying other web properties as a key value pair. The header section will look like,
- headers: {
- "accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "content-Type": "application/json;odata=verbose"
- },
Here accept attribute specifies the data type for the return value and content-type defines the data type for the data sent to the server. In POST request we have to send the X-RequestDigest value along with the request for form validation without which we will get validation error. To fullfill this we will be assigning the $("#__REQUESTDIGEST").val() which sets the value of the form digest control present within the page to X-RequestDigest key.
Output: The console output shows a custom successful site creation message as shown below:
We can navigate to the site to see the newly created web.
Full Code
The full code for creating the site using REST is as shown below,
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function() {
- var newSiteUrl = "/_api/web/webinfos/add";
- createSite(newSiteUrl)
- });
-
- function createSite(newSiteUrl) {
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl + newSiteUrl,
- type: "POST",
- data: JSON.stringify({
- 'parameters': {
- '__metadata': {
- 'type': 'SP.WebInfoCreationInformation'
- },
- 'Url': 'NewSubWeb',
- 'Title': 'SubWeb Created using REST',
- 'Description': 'REST created web',
- 'Language': 1033,
- 'WebTemplate': 'sts',
- 'UseUniquePermissions': false
- }
- }),
- headers: {
- "accept": "application/json; odata=verbose",
- "content-type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val()
- },
- success: function(data) {
- console.log(data);
- console.log("The site has been created successfully.");
- },
- error: function(error) {
- alert(JSON.stringify(error));
- }
- });
- }
- </script>
Delete exiting web
In order to delete the SharePoint site we can issue a DELETE AJAX request using the below REST endpoint.
/_api/Web
In the header section we can specify the ‘accept’ and ‘X-RequestDigest’ attribute which specifies the return data type and the form digest value respectively. The “IF-Match” attribute is used to check concurrency of the site to ensure that the site that is being deleted is really the one that we intent to delete. We can either specify “*” which will blindly skip the concurrency check else we can specify the e-tag value (which can be obtained by issuing a GET request).
Output: On running the script for site deletion, a success message has come up in the console as shown below,
Full Code: The full code for deleting the site using REST is as shown below,
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function() {
- var existingSiteUrl = "/NewSubWeb/_api/Web";
- deleteSite(existingSiteUrl)
- });
-
- function deleteSite(existingSiteUrl) {
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl + existingSiteUrl,
- type: "DELETE",
- headers: {
- "accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "IF-MATCH": "*"
- },
- success: function(data) {
- console.log(data);
- console.log("The web has been successfully deleted.");
- },
- error: function(error) {
- alert(JSON.stringify(error));
- }
- });
- }
- </script>
Let’s see how to implement the above scripts in SharePoint. Below steps will demo how to work with the script for deleting the site. Similarly other scripts can also be tested. Save the script for deleting the site as a text file and upload it to Site Assets library.
SharePoint Implementation
- Go to the edit settings of the SharePoint page and click on Web part from the Insert tab
- Add Content Editor Web part,
- Click on Edit Web art from Content Edit Web part. Assign the URL of the script text file and click on Apply.
- Click on Apply and we can see the successful list deletion message from the console.
I had the Site page opened in the browser prior to running the script. Refreshing it gives the below error stating the deletion of the site.
Summary
Thus, we have seen how to create a SharePoint 2016 web and delete the existing web in SharePoint 2016, using REST API. This works the same way with Office 365 as well.