Sunday, February 27, 2011

Create Site Collections from Custom Site Templates

In SharePoint 2010 creating templates from sites is as simple as a few clicks. Using that template to create new site collections is a bit more involved. The standard approach goes something like this; download the newly created template, in central administration create a new site collection without specifying a template, navigate to the new site collection, when prompted for the site template upload your custom template. The problem with this approach it's too repetitive if you're going to create a lot of sites and site collections.

What I wanted to do was to select the template to create new sites and site collection just like the builtin ones. To do this you need to understand what's going on when you create a site template from the SharePoint interface. It's a solution file so we can rename it to a .cab file and take a look inside. Along with the manifest.xml file, there are folders for; ListInstances, Modules, PropertyBags, and WebTemplate. The webtemplate folder is the giveaway, what has been created for us is a definition for a webtemplate. The webtemplate folder contains a feature with a scope of "Site" that needs to be changed to a scope of "Farm".
That's it, this webtemplate can now be used to create site collections from central administration, after installation of coarse.

The steps; create the template, download the template, rename the template to .cab, extract the .cab file to a folder, modify the feature xml, convert folder back to .cab, rename to .wsp, add and deploy the solution. To convert the folder back to a cab file I used TUGZip.

Thursday, February 24, 2011

Disable ASP:Button onclick

A common requirement is to disable a button after it's been clicked. It can be accomplished by modifying two properties of the asp:button control. First, set the UseSubmitButton to "false". Second, modify the OnClientClick property to include "this.disabled=true;". These two settings will work in most cases. A problem will crop up if client side data validation is being used. The button will remain disabled and the user will not be able to click the button again. If you're using .Net validation controls you will need to check Page_ClientValidate() and then disable the button. 

<script type='text/javascript'>
   function disableBtn(control) {
     if (typeof Page_ClientValidate == 'function') {
        if (Page_ClientValidate()) { control.disabled = true; }
     { control.disabled = true; }

<asp:Button runat="server" ID="btnSubmit" OnClientClick="disableBtn(this);" UseSubmitBehavior="false" OnClick="btnSubmit_Click" Text="Submit" />