woensdag 10 februari 2010

Customizing SharePoint list toolbars

Many clients ask for custom toolbars where for example, the new button is hidden so users can only upload files. In this post I will explain you how to do this if you're able to start from a point where you can still use list definitions.
If , for any reason, you need to do this with already existing libraries, this post will be no good for you but don't worry, because in some of the next posts, I will explain a cool method to do this as well.

1. Create a list definition to start from
First we need some kind of list definition to start from. Most of the time I use the SharePoint Solution Generator to generate a solution with the list definition files in it. So just create your list in the GUI and create a list definition with this tool.

2. Discovering the templates
All templates for rendering the toolbars found in any list in SharePoint, can be found in the defaulttemplates.aspx file which is located in the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES folder.
To check which template your list definition is using, search for the property "ToolbarTemplate".

3. Creating your own control template
  1. Copy the defaulttemplates.ascx and place it in the same location C:\Program Files\Common Files\Microsoft Shared\webserverextensions\12\TEMPLATE\CONTROLTEMPLATES. You can give any custom name to the ascx file.
  2. Remove all the rendering templates from the custom defaulttemplates.ascx except the “DocumentLibraryViewToolbar”. Change the ID to a custom name like “MyCustomViewToolbar”.
  3. Remove the "SharePoint: UploadMenu" control rendering from the template and save the custom defaulttemplates.ascx. Of course you can delete other control renderings if you want to hide other buttons as well.
  4. Open the schema.xml file of your list definition you just created and in the default view (BaseViewId=1) add the “ToolbarTemplate” attribute which mentions your custom rendering template created without the upload menu. This XML snippet could look something like this:

    <View BaseViewID="1" Type="HTML" WebPartZoneID="Main" ToolbarTemplate="MyCustomViewToolbar" DisplayName="$Resources:core,All_Documents;" DefaultView="TRUE" MobileView="True" MobileDefaultView="True" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/dlicon.png" Url="Forms/AllItems.aspx"><!-- _locID@DisplayName="camlidDc1" _locComment=" " -->
  5. Save your changes, install and activate the feature for your custom list defintion and you're good to go. If all goes well you will see your template end up in the collection of possible list templates when you want to create a new list.

dinsdag 9 februari 2010