Wednesday, January 2, 2008

Removing the New action from a MOSS List

So I had an interesting problem with the new project. The client wants an application that uses the dicussion board functionality, but with alot of additional functionality. Amoung other things, the ability to create new dicusssions on the discussion board itself should not be available.

I played around with alot of ideas, using security and disallowing everyone from creating new discussions, using CSS etc, but in the end the answer was surprisingly simple.

I found a javascript function on someone's blog (I'd credit him if I could find it again, too many blogs & Christmas & New Year in between). This javascript function, when added to master page, removed all New items from list menus that are document library types.

Ok, not exactly what I want, but at least a start. So I tried modding the javascript to work with my new feature off the masterpage. No luck. Then I played around with the CEWP, adding it to my forum. Worked like a charm, but I need to add alot of functionality besides this one and I could just extend the new template list with it's CEWP added.

Finally, the solution I found was: Add the javascript to the AllItems.aspx page of my forum, to the content area of the main place holder. Success!

This is the javascript:

function HideNewQADiscussionMenuItem()
var tables = document.getElementsByTagName("table");
for(var i=0; i<> 0 )
var elm = tables[i];"none";"none";

And here's how it fits in:

The last line is:
if(!document.all){ window.onload=HideNewQADiscussionMenuItem; }else{ HideNewQADiscussionMenuItem(); }

This makes sure the javascript is called when the page is loaded. And that removes the New fucntion from the top menu of my dicussion forum.

Step by Step:

  1. Create a new visual studio project, select List Definition from the SharePoint projects. Select the basic list to inherit from, in my case Discussions. This creates all ASPX files & XML files needed for a list definition.
  2. Open the AllItems.aspx file
  3. Find the PlaceHolderMain content tag.
  4. Below the web part zone, add the javascript. Remember the javascript tag & the line of code to make sure the javascript is called (I couldn't figure out why my script wasn't being called :P )
  5. Build & Deploy the solution (Visual Studio should have this option under the build menu, if it doesn't, you deploy the list definition as a feature).

And that's that. Your new list is ready for use. Of course, you now need some way to create new list items :)

No comments: