Friday, August 26, 2011

There are comments and then there are comments

The title may be somewhat vague, but in this post I want to alert you to the different type of comments you can use when working with ASP.NET. I have seen too many developers not understand the difference between

<!-- Comment-->
and
<%-- Comment --%>
The first type is an HTML comment which instructs the browser to ignore the contents. The second type is  an ASP.NET comment which instructs the ASP.NET parser to ignore the contents.

This is a very important distinction. The first type of comment means nothing to the ASP.NET parser, and is handled just like any other HTML in the page. That means that if I put something along the lines of:
<!-- <asp:Literal runat="server" Text="joe was here"/> -->
into my web page, the ASP.NET parser will stil parse this and execute the code in that literal. So my HTML will look something like
<!-- joe was here -->
When this becomes very important is at times where a developer thinks they have commented out some controls, but they haven't really, it's just that the output of that control isn't being displayed by the browser.

Imagine a line like:
<!-- <myControls:SomeVerySlowThing runat="server"/> -->
A novice developer may be under the impression that 'SomeVerySlowThing' has been commented out, but in reality, the code for that control is still being run.

Another time this is important is when trying to create lean and efficient HTML. Adding comments to HTML is handy, it is however still content being sent down the wire. If you are trying to make your HTML as lean as possible, use the ASP.NET comments and they will not show up in the end result HTML.

Wednesday, August 24, 2011

Free Brainstorming Session

Are you interested in what SharePoint is and what it could do for your business? XComplica is currently offering a free brainstorming session to help you answer these questions!

Visit www.xcomplica.com/Promo for more details.

Tuesday, August 23, 2011

Public facing sites using SharePoint 2010

This post is the first in a series that will walk you through building public facing sites in SharePoint 2010. I will discuss a method that I use for this process, whether or not it is the best method I leave up to you to decide.
In the past I used site definitions, but I recently decided to switch to the new web template method. In order to save myself and others time in the future, I have created a starter web template for public sites. You can download this at Codeplex: http://xcomplicaps.codeplex.com

I have to thank Mirjam van Olst and Vesa “vesku” Juvonen for their blog posts that were extremely helpful in putting this web template together.

http://www.sharepointchick.com/archive/2011/02/10/using-web-templates-to-create-site-collections.aspx

http://blogs.msdn.com/b/vesku/archive/2010/10/14/sharepoint-2010-and-web-templates.aspx

In this post, I will walk through the components in this web template, and how you can get started using it. Later posts will build on this one.

If you haven’t already, you may want to download the solution from Codeplex, and open it in Visual Studio.
Disclaimer: The solution is meant to be used on a development machine running SharePoint 2010. I also have the CKS Development tools installed and urge you to do the same.

You will see that it is a relatively small and simple solution containing one project. The project consists of a number of components, and we will go through each one.



Web Template (XComplicaPublicStarter)
There are two files that define the web template. There is the onet.xml file and the Elements.xml file. The Elements.xml file tells SharePoint about the web template and the onet.xml file defines the components of the web template. This means that any changes to the information about the web template, such as its name, description and display category should be changed in the element.xml file. This is similar to the webtemp_zzz.xml file for site definitions. The onet.xml file is a very stripped down version of the onet.xml used by the Publishing Portal site definition (BLANKINTERNET#53). Everything has been removed except for one configuration, and this configuration contains nothing except site level features and web level features. The features are the same as for the Publishing Portal, except there is one extra added at site scope and web scope, and these add the custom components we will be discussing later in the article. Two changes have been made to the web scoped publishing feature. The master page has been changed to a custom master page, and simple publishing has been turned on.

Three Features
There are three features as part of this project, one for farm scope, one for site scope and one for web scope.

The farm scoped feature has only one component, and that is the web template just discussed. It ensures that you can create a site collection based on this web template.

The site scoped feature contains elements that are global to the site collection as listed below. These will each be discussed in detail later:

• Master pages
• Page layouts
• Site columns
• Content types
• Global branding files

The web scoped feature contains elements that apply to the web itself and these will also be discussed in detail later:

• Content type binding
• Home Page
• Property Bag Setter

The web scoped feature also includes an event receiver. This event receiver enables anonymous access to the web. Since this is a public facing site, it is handy to have anonymous access automatically enabled on deployment.

Master pages
In the modules section, you will find a module called master pages. There are two master pages here, one is the very well known starter master page by Randy Drisgill, _starter_publishing.master.

http://blog.drisgill.com/2010/02/microsofts-sharepoint-2010-starter.html

The other master page, xcps_blank.master, is a stripped down version of Randy’s master page. This master page is what I use as a starter master page, and it has very little visible content. In this page I have commented out or removed as much of the html as possible, and I placed those placeholders that I could not remove into hidden panels. This technique is nothing new, I just took it further than others. There are only two content placeholders that are left in the visible portion of the page, the PlaceHolderMain and the PlaceHolderPageTitle. I assume these will be used on most if not all public facing sites.

I have also added a few links to content in the head section of the master page, since I assume every site these days will include at least one css file, one javascript and likely a favicon. These three branding files are located in the branding module and are meant to be changed when creating a new site.

I also add a reference to jQuery, but note that this is a reference to the CDN version of jQuery. There is no need to host this file locally.

As you develop your site, you will likely want to use one of the other placeholders or other components that are currently hidden. This is perfectly fine, just cut and paste them from the hidden panels into the appropriate location.

Note that I have also removed the ASP.NET form tag. I don’t see a need for it in a clean public facing site, but if you do find yourself needing it, feel free to add it back. There is a great article on how to make web parts work without the form tag on Waldek’s blog.

http://blog.mastykarz.nl/web-parts-in-content-with-master-pages-without-the-form-tag-no-problem/

Page Layouts
There is currently only one page layout in the project, and that is for the home page. It is very simple since this layout will differ for every site. I have added some very basic components in the PlaceHolderMain just to demonstrate how content can be rendered. There is a standard SharePoint Webcontrol for rendering html and there is a custom control that I developed for rendering Publishing Images. SharePoint provides a control called RichImageField http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.webcontrols.richimagefield.aspx that is meant for this, but I find it renders too much extra html around the image. I therefore created a simple server side control that renders just the img tag and some core attributes. You can see (and change) the code for this in the ServerControls folder of the project.

Site Columns (Fields)
Most public facing sites will contain at least a few different types of pages. These different types of pages should be represented by a number of content types, and content types are composed of fields. The StarterFields element contains one file which defines three site columns. One is a simple numeric site column used for ordering pages and the other two are Publishing site columns used to hold the html and image that the home page renders. You will add fields to this file, or create a separate element for your fields as you need them.

Content Types
Two content types are defined in the project. The generic content type is meant as a base type for all pages. It inherits from the built-in Page content type and it contains any fields that are common to all pages in the site. As it is, it contains one field which is the Sort Order. I find that a common request from authors somewhere down the line is to be able to order pages in some custom order, and so I always ensure this field is in all page content types.

The other content type is for the home page. It inherits from the generic content type and contains two fields as a sample. These are the Main Image and Body Text fields. Any site you build from this template will likely have different needs, so this will change. Note that I strongly advise using a separate content type and page layout for the home page and not share these with other pages. The home page of any site tends to be a very special page that evolves and changes in a different fashion from the rest of the site.

Global branding files
Every public facing site will need some custom CSS, JavaScript and probably a favicon. I have created a module to contain these files, and they are deployed into the Style Library. There are a number of discussions out there regarding the use of the Style Library, read Andrew Connell’s article for a good introduction.

http://msdn.microsoft.com/en-us/library/dd221375.aspx

Note that I have referenced the CSS, JavaScript and favicon files in the master page.

Content Type binding
Every web that has the publishing features enabled has a Pages library. If we want to be able to create pages based on the content types we define, we need to bind these content types to that Pages library. There is an element file in the Content Types folder that takes care of this, just remember to add a reference in that file to any new content types that you create.

Home Page
The home page is an instantiation of the home page layout, the home page content type and some actual content. The home page module is where this is all put together. The aspx page itself just has a reference telling SharePoint to use a page layout, this is the same for every instance of a publishing page. The interesting stuff is in the elements file, which defines the properties of the page, as well as the location (Pages List) that the page should be deployed to. Notice that the properties include the page layout, the content type, but also properties like Title, Body Text, etc. Also note that the MainImage property has been specified and contains a link to an image from teh style library. That image is provisioned as part of the branding module. In a different post I will show how to provision actual content into image libraries. For most pages we would allow authors to add the content, but for some pages as well as for test purposes it can be useful to prefill some content in.

Property Bag Setter
Due to the way web templates work, there is no way to know what web template was applied once the provisioning of the site is finished. It is therefore best practice to add a property in the web’s property bag in order to store this information. See the links above about web templates for more info on this.

Getting it all working
Now that you’ve had a full tour of the project, you should be able to create a site based on this template. You first need to specify a Site URL in the project properties so that it points to a valid SharePoint URL. What is somewhat annoying here is that we will be creating a new site collection based on this web template, but there already needs to be a site collection at the Site URL you specify. I often just start by creating a blank site at that URL, since I know I will be deleting it right after deployment. There are some PowerShell shortcuts you can also take, but I’ll leave those for another time.

Once your project has deployed, go to central administration and open up the page for creating site collections. You should see a new tab, and in that tab the Public Starter template.

Before you create a new site collection, I suggest you delete the blank site that you needed to deploy the project. This is up to you, but if you do so, you can create your new site collection at the root URL.
From this point, it is the same as creating any out-of-the box site collection. Once the process completes, you should be greeted with a new site based on the components discussed. Just for fun, look at the source and the page weight of your new site, and compare this to the regular SharePoint publishing template.

Conclusion
In this article I walked through the different pieces that make up the XComplica Public Starter web template. The purpose of the template is to create a clean starting point for creating lean and clean HTML sites using SharePoint. In future articles I will expand on how to create different types of pages, how to implement custom html into this template and other topics.

Tuesday, August 16, 2011

SPSTC Slides

I spoke at SharePoint Saturday The Conference and as promised, here are the slides for both sessions and the transcript of the powershell presentation.

I also promised the code for the branded site, I will clean this up and post on codeplex in the near future. I will also write some articles on the process of creating such a site, so keep an eye on this blog.

Slides of Branded Site talk
Slides of Powershell talk
Transcript of Powershell talk