Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have found a number of recommended ways in blogs looking to answer how to do this and have not successfully made any of them work using SharePoint MOSS 2007 and SharePoint Designer 2007.

I have already hidden the fields that I don't want any user to see within the site content type (those fields being set by a SharePoint Designer Workflow).

I'm trying to hide specific fields on the NewForm.aspx and EditForm.aspx based on whether the current user is in a Specific SharePoint group. My list has a site content type that I created and site columns.

When I look at the NewForm.aspx in Designer the design view gives me generic Field 1, 2, 3 and not my specific content types. If I create a new aspx page and insert SharePoint control > custom list form, then my fields show up. I'll take ways to edit in designer if you have them, but I need someone to check the solution below and help me figure out where I'm messing up.

Went with option indicating to use jquery (1.9.0.js) and SPServices (0.7.2.min.js).

  1. I uploaded both files to a document library on my site.

  2. I then created a CEWP on my NewForm.aspx page (not through designer but the front-end by appending "shared&ToolPaneView=2" to the url.

  3. Pasted the following code into the CEWP:

'

<script type="text/javascript" language="javascript" src="/Style%20Library/JavaScript%20Utilities/jquery.SPServices-0.7.2.min.js"></script>

<script language="javascript" type="text/javascript">

$(document).ready(function() {

  $().SPServices({

    operation: "GetGroupCollectionFromUser",
    userLoginName: $().SPServices.SPGetCurrentUser(),
    async: false,
       completefunc: function (xData, Status) {
       var xml = xData.responseXML.xml;
       if (xml.search('MyGroupName') != -1)

function hidefields() {

         var control = findacontrol("MyColumnName");
         control.parentnode.parentnode.style.display="none";
  }
     }
  });
});
</script>

When I click save in the 'Source Editor' dialogue box, it closes and on my main browser status I get a message 'Please wait while scripts are loaded..." and then nothing happens. I can click okay on the Modify Shared Web Part menu and the web part closes. Get same wait message...

Goal is that if current user is not part of MyGroupName then the field for MyColumnName will not be visible on the page. Not a developer but trying to play one in real life.

If you're still reading, Thanks!!!

share|improve this question
add comment

1 Answer

Your syntax is a little wonky. I also added some jQuery inside your hidefields function instead of the native JS you were using.

Try this...

$(document).ready(function () {

    $().SPServices({

        operation: "GetGroupCollectionFromUser",
        userLoginName: $().SPServices.SPGetCurrentUser(),
        async: false,
        completefunc: function (xData, Status) {
            var xml = xData.responseXML.xml;
            if (xml.search('MyGroupName') != -1) {
                hidefields();
            }
        }
    });
});

function hidefields() {
    var control = $("[title='MyColumnName']");
    control.parent().parent().css("display","none");
}

If you continue to need to troubleshoot this, put some alerts inside your ready() and in your callback, so you can verify whether those functions are being called.

share|improve this answer
1  
Keep in mind that if you need real security (i.e., this is sensitive data), this is not the way to do it. A savvy user will find a way to "unhide" this hidden field. –  Derek Gusoff May 1 '13 at 19:47
    
I replaced my code with yours and still have the field showing up on the NewForm.aspx. I added alert ready(function () { alert('Hello World'); and it worked. Any where else I put it nothing happens. I don't disagree, my attempts at code are always "wonky". –  Just Need Answers May 1 '13 at 20:32
    
You'll need some debugging to sort this out. Unfortunately your options for debugging in a CEWP are limited. By now you know the ready() handler is firing, so then you'll need to figure out: 1) did the SPServices call succeed, 2) did the XML contain what you expected, 3) was the group you were searching for found, 4) was hidefields() called, and 5) did the jQuery in hidefields() actually find the field. If it were me I'd ditch the CEWP and just write the javascript onto the page using SPD, and step through the code using F12, and possibly Fiddle the SPServices call. –  Derek Gusoff May 2 '13 at 13:47
    
if that's outside your abilities you'll have to use alerts everywhere. –  Derek Gusoff May 2 '13 at 13:48
    
I prefer to use console.log() instead of alerts, particularly useful in Firefox where you can drill down into objects logged to the console. NB IE has a quirk that causes a script error if the developer tools aren't open when trying to log to the console. –  David Clarke May 23 '13 at 2:43
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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