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" />

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;
            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;
  • 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 Commented 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
    Commented 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/… Commented 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
    Commented 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. Commented Apr 11, 2018 at 7:59

2 Answers 2


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() {

     operation: "GetGroupCollectionFromUser",
     userLoginName: $().SPServices.SPGetCurrentUser(),
     async: false,
     completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='Test_ Visitors']").length == 0)


SPServices GitHub

  • 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
    Commented 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/… Commented 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
    Commented Apr 11, 2018 at 9:01
  • I'll suggest to see if your SharePoint admin / IT-department can assist you! Commented Apr 11, 2018 at 9:14

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();

     var userGroups = currentUser.get_groups();
     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;


       }, function(err){


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 you have read our privacy policy.

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