0

I'm trying to attach a script to a list page in order to hide a column depending on the user's permissions.

This is the code I put on the aspx using SharePoint Designer 2010:

<asp:Content ContentPlaceHolderId="ScriptHideColumn" runat="server">
<SharePoint:ScriptLink runat="server" Localizable="false" 
name="/SiteAssets/_scriptEscondercolumna.js" id="ScriptHideColumn1" />
</asp:Content>

And this is the error I get:

Error An unexpected error has occurred. Troubleshoot issues with Microsoft SharePoint Foundation. Correlation ID: 308118dd-5e8c-419e-9855-cc5be7810518 Date and Time: 11/04/2018 09:01:29 Go back to site

How can I make the script work without getting any errors?

This is the javascript code:

// JavaScript source code

 function checkUserGroup() {
     bool isMember = false;
     SPSecurity.RunWithElevatedPrivileges(delegate  
        {
            using(SPSite site = new 
SPSite(".../test_")) {
                SPWeb web = site.RootWeb;
                string groupName = "Test_ Visitors";
                var spGroup = web.Groups[groupName];
                isMember = web.IsCurrentUserMemberOfGroup(spGroup.ID);
            }
        });

    return isMember;
}

function hideColumn() {
    var isVisitor = checkUserGroup();
    if (isVisitor) {
        document.getElementsByName("columnHide")[0].style.display = none;
    }
}
6
  • 1) Share the logs 2) try to provide another js file path instead of _scriptEscondercolumna.js to just check whether you are calling js correct or not 3) Also, share your js code Apr 11, 2018 at 7:31
  • 1) I don't get other logs than this error. 2) I keep getting this error. 3)Now there's the js code. 4)Thank you for your help
    – Manu R
    Apr 11, 2018 at 7:43
  • The first function does not look like JavaScript to me. More like C#. social.msdn.microsoft.com/Forums/sqlserver/en-US/… Apr 11, 2018 at 7:53
  • And how can I make a similar function in JS? is it even possible? They keep telling me that this can be done in javascript directly attaching it to the aspx file using the Sharepoint Designer, but they don't know how to do it.
    – Manu R
    Apr 11, 2018 at 7:57
  • @Christoffer is right, you are using managed .net CSOM in js file, that why it is giving an error. Apr 11, 2018 at 7:59

2 Answers 2

0

Better of by not touching SharePoint Designer and messing with the listform.

Instead you could add a content editor webpart on the listform from the UI and link the code from a txt/js file uploaded in the same site in the CEWP.

Using jQuery and SPServices, you can check if the current user is member of the group "Test_Visitors" and then hide the column "columnHide" if the user is a visitor.

This example using a CDN to get the required libraries.

<script language="javascript" type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices-2014.02.min.js"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function() {

   $().SPServices({
     operation: "GetGroupCollectionFromUser",
     userLoginName: $().SPServices.SPGetCurrentUser(),
     async: false,
     completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='Test_ Visitors']").length == 0)
        {
           $("input[id^='columnHide']").closest('tr').hide();
        }
      }
   });
});

</script>

SPServices GitHub

4
  • I don't see the Content Editor web part. When I edit the List and click on Add a Web Part, these are the only web parts I can add: Announcements, Calendar, Links, Shared Documents, Site Assets, Site Pages, Tasks, Team Discussion, and TestList_3 (my list). Is the content editor web part elsewhere?
    – Manu R
    Apr 11, 2018 at 8:50
  • I don't have access to SharePoint 2010 anymore, but they should be there. If you don't see any webparts, you might need to find someone with more permissions on the site that can add the webparts for you. blogs.msdn.microsoft.com/cumgranosalis/2009/12/06/… Apr 11, 2018 at 8:58
  • Okay, I'll try asking for more permissions but I don't think they'll give me more. Anyway, thank you for your help.
    – Manu R
    Apr 11, 2018 at 9:01
  • I'll suggest to see if your SharePoint admin / IT-department can assist you! Apr 11, 2018 at 9:14
0

You can replace your code with below code:

<script language="javascript" type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.1.min.js"></script>

function hideColumn() {
     var clientContext = new SP.ClientContext.get_current();
     var currentUser = clientContext.get_web().get_currentUser();
     clientContext.load(currentUser);

     var userGroups = currentUser.get_groups();
     clientContext.load(userGroups);
     clientContext.executeQueryAsync(function(){
     var groupsEnumerator = userGroups.getEnumerator();
           while (groupsEnumerator.moveNext()) {
                 var group= groupsEnumerator.get_current();               
                 if(group.get_title() == "Test_ Visitors") {
                 // hide your column here
                 // try to hide your column with id like $("#your_column_id").hide(); OR try it with your code
                 document.getElementsByName("columnHide")[0].style.display = none;

                 break;
            }
            //etc...
         }

       }, function(err){
                alert(err);
          });
      }

 SP.SOD.executeOrDelayUntilScriptLoaded(hideColumn,'sp.js');

In above code, I am checking that if the current user is in Test_ Visitors then hide it. In a similar way, you have to check whether the user is the part of the permission group or not and write js code to hide the column using column id as per my commented code.

Your Answer

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

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