Introduction
In Share Point we have if conditions such as "If this user is a member of this share point group". We can use this condition to determine whether or not a user belongs to a specific group.
Cause
Determine whether a user belongs to a SharePoint Group and hide some controls in new or edit forms in a SharePoint Custom List.
Solution
The following script will help you determine whether the current logged in SharePoint user belongs to a SharePoint user group and based on it hide some controls in a new or edit form in a Custom List.
Step 1
Navigate to your SharePoint 2013 site.
Step 2
From this page select Site Actions | Edit Page.
Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts" picker area, go to the "Media and Content" category, select the "Script Editor" Web Part and press the "Add button".
Step 3
Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following:
Script
- <script src="/sites/JS/jquery-1.4.2.min.js"></script><script type="text/javascript">
-
-
- ExecuteOrDelayUntilScriptLoaded(disableControls, "sp.js");
-
- var clientContext = null;
- var web = null;
- var users ;
- var oList;
- var oListNew;
-
- function disableControls()
- {
- clientContext = new SP.ClientContext();
- var groupCollection = clientContext.get_web().get_siteGroups();
- var group = groupCollection.getById(4);
- users = group.get_users();
- clientContext.load(group);
- clientContext.load(users);
- currentUser = clientContext.get_web().get_currentUser();
- clientContext.load(currentUser);
- clientContext.executeQueryAsync(Function.createDelegate(this,
- this.onQuerySucceeded), Function.createDelegate(this,
- this.onQueryFailed));
- RefreshCommandUI();
- }
- function onQuerySucceeded()
- {
- if(this.users.get_count() >0)
- {
- var UserExistInGroup = false;
- for(var i=0; i < users.get_count(); i++)
- {
-
- if(users.itemAt(i).get_loginName() == this.currentUser.get_loginName())
- {
- UserExistInGroup = true;
- break;
- }
- }
- }
- if (UserExistInGroup)
- {
- $('nobr:contains("Approver")').closest('tr').show();
- }
- else
- {
- $('nobr:contains("Approver")').closest('tr').hide();
- }
- }
- function onQueryFailed(sender, args)
- {
-
- }
- </script>
Final Result