Client Object Model to Determine Whether a User Belongs to a Group and Hide Controls in New/Edit Forms

Posted: November 7, 2014 in SharePoint2010

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.

Approver option

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

  1. <script src=“/sites/JS/jquery-1.4.2.min.js”></script><script type=“text/javascript”>
  2. ExecuteOrDelayUntilScriptLoaded(disableControls, “sp.js”);
  3. var clientContext = null;
  4.         var web = null;
  5.         var users ;
  6.         var oList;
  7.         var oListNew;
  8. function disableControls()
  9. {
  10. clientContext = new SP.ClientContext();
  11. var groupCollection = clientContext.get_web().get_siteGroups();
  12. var group = groupCollection.getById(4);//the SharePoint usergroup
  13. users = group.get_users();
  14. clientContext.load(group);
  15. clientContext.load(users);
  16. currentUser = clientContext.get_web().get_currentUser();
  17. clientContext.load(currentUser);
  18. clientContext.executeQueryAsync(Function.createDelegate(this,
  19. this.onQuerySucceeded), Function.createDelegate(this,
  20. this.onQueryFailed));
  21. RefreshCommandUI();
  22. }
  23. function onQuerySucceeded()
  24. {
  25. if(this.users.get_count() >0)
  26. {
  27. var UserExistInGroup = false;
  28. for(var i=0; i < users.get_count(); i++)
  29. {
  30. if(users.itemAt(i).get_loginName() == this.currentUser.get_loginName())
  31. {
  32. UserExistInGroup = true;
  33. break;
  34. }
  35. }
  36. }
  37. if (UserExistInGroup)
  38. {
  39. $(‘nobr:contains(“Approver”)’).closest(‘tr’).show();
  40. }
  41. else
  42. {
  43.  $(‘nobr:contains(“Approver”)’).closest(‘tr’).hide();
  44. }
  45. }
  46. function onQueryFailed(sender, args)
  47. {
  48. }
  49. </script>

Final Result

add new item

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s