Introduction
SharePoint 2013 introduces a Representational State Transfer (REST) service that is comparable to the existing SharePoint client object models. This allows the developers to interact remotely with SharePoint data using any technology that supports REST web requests. This means that developers can perform Create, Read, Update and Delete (CRUD) operations from their apps for SharePoint, solutions and client applications, using REST web technologies and standard Open Data Protocol (OData) syntax. In this article you will see the following:
Create an app using the NAPA Tool in SharePoint 2013 Online.
Cross-Domain Requests.
Delete a site group using the REST API.
Delete a role definition using the REST API.
Get the permission level properties using the REST API.
Endpoint URI
Note: If you are making cross-domain requests then you need to add SP.AppContextSite(@target) and ?@target='<host web url>' to the endpoint URI.
HTTP Method: POST
Properties
The following properties must be used in the REST request for deleting the role definition:
IF-MATCH header: It is required in POST requests for a DELETE operation. Description: Provides a way to verify that the object being changed has not been changed since it was last retrieved. Or, lets you specify to overwrite any changes, as shown in the following example: "IF-MATCH":"*".
X-HTTP-Method header: It is required in POST requests for DELETE operations. Description: Used to specify that the request performs a delete operation. Example: "X-HTTP-Method":"DELETE".Use the following to get the permission level properties.
HTTP Method:Ā GET: Read a Resource JSON:
Create an app using the NAPA Tool
Navigate to the SharePoint 2013 Online site.
Click on Site Contents in the quick launch bar.
Click on āNapaā Office 365 Development Tools.
Click on Add New Project.
Select App for SharePoint, enter the Project name (choose the project name depending on your requirements for whatever you will do, like: deleting site group, deleting role definition or creating an application to get the permission level properties) and then click on Create.
Permissions
Ensure appropriate permission is provided to access the content. Click on the Properties button, and then click on Permissions. Set the required permission to access the content.
Default.aspx
Now suppose you creating an application for deleting a site group in SharePoint. So, for that replace the contents of Default.aspx with the following:
<%--Ā TheĀ markupĀ andĀ scriptĀ inĀ theĀ followingĀ ContentĀ elementĀ willĀ beĀ placedĀ inĀ theĀ <head>ofĀ theĀ pageĀ --%>
<asp:content contentplaceholderid="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<!--Ā AddĀ yourĀ CSSĀ stylesĀ toĀ theĀ followingĀ fileĀ -->
<link rel="Stylesheet" type="text/css" href="../Content/App.css" />
<!--Ā AddĀ yourĀ JavaScriptĀ toĀ theĀ followingĀ fileĀ -->
<script type="text/javascript" src="../Scripts/App.js"></script>
</asp:content>
<%--Ā TheĀ markupĀ inĀ theĀ followingĀ ContentĀ elementĀ willĀ beĀ placedĀ inĀ theĀ TitleAreaĀ ofĀ theĀ pageĀ --%>
<asp:content contentplaceholderid="PlaceHolderPageTitleInTitleArea" runat="server">PageĀ Title</asp:content>
<%--Ā TheĀ markupĀ andĀ scriptĀ inĀ theĀ followingĀ ContentĀ elementĀ willĀ beĀ placedĀ inĀ theĀ <body>ofĀ theĀ pageĀ --%>
<asp:content contentplaceholderid="PlaceHolderPageTitleInTitleArea" runat="server">RESTĀ APIĀ Examples</asp:content>
<%--Ā TheĀ markupĀ andĀ scriptĀ inĀ theĀ followingĀ ContentĀ elementĀ willĀ beĀ placedĀ inĀ theĀ <body>ofĀ theĀ pageĀ --%>
<asp:content contentplaceholderid="PlaceHolderMain" runat="server">
<div>
<p>
<b>DeleteĀ Group</b>
<br />
<input type="text" value="GroupĀ NameĀ Here" id="deletegroupname" />
<button id="deletegroupbutton">DeleteĀ Group</button>
</p>
</div>
</asp:content>
If you want to create an application for deleting the role definition in SharePoint, just replace the contents of Default.aspx with the following:
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
<div>
<p>
<b>DeleteĀ RoleĀ Definition</b>
<br />
<input type="text" value="RoleĀ DefinitionĀ Name" id="rdtext" />
<button id="deleterdbutton">DeleteĀ RoleDefinition</button>
</p>
</div>
</asp:Content>
If you want to create an application for getting the permission level properties in SharePoint, just replace the contents of Default.aspx with the following:
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
<div>
<p>
<b>RoleĀ DefinitionĀ Properties</b>
<br />
<input type="text" value="RoleĀ definitionĀ NameĀ Here" id="Text1" />
<button id="rdpropertiesbutton">GetĀ Properties</button>
</p>
</div>
</asp:Content>
App.js
To create an application for deleting a site group in SharePoint, just replace the contents of App.js with the following:
'useĀ strict';
varĀ hostweburl;
varĀ appweburl;
//Ā LoadĀ theĀ requiredĀ SharePointĀ libraries.
$(document).ready(functionĀ ()Ā {
//GetĀ theĀ URIĀ decodedĀ URLs.
hostweburlĀ =Ā decodeURIComponent(
getQueryStringParameter("SPHostUrl"));
appweburlĀ =Ā decodeURIComponent(
getQueryStringParameter("SPAppWebUrl"));
//AssignĀ eventsĀ toĀ buttons
$("#deletegroupbutton").click(functionĀ (event)Ā {
deleteGroup();
event.preventDefault();
});
//Ā ResourcesĀ areĀ inĀ URLsĀ inĀ theĀ form:
//Ā web_url/_layouts/15/resource
varĀ scriptbaseĀ =Ā hostweburlĀ +Ā "/_layouts/15/";
//Ā LoadĀ theĀ jsĀ fileĀ andĀ continueĀ toĀ loadĀ theĀ pageĀ withĀ information.
//Ā SP.RequestExecutor.jsĀ toĀ makeĀ cross-domainĀ requests
$.getScript(scriptbaseĀ +Ā "SP.RequestExecutor.js");
});
//Ā Utilities
//Ā RetrieveĀ aĀ queryĀ stringĀ value.
//Ā ForĀ productionĀ purposesĀ youĀ mayĀ wantĀ toĀ useĀ aĀ libraryĀ toĀ handleĀ theĀ queryĀ string.
functionĀ getQueryStringParameter(paramToRetrieve)Ā {
varĀ paramsĀ =Ā document.URL.split("?")[1].split("&");
forĀ (varĀ iĀ =Ā 0;Ā iĀ <Ā params.length;Ā iĀ =Ā iĀ +Ā 1)Ā {
varĀ singleParamĀ =Ā params[i].split("=");
ifĀ (singleParam[0]Ā ==Ā paramToRetrieve)Ā returnĀ singleParam[1];
}
}
//Ā DeleteĀ siteĀ group
functionĀ deleteGroup()Ā {
varĀ groupnameĀ =Ā document.getElementById("deletegroupname").value;
varĀ executor;
//Ā InitializeĀ theĀ RequestExecutorĀ withĀ theĀ appĀ webĀ URL.
executorĀ =Ā newĀ SP.RequestExecutor(appweburl);
executor.executeAsync({
url:Ā appweburlĀ +Ā "/_api/SP.AppContextSite(@target)/web/sitegroups/removebyloginname('"Ā +Ā groupnameĀ +Ā "')?@target='"Ā +Ā hostweburlĀ +Ā "'",
method:Ā "POST",
success:Ā deleteGroupSuccessHandler,
error:Ā deleteGroupErrorHandler
});
}
//Ā SuccessĀ Handler
functionĀ deleteGroupSuccessHandler(data)Ā {
alert("GroupĀ deletedĀ successfully")
}
//Ā ErrorĀ Handler
functionĀ deleteGroupErrorHandler(data,Ā errorCode,Ā errorMessage)Ā {
alert("CouldĀ notĀ deleteĀ theĀ group:Ā "Ā +Ā errorMessage);
}
When you create application for deleting role definition in SharePoint. So, for that just change the App.js with the following
//AssignĀ eventsĀ toĀ buttons
$("#deleterdbutton").click(functionĀ (event)Ā
{
deleteRoleDefinition();
event.preventDefault();
});
//Ā DeleteĀ theĀ roleĀ definition
functionĀ deleteRoleDefinition()Ā {
varĀ rdNameĀ =Ā document.getElementById("rdtext").value;
varĀ executor;
//Ā InitializeĀ theĀ RequestExecutorĀ withĀ theĀ appĀ webĀ URLĀ Ā Ā
executorĀ =Ā newĀ SP.RequestExecutor(appweburl);
executor.executeAsync(
{
url:Ā appweburlĀ +Ā "/_api/SP.AppContextSite(@target)/web/roledefinitions/getbyname('"Ā +Ā rdNameĀ +Ā "')?@target='"Ā +Ā hostweburlĀ +Ā "'",
method:Ā "POST",
headers:Ā
{
"IF-MATCH":Ā "*",
"X-HTTP-Method":Ā "DELETE"
Ā },
success:Ā deleteRDSuccessHandler,
error:Ā deleteRDErrorHandler
});
}
//Ā SuccessĀ Handler
functionĀ deleteRDSuccessHandler(data)Ā {
alert("RoleĀ DefinitionĀ deletedĀ successfully")
}
//Ā ErrorĀ Handler
functionĀ deleteRDErrorHandler(data,Ā errorCode,Ā errorMessage)Ā {
alert("CouldĀ notĀ deleteĀ theĀ roleĀ definition:Ā "Ā +Ā errorMessage);
}
When you create application for get the permission level properties in SharePoint. So, for that just change the App.js with the following
//AssignĀ eventsĀ toĀ buttons
$("#rdpropertiesbutton").click(functionĀ (event)Ā {
rdProperties();
event.preventDefault();
});
//Ā GetĀ theĀ roleĀ definitionĀ properties
functionĀ rdProperties()Ā {
varĀ rdnameĀ =Ā document.getElementById("rdtext").value;
varĀ executor;
//Ā InitializeĀ theĀ RequestExecutorĀ withĀ theĀ appĀ webĀ URL.
executorĀ =Ā newĀ SP.RequestExecutor(appweburl);
executor.executeAsync({
url:Ā appweburlĀ +Ā "/_api/SP.AppContextSite(@target)/web/roledefinitions/getbyname('"Ā +Ā rdnameĀ +Ā "')?@target='"Ā +Ā hostweburlĀ +Ā "'",
Ā method:Ā "GET",
Ā headers:Ā {
"Accept":Ā "application/json;Ā odata=verbose"
},
success:Ā rdPropertiesSuccessHandler,
error:Ā rdPropertiesErrorHandler
});
}
//Ā SuccessĀ Handler
functionĀ rdPropertiesSuccessHandler(data)Ā {
varĀ jsonObjectĀ =Ā JSON.parse(data.body);
varĀ propertiesĀ =Ā 'RoleĀ DefinitionĀ Properties:\n';Ā
propertiesĀ +=Ā "NameĀ Ā Ā Ā Ā Ā Ā Ā Ā :Ā "Ā +Ā jsonObject.d.NameĀ +Ā '\n';
propertiesĀ +=Ā "DescriptionĀ Ā :Ā "Ā +Ā jsonObject.d.DescriptionĀ +Ā '\n';
alert(properties);
}
Deploy the App
Click on Run Project.
The app will be packaged, deployed and launched.
Click on āClick here to launch your app in a new windowā.
At that point you need to Trust it.
If you are working on a site group deleting application, the window looks as in the following:
If you working on a role definition deletion application then the window looks as in the following:
If you working on an application to get the permission level properties then the window looks as in the following
Enter the group name that you want to delete and then click on the Delete Group button.
The Site group is deleted successfully in the host site.
Role definition is deleted successfully in the host site.
If working on a site role definition application then enter the site role definition name and then click on the Get Properties button.
Summary
Thus in this article you saw how to delete a site group, role definition and get the permission level properties in the host site using the REST API in SharePoint 2013 Online.
Comments