One of the features you probably want to give to your users, whether your solution is hosted in a cloud or on premises is the ability to create SharePoint sub sites based on defined template and features in it.
One of the easiest ways to create site template with custom features, is to use existing template available through your cloud provider and extend it with your customizations. This way you know you’re not building something that potentially has dependencies on functionality not available to you.
Let’s take a look at how we can extend an existing cloud hosted team site template and attach an event receiver to the announcements list of the customized version of the team site.
1. Create an instance of the site using a template you would like to extend.
2. Click Site Actions -> Site Settings -> Save site as template
3. Provide the template name and the file name. Remember the template name will be displayed to the user.
4. When saved, you will be given a link to the solution gallery http://[site url]/_catalogs/solutions
5. Download the file from the solution gallery to disk.
Now that we have the solution file we will extend it in Visual Studio:
1. Create new Visual Studio SharePoint 2010 project of type: Import SharePoint Solution Package
2. Accept all defaults and select the downloaded file for the template
3. After the import has completed, we’ll pick the announcements list and add a new project item to the Announcements folder of type Event Receiver
4. In my case the only logic is going to be in the ItemAdding event looking like this:
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
properties.AfterProperties["Title"] +=
" " + DateTime.Now.ToShortTimeString();
}
5. Ensure in the Elements.xml of your receiver you’re binding to the right item type, in our case announcements type:
<Receivers ListTemplateId=”104″>
….
6. Trigger the Deploy of the project to generate new WSP file
7. Upload the new file to the solutions gallery of your cloud hosted site collection:
http://[site url]/_catalogs/solutions
8. Activate the solution.
Now the new site template with our custom logic will be available for the users in the Create new site user interface.
In our case, the only custom logic we added is to trigger addition of current time when the new announcement item is added in the instance of the web based of our template. However, you can add new items, lists, or most of any other custom logic you would expect to do in the custom site template.
Give it a try!
