top of page
Writer's pictureVijai Anand Ramalingam

How to Get the Site Group Properties in SharePoint 2013 Online Using REST API

Introduction

SharePoint 2013 introduces a Representational State Transfer (REST) service that is comparable to the existing SharePoint client object models. This allows developers to interact remotely with SharePoint data by 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:

  1. Create an app using NAPA Tool in SharePoint 2013 Online.

  2. Cross-Domain Requests.

  3. Get the site group properties using the REST API.


Endpoint URI

https://c986.sharepoint.com/_api/web/sitegroups/getbyname(ā€˜groupnameā€™)

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 Request

GET: Read a Resource

Create an app using 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 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

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>GroupĀ Properties</b>

<br />

<input type="text" value="GroupĀ NameĀ Here" id="groupnametext" />

<button id="grouppropertiesbutton">GroupĀ Properties</button>

</p>

</div>

</asp:Content>



App.js

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

$("#grouppropertiesbutton").click(functionĀ (event)Ā {

groupProperties();

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.

//Ā 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];

}

}

//Ā GetĀ theĀ groupĀ properties

functionĀ groupProperties()Ā {

varĀ groupnametextĀ =Ā document.getElementById("groupnametext").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/getbyname('"Ā +Ā groupnametextĀ +Ā "')?@target='"Ā +Ā hostweburlĀ +Ā "'",

method:Ā "GET",

headers:Ā {

"Accept":Ā "application/json;Ā odata=verbose"

Ā },

success:Ā groupPropertiesSuccessHandler,

error:Ā groupPropertiesErrorHandler

});

}

//Ā SuccessĀ Handler

functionĀ groupPropertiesSuccessHandler(data)Ā {

varĀ jsonObjectĀ =Ā JSON.parse(data.body);

varĀ propertiesĀ =Ā 'GroupĀ Properties:\n';

propertiesĀ +=Ā "TitleĀ Ā Ā Ā Ā Ā Ā Ā :Ā "Ā +Ā jsonObject.d.TitleĀ +Ā '\n';

propertiesĀ +=Ā "DescriptionĀ Ā :Ā "Ā +Ā jsonObject.d.DescriptionĀ +Ā '\n';

propertiesĀ +=Ā "LoginNameĀ Ā Ā Ā :Ā "Ā +Ā jsonObject.d.LoginNameĀ +Ā '\n';

propertiesĀ +=Ā "OwnerTitleĀ Ā Ā :Ā "Ā +Ā jsonObject.d.OwnerTitleĀ +Ā '\n';

alert(properties);

}

//Ā ErrorĀ Handler

functionĀ groupPropertiesErrorHandler(data,Ā errorCode,Ā errorMessage)Ā {

alert("CouldĀ notĀ getĀ theĀ groupĀ properties:Ā "Ā +Ā errorMessage);

}


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ā€.


  • Click on Trust it.


  • Enter the site group name and then click on the Group Properties button.


Summary

Thus in this article you saw how to get the site group properties using the REST API in SharePoint 2013 Online.


0 comments

Comments


bottom of page