Free MOSS Web Part – Hide Controls via JavaScript

Note: version 0.2 posted with minor bugfix 15th March 08!

Note2: Only works with MOSS 2007 sorry as you WSS guys do not have audiences targeting 🙁

This is my small contribution to the SharePoint world. It is a web part that once added to a web part page, allows you to customise the display by adding JavaScript to selectively hide controls on the page . Ever needed to hide a field from display/edit for a certain audience? Well here is a way do it without requiring SharePoint Designer and having to break a page from it’s site definition (unghosting).

Before and after shots below (look ma – no top button!)

image  image

To fully understand what is being done here, I suggest you read my series of articles on the use of JavaScript in SharePoint. Part 3 in particular will show you how to safely add this web part to pages with editing disabled (NewForm.aspx, EditForm.aspx and DispForm.aspx)

The full series can be found here: Part 1, Part 2, Part 3, Part 4, Part 5 and Part 6.

Kudos to Jeremy Thake for feedback and some code contribution. Despite being seriously metrosexual, he is otherwise otherwise very cool :-P.

Now two important warnings:

Warning 1: This is an alpha quality release and I may never touch it again 🙂 So you very likely *will* break it. If there is enough interest, I am happy to pop it on codeplex

Warning 2: This web part should NOT be considered as a security measure and thus used in any security sensitive scenario (such as an extranet or WCM site). JavaScript by its very nature can be trivially interfered with and thus other methods (server side) should be employed in these scenarios to prevent interference at the browser.

You can download by reading the disclaimer and clicking the button below..

THIS CODE IS PROVIDED UNDER THIS LICENSE ON AN “AS IS” BASIS, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER

Use at your own risk!

To install perform the following commands

  1. stsadm.exe” -o addsolution -filename CleverWorkAroundsHideFields.wsp
  2. stsadm.exe” -o execadmsvcjobs
  3. stsadm.exe” -o deploysolution -name CleverWorkAroundsHideFields.wsp -immediate -allowgacdeployment -allcontenturls
  4. stsadm.exe” -o execadmsvcjobs

To remove/reinstall perform the following commands

  1. stsadm.exe” -o retractsolution -name CleverWorkAroundsHideFields.wsp -immediate -allcontenturls
  2. stsadm.exe” -o execadmsvcjobs
  3. stsadm.exe” -o deletesolution -name CleverWorkAroundsHideFields.wsp
  4. stsadm.exe” -o execadmsvcjobs
  5. stsadm.exe” -o addsolution -filename CleverWorkAroundsHideFields.wsp
  6. stsadm.exe” -o execadmsvcjobs
  7. stsadm.exe” -o deploysolution -name CleverWorkAroundsHideFields.wsp -immediate -allowgacdeployment -allcontenturls
  8. stsadm.exe” -o execadmsvcjobs

15 Comments on “Free MOSS Web Part – Hide Controls via JavaScript

  1. can’t get this to display on my dev site. Followed directions specifically, and according to everything in cmd prompt and subsequently in the SP Central Admin, it states that it deployed successfully. But even after iisreset, still not appearing in the web part gallery. Any thoughts? Is this going to need to be unpackaged manually and deployed via the .dll, .xml, .dwp files manually?

    thanks for any assistance anyone can provide!

  2. In site admin -> site settings -> site collection features, enable the cleverworkarounds hide fields web part

    regards

    Paul

  3. that’s funny…just found it myself and came back here to post what I did and saw that you had replied. Thanks for the fast response!

  4. Hello there,
    I find your post quite useful. But my problem is little different can you
    please suggest me something to overcome it.
    I have a list names L1 which has a column named status(type dropdown).
    Status may have three values open, onprogress and closed.
    I created three views on the L1 based on three respective values.
    On default.aspx of my sitecollection i added three list view webpart and
    selected Open , on progress and closed view.
    I have already set item level permissions on L1 means only ower can see
    and edit his/her item.
    Now I want that user should not be able to edit items that have
    on progress or closed status. So for on progress and closed view , i detached editing option. But whenever from the edit option from Title column user .
    But real problem is that if user will click on
    any of view name he will be led to allitem.aspx and where he can edit
    any item. I want to disable edit option for the items which have status
    value as on progress or closed.
    Please help me .
    How can i do it using javascript in any of list forms.
    Thanks
    -Neha

  5. Also i forgot to mention that i can use SPD because that will unghost the
    page. So i am left with javascript or any other solution if you suggest.
    Thanks a ton in advance.
    -Neha

  6. Thanks for this great tool!
    Is it possible to get the sources?
    Will you put it on codeplex?
    I’d be very happy to find it there.

    thx in advance
    JaSon

  7. This is working well in IE, but not in Firefox…and Javascript is enabled on browser end. Has anyone else seen this problem?

    thanks,
    t

  8. Hi!
    I can’t seem to find the Target Audience field when editing the properties of the webpart based on the wsp file you’ve uploaded.

    Thanks.

    Jess

  9. As someone mention before for some reason firefox wont process the end of the javascript I used the debug option to to confirm it was running. I tried i few scripts but unfortunately im not programmer. I dont know if you still are checking this thread but I was wondering if you have gotten it to work in firefox?

  10. The reason that firefox doesn’t work is that it does not recogise a comment as a valid HTML element and therefore my method will not work. But since all that nice integration with office requires IE, I’ve never worried about it. These days I would use a jQuery based solution anyway

  11. This is a great solution. My problem is only slightly different in that I need to hide the controls based on user intp to previous controls. For example, you click “Yes” to a question, I want to reveal a set of questions. Do you have a solution for that?

    Please respond as I am in a tight bind at the moment and you seem to be the closest to a solution.

    Thanks a bunch!

    Eddie

  12. Sorry mate but to do what you want requires some customised javascript coding or to pay lots and use InfoPath and publiush it via forms services

    regards

    Paul

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.