Steps for Implementation
- Get Current Context.
- Get App web URL and Host Web URL from Query string parameter.
- Calling check method in document ready.
- Get app context site.
- Get all site groups from host web site.
- Success function check whether group is available or not.
In your JavaScript file write below code,
-
- 'use strict';
-
-
- var context = SP.ClientContext.get_current();
-
- var hostWebURL, appWebURL;
- var isGroupAvail = false;
-
-
- $(document).ready(function () {
-
-
- hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
-
-
- CheckGroupExist();
- });
-
-
- function CheckGroupExist() {
- var appCtxSite = new SP.AppContextSite(context, hostWebURL);
- var website = appCtxSite.get_web();
- var currentGroups = website.get_siteGroups();
- context.load(currentGroups);
- context.executeQueryAsync(function () {
- if (currentGroups.get_count() > 0) {
- var grouEnum = currentGroups.getEnumerator();
- while (grouEnum.moveNext()) {
- var currentGrp = grouEnum.get_current();
- if (currentGrp.get_title() == "Dev Owners") {
- isGroupAvail = true;
- }
-
- }
- if (isGroupAvail) {
- alert("Group is avail in current site");
- }
- else {
- alert("Group is not avail in current site");
- }
- }
- },
- function (sender, args) {
- console.log("Request failed to get site groups :" + args.get_message());
- });
- }
-
-
- function manageQueryStringParameter(paramToRetrieve) {
- var params = document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1) {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve) {
- return singleParam[1];
- }
- }
- }
Note: Make sure to provide permission Manage permission to Site Collection.
Summary
In this article we have seen whether SharePoint user group is exist or not in current site using JavaScript object model.