none
Hide Export to Excel feature for a specific group permission RRS feed

  • Question

  • Hello everyone!

    I want to hide [Export to Excel] feature for a specific group permission. I have unchecked "Use Client Integration Features" in the group permission but nothing happens they still can export the list data.
    Also I've tried the following CSS code but the problem hides from all users:

    <style type="text/css"> a[id*='ExportToSpreadsheet'] { display:none;} </style>

    Export To Excel

    Since last week, I've added a code to hide multiple columns for a specific group permission but it breaks when exporting to excel that's why i want to hide this feature.

    Is it possible to achieve it with a piece of code that hide this option for this library, and for a specific group permission ?

    Any help will be greatly appreciated.
    Thank you!


    • Edited by SynthBZ Sunday, December 1, 2019 8:39 AM
    Sunday, December 1, 2019 8:33 AM

Answers

  • Hi ,

    Try below script.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            function IsCurrentUserMemberOfGroups(groups, OnComplete) {
                var currentContext = new SP.ClientContext.get_current();
                var currentWeb = currentContext.get_web();
                var currentUser = currentWeb.get_currentUser();
                var oGroups = currentUser.get_groups();
                currentContext.load(currentUser);
                currentContext.load(oGroups);
                currentContext.executeQueryAsync(OnSuccess, OnFailure);
    
                function OnSuccess(sender, args) {
                    var userInGroup = false;
                    var groupEnumerator = oGroups.getEnumerator();
                    while (groupEnumerator.moveNext()) {
                        var oGroup = groupEnumerator.get_current();
                        var groupTitle = oGroup.get_title();
                        console.log(groupTitle);
                        $.each(groups, function (index, value) {
                            if (value == groupTitle) {
                                console.log('user in group ' + groupTitle);
                                userInGroup = true;
                            }
                        });
                        if (userInGroup)
                            break;
                    }
                    OnComplete(userInGroup);
                }
    
                function OnFailure(sender, args) {
                    OnComplete(false);
                }
            }
            function CheckUserGroups() {
                var groups = ["Dev Visitors", "Designers"]
                IsCurrentUserMemberOfGroups(groups, function (isCurrentUserInGroup) {
                    if (isCurrentUserInGroup) {
                        var css = "a[id*='ExportToSpreadsheet']{display:none;}",
                            head = document.head || document.getElementsByTagName('head')[0],
                            style = document.createElement('style');
    
                        head.appendChild(style);
                        style.type = 'text/css';
                        style.title = 'MSCustom';
                        if (style.styleSheet) {
                            // This is required for IE8 and below.
                            style.styleSheet.cssText = css;
                        } else {
                            style.appendChild(document.createTextNode(css));
                        }
                    }
                });
    
            }
            ExecuteOrDelayUntilScriptLoaded(CheckUserGroups, 'SP.js');
        </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by SynthBZ Tuesday, December 3, 2019 10:11 AM
    Tuesday, December 3, 2019 8:59 AM

All replies

  • Hi,

    Just add a script editor webpart to the page.

    Using javascript to get current user's group and if it is the target group, change the style to hide the ribbon.

    Below is a sample code for your reference.

    <script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(injectMethod,'sp.js');
    var currentUser;
    function injectMethod(){
    getcurrentuserDetails();
    }
    function getcurrentuserDetails () {
    var clientContext = SP.ClientContext.get_current();
    oUser = clientContext.get_web().get_currentUser();
    oGroups = oUser.get_groups();
    clientContext.load(oUser);
    clientContext.load(oGroups);
    clientContext.executeQueryAsync(
    Function.createDelegate(this, function() {
    var groupsInfo = '';
    var groupsEnumerator = oGroups.getEnumerator();
    console.log('Count of current user groups: ' + oGroups.get_count());
    while (groupsEnumerator.moveNext()) {
    var oGroup = groupsEnumerator.get_current();
    groupsInfo += '\n' + 'Group ID: ' + oGroup.get_id() + ', ' + 'Title : ' + oGroup.get_title();
    }
    alert(groupsInfo.toString());
    }),
    Function.createDelegate(this, function() {
    alert('failed');
    }));
    }
    </script>


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Learn Microsoft 365 from Microsoft DOCs now!
    Please Vote and Mark as Answer if it helps you.

    Monday, December 2, 2019 1:26 AM
  • Hi

    Sample tested script, the JSOM script is shared in below thread.

    https://sharepoint.stackexchange.com/questions/86912/check-if-user-is-in-a-specified-group

    <script type="text/javascript">
            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);
                }
            }
            function IsCurrentUserHasContribPerms() {
                IsCurrentUserMemberOfGroup("Dev Members", function (isCurrentUserInGroup) {
                    if (isCurrentUserInGroup) {
                        var css = "a[id*='ExportToSpreadsheet']{display:none;}",
                            head = document.head || document.getElementsByTagName('head')[0],
                            style = document.createElement('style');
    
                        head.appendChild(style);
                        style.type = 'text/css';
                        style.title = 'MSCustom';
                        if (style.styleSheet) {
                            // This is required for IE8 and below.
                            style.styleSheet.cssText = css;
                        } else {
                            style.appendChild(document.createTextNode(css));
                        }
                    }
                });
    
            }
            ExecuteOrDelayUntilScriptLoaded(IsCurrentUserHasContribPerms, 'SP.js');
        </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, December 2, 2019 2:12 AM
  • Hi Lee__li,

    The scripts you mentioned works perfectly.

    I got one more question what i should change in the code to add more groups in this script ?

    Because I've tried to add in this line more groups but i couldn't achieve it.

     IsCurrentUserMemberOfGroup("TEST1","TEST2", function (isCurrentUserInGroup) {

    Any idea how to add more groups?

    Thank you for your support!

    Tuesday, December 3, 2019 8:27 AM
  • Hi ,

    Try below script.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            function IsCurrentUserMemberOfGroups(groups, OnComplete) {
                var currentContext = new SP.ClientContext.get_current();
                var currentWeb = currentContext.get_web();
                var currentUser = currentWeb.get_currentUser();
                var oGroups = currentUser.get_groups();
                currentContext.load(currentUser);
                currentContext.load(oGroups);
                currentContext.executeQueryAsync(OnSuccess, OnFailure);
    
                function OnSuccess(sender, args) {
                    var userInGroup = false;
                    var groupEnumerator = oGroups.getEnumerator();
                    while (groupEnumerator.moveNext()) {
                        var oGroup = groupEnumerator.get_current();
                        var groupTitle = oGroup.get_title();
                        console.log(groupTitle);
                        $.each(groups, function (index, value) {
                            if (value == groupTitle) {
                                console.log('user in group ' + groupTitle);
                                userInGroup = true;
                            }
                        });
                        if (userInGroup)
                            break;
                    }
                    OnComplete(userInGroup);
                }
    
                function OnFailure(sender, args) {
                    OnComplete(false);
                }
            }
            function CheckUserGroups() {
                var groups = ["Dev Visitors", "Designers"]
                IsCurrentUserMemberOfGroups(groups, function (isCurrentUserInGroup) {
                    if (isCurrentUserInGroup) {
                        var css = "a[id*='ExportToSpreadsheet']{display:none;}",
                            head = document.head || document.getElementsByTagName('head')[0],
                            style = document.createElement('style');
    
                        head.appendChild(style);
                        style.type = 'text/css';
                        style.title = 'MSCustom';
                        if (style.styleSheet) {
                            // This is required for IE8 and below.
                            style.styleSheet.cssText = css;
                        } else {
                            style.appendChild(document.createTextNode(css));
                        }
                    }
                });
    
            }
            ExecuteOrDelayUntilScriptLoaded(CheckUserGroups, 'SP.js');
        </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by SynthBZ Tuesday, December 3, 2019 10:11 AM
    Tuesday, December 3, 2019 8:59 AM
  • Thank you very much Lee__li!
    Tuesday, December 3, 2019 10:11 AM
  • Hi,

    Thanks for marking the replies as answer. I’m pleased to know that the information is helpful to you. I hope you are delighted with the service we provided you.

    Here I will provide a brief summary of this post for your information. 

    Requirement

    ====================

    Hide Export to Excel feature for a specific group permission 

    Solution

    ====================

    Use JSOM to check user groups and add CSS to hide the command if user in group.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, December 9, 2019 9:07 AM