0

I am fairly new to SharePoint Online... What I am trying to accomplish is hiding a certain field in the View and Edit view mode on a Custom List. I have found articles on it but can't seem to figure what I am doing wrong because nothing I do works. I am entering the following code as SEWP in both the View and Edit view modes.

The goal I am trying to achieve: Hide the "Feedback" column if the user IS NOT part of the "Template Test Members" permission group.

    <script type="text/javascript>
function checkUser() {
    IsCurrentUserMemberOfGroup("Template Test Members", function(isCurrentUserInGroup) {
        if (isCurrentUserInGroup) {

        } else {
            document.getElementById("Feedback").style.display = "none";
        }
    });
}

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
    var currentContext = new SP.ClientContext.get_current();
    var currentWeb = currentContext.get_web();
    var currentUser = currentContext.get_web().get_currentUser();
    currentContext.load(currentUser);
    var allGroups = currentWeb.get_siteGroups();
    currentContext.load(allGroups);
    var group = allGroups.getByName(groupName);
    currentContext.load(group);
    var groupUsers = group.get_users();
    currentContext.load(groupUsers);
    currentContext.executeQueryAsync(OnSuccess, OnFailure);

    function OnSuccess(sender, args) {
        var userInGroup = false;
        var groupUserEnumerator = groupUsers.getEnumerator();
        while (groupUserEnumerator.moveNext()) {
            var groupUser = groupUserEnumerator.get_current();
            if (groupUser.get_id() == currentUser.get_id()) {
                userInGroup = true;
                break;
            }
        }
        OnComplete(userInGroup);
    }

    function OnFailure(sender, args) {
        OnComplete(false);
    }
}
</script>

Display/View Mode

enter image description here

Edit Mode

enter image description here

-Thank you again for any help you can provide

0

3 Answers 3

0

You can also use jQuery and REST API in script editor webpart:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">

IsCurrentUserMemberOfGroup = function (groupName) {

  var result = false;
  $.ajax({
    url: _spPageContextInfo.webAbsoluteUrl +'/_api/web/currentuser/groups',
    type: "GET",
    async: false,
    contentType: "application/json;odata=verbose",
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),       
    },
    success: function(data){
        var groups = data.d.results
        for(i=0; i<groups.length; i++) {
            if (groups[i].LoginName == groupName) {               
               result = true;
            }
        };
    },
    error: function() { return false; }
  })
  return result;
}

checkUser = function() {
    if (IsCurrentUserMemberOfGroup("Template Test Members") == false) 
        $('nobr:contains("Feedback")').closest('tr').hide();
}
_spBodyOnLoadFunctionNames.push("checkUser");
</script>
1
  • thank you for taking the time to give me a hand. Much Appreciated
    – J. Lopez
    Commented Aug 28, 2018 at 18:47
0

In your code, try to use $('nobr:contains("Feedback")').closest('tr').hide(); instead of document.getElementById("Feedback").style.display = "none"; where there is no ID for each field.

Note: don't forget to add JQuery ref <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>


Also, you can achieve that using **SPServices**

Try to add the below Code in script editor web part for each Edit/ Display Page

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices.js"></script>
<script type="text/javascript">
$(document).ready(function() {
 $().SPServices({
 operation: "GetGroupCollectionFromUser",
 userLoginName: $().SPServices.SPGetCurrentUser(),
 async: false,
 completefunc: function(xData, Status) {

 //in case, the current User does belong to the group "Template Test Members"
 if($(xData.responseXML).find("Group[Name='Template Test Members']").length != 1)
   {
     $('nobr:contains("Feedback")').closest('tr').show();
   } else
   { 
     $('nobr:contains("Feedback")').closest('tr').hide();
   } 
  }
 });
});
</script>

Note: In display mode, you should use $('h3:contains("Feedback")').closest('tr').hide();

Check also, Show / Hide a Div based on a user group in SharePoint via SPServices

3
  • El-Qassas, thank you so much, that worked perfectly. I've followed your examples since I began using SharePoint. Means a lot, thank you again.
    – J. Lopez
    Commented Aug 28, 2018 at 18:47
  • You are welcome @J.Lopez , glad to hear it helped you, Commented Aug 28, 2018 at 19:43
  • hello @J.Lopez Why you have removed the accepted answer!! Commented Nov 30, 2018 at 21:43
0

Test script based on your code.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            SP.SOD.executeOrDelayUntilScriptLoaded(Validator, 'SP.js');
            function Validator() {
                IsCurrentUserMemberOfGroup("GroupName", function (isCurrentUserInGroup) {
                    if (isCurrentUserInGroup) {
                        alert("1");                        
                    } else {
                        alert("0"); 
$('input[title="Feedback"]').closest('tr').css('display', 'none');                   
                    }
                });
            }


            function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
                var currentContext = new SP.ClientContext.get_current();
                var currentWeb = currentContext.get_web();
                var currentUser = currentContext.get_web().get_currentUser();
                currentContext.load(currentUser);
                var allGroups = currentWeb.get_siteGroups();
                currentContext.load(allGroups);
                var group = allGroups.getByName(groupName);
                currentContext.load(group);
                var groupUsers = group.get_users();
                currentContext.load(groupUsers);
                currentContext.executeQueryAsync(OnSuccess, OnFailure);

                function OnSuccess(sender, args) {
                    var userInGroup = false;
                    var groupUserEnumerator = groupUsers.getEnumerator();
                    while (groupUserEnumerator.moveNext()) {
                        var groupUser = groupUserEnumerator.get_current();
                        if (groupUser.get_id() == currentUser.get_id()) {
                            userInGroup = true;
                            break;
                        }
                    }
                    OnComplete(userInGroup);
                }

                function OnFailure(sender, args) {
                    OnComplete(false);
                }
            }
        </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.