Friday, January 21, 2011

Hiding Menu Items on a SharePoint List form

This might be a bit basic but I thought I would give a step by step guide on how to customise a SharePoint list form. The first step is you need a web control.

In your Visual Studio project you need a TEMPLATES folder and a CONTROLTEMPLATES folder beneath it. This is where you add your user control. Lets not worry too much about the code on the user control just yet.

Next we want to make the web control available when the SharePoint list form loads.
To do this add the line

<%@ Register TagPrefic="xyz" TagName="MyWebControl" src="~/_controltemplates/WebControl.ascx" %>

into the web page.
Now there are two ways to do this depending on what you want to achieve.

Firstly you can add the line into the master page so that is available to every page on the site.

Secondly you can add it to the custom page associated to the list.

In either event, the result is that the code on the web control is executed when your page loads. Since your web control is injected into the HTML of the page you can do all kindes of things from adding inline css styles that can be applied to the page or by injecting javascript onto the page to hide a menu or menu item.

One approach is to add an asp:panel onto the ASCX and embed your css styles between the start and end tags of the panel. You can also embed javascript to change the way the page is displayed.

Your code behind can then determine when to make the panel visible or hidden which will determine if the styles are applied to the page or not.

If you need to have more control over the javascript go to the code behind and use the RegisterClientScript() method or add a literal control and set the text property to the javascript function. When the literal control is rendered, the javascript is executed.

No comments: