Creating our own Visual Studio 2005 Templates for MCMS

From ThemesWiki

Jump to: navigation, search
Applying Theme to an MCMS Site
Official Page
Project Documentation
Download
Source Book
Enhancing Microsoft Content Management Server with ASP.NET 2.0
Enhancing Microsoft Content Management Server with ASP.NET 2.0
ISBN 978-1-904811-52-7
Publisher Packt Publishing
Author(s) Lim Mei Ying, Spencer Harbar, Stefan Goßner

Contents

[edit] Getting Started with the Development Environment

Before proceeding, we will spend some time getting familiar with the MCMS Service Pack 2 development environment for Visual Studio 2005, which is slightly different from what we are used to with previous versions of Visual Studio. In addition we will create custom Visual Studio 2005 templates to overcome some of the issues that are present with the templates that shipped with MCMS SP2.

[edit] Visual Web Developer Websites

The key difference between developing MCMS applications with Visual Studio .NET 2003 and Visual Studio 2005 is that ASP.NET applications (and therefore MCMS applications) are now built using the Visual Web Developer component of Visual Studio 2005. Visual Web Developer introduces a new "project system", which no longer uses the project (*.csproj) files and simply accesses web applications via HTTP or the file system.

In Visual Studio .NET 2003, MCMS applications were created by choosing the MCMS Web Application project type. This project type was effectively a regular ASP.NET web application project with some modifications required by MCMS, such as additional references, the web authoring console, and modifications to the web.config. In Visual Studio 2005, developing web applications has been separated from developing other project types. The feature to develop a web application has been moved into the Visual Web Developer component.

To reflect this design change, you are no longer using New Project but New Web Site from the File menu in Visual Studio 2005 to create a new website.

Visual Studio 2005 ships with several website templates. The installation of the developer tools for MCMS extends the list of website templates with three additional templates: MCMS Empty Web Project, MCMS Web Application, and MCMS Web Service. These templates are actually modified versions of the similarly named standard templates shipped with Visual Studio 2005.

[edit] Creating an MCMS Web Application

Let's create an MCMS web application using Visual Studio 2005.

  1. Open Visual Studio 2005.
  2. From the File menu, choose New, followed by Web Site
  3. In the New Web Site dialog, select the MCMS Web Application within the My Templates section. Note- If the MCMS Web Application template does not appear in the My Templates section, the MCMS Visual Studio 2005 templates have not been correctly installed.
  4. In the Location combo box, select HTTP, and in the textbox, enter http://localhost/mcmstest . Note- MCMS applications have to be created using a local installation of IIS and do not support being created using the file system, which makes use of the built-in Visual Web Developer Web Server.Note that the New Web Site wizard will not prevent you from configuring an invalid website using the File System and Visual Web Developer Web Server.
  5. In the Language combo box (shown in the following figure), select Visual C#, and click OK.
  6. Visual Studio 2005 will create your project and initialize the MCMS Template Explorer. When it's done, you will be presented with an MCMS website with the basic foundation files.
  7. Click the MCMS Template Explorer tab at the bottom of the Solution Explorer, and note that the Template Gallery is accessible.

    Note - If you don't see the Template Explorer, it is likely you didn't select HTTP in the Location combo box in step 4. You may also not see the Template Explorer if you are using a locale other than US English, in which case you need to install hotfix 914195.

  8. Click the Solution Explorer tab at the bottom of the MCMS Template Explorer, and click the Refresh button. Notice that unlike the web applications from ASP.NET 1.x days, the 'CMS' virtual directory is now part of the website.
  9. If you examine the contents of the website, its references, and web.config file, you will see that the necessary MCMS files and configuration changes have been added.

[edit] Checking the Website Configuration Settings in IIS

We can verify that Visual Studio 2005 has configured the MCMS application correctly by using the Internet Information Services snap-in. First, let's ensure that the mcmstest website is indeed running on ASP.NET 2.0.

  1. From the Start Menu click Run, enter inetmgr in the Run textbox, and click OK.
  2. In Internet Information Services, expand the tree view to display the mcmstest application.
  3. Right-click the mcmstest application and click Properties.
  4. Click the ASP.NET tab and note that the ASP.NET version is correctly configured as 2.0.50727.
.

Note - When developing on Windows Server 2003, the Virtual Website root must run in the same worker process (i.e. Application Pool) as all MCMS applications so that the MCMS ISAPI Filter can work as expected. This filter cannot route requests across worker-process boundaries. In effect this means that all MCMS applications will share the same ASP.NET version, as ASP.NET does not support side-by-side execution of different versions inside the same worker process. This is not necessary with IIS on Windows XP as it does not use Worker Process Isolation mode.

Next, we will check the authentication settings. For now, we will configure the website to use integrated Windows authentication. Only users with a domain or local user account will have access to the site.

  1. Click the Directory Security tab followed by the Edit... button, and note that the permissions are correctly inherited from the Virtual Web Site settings. In this example, we will use integrated Windows authentication.

    Note Assuming that the Virtual Web Site to use Windows authentication is already configured. Authentication methods can be configured on a per-application basis.

  2. Click Cancel and close Internet Information Services.

[edit] Developing MCMS Web Applications

We are now ready to get started on developing our ASP.NET 2.0-based MCMS applications. There are a number of quirks with the MCMS web application templates, which we need to bear in mind during development.

  1. Switch back to Visual Studio 2005.
  2. In Solution Explorer, right-click on the website http://localhost/mcmstest , and click New Folder.
  3. Enter Templates as the folder name.
  4. Right-click the Templates folder and click Add New Item
  5. In the Add New Item dialog, select the MCMS Template File item and enter Basic.aspx in the Name textbox. Click Add.
  6. The new Basic.aspx template file is created and opened in Source View.
  7. Examine the contents of Basic.aspx.

[edit] Correcting Basic.aspx

Notice that the Basic.aspx file has a few problems. Some elements are highlighted by IntelliSense "squiggles", and if we attempt to build the website, a number of errors will prevent a successful build. Let's correct the Basic.aspx template file.

  1. In the CodeFile attribute of the Page directive on line one, replace CodeFile="~/Basic.aspx.cs" with CodeFile="basic.aspx.cs" Note -The MCMS Web Application New Item template doesn't recognize that our new template file has been created in a subdirectory, and therefore the CodeFile attribute is incorrect. New templates in the web root are not affected.
  2. From the Build menu, choose Build Web Site. Notice that the website now builds, but still includes a number of errors.
  3. Correct the DOCTYPE. On line 19, replace
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> 

    with

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">.
    
  4. Correct the <html> element. On line 20, replace <html> with <html xmlns="http://www.w3.org/1999/xhtml">.
  5. Delete the comments on lines 4 through 17. Note - The comments within an inline ASP script block (<% %>) are unnecessary.
  6. Delete the <meta> tags on lines 10 through 13.
      <meta name="GENERATOR" content="Microsoft Visual Studio .NET 8.0">
      <meta name="CODE_LANGUAGE" content="C#">
      <meta name="vs_defaultClientScript" content="JavaScript">
      <meta name="vs_targetSchema"
      content="http://schemas.microsoft.com/intellisense/ie5">
    
  7. Correct the WebControls Register directive. On line 2, replace:
      <%@ Register TagPrefix="cms" Namespace="Microsoft\ ContentManagement.WebControls" 
       Assembly="Microsoft. ContentManagement.WebControls"%>
    

    with

      <%@ Register Assembly="Microsoft.ContentManagement.WebControls, Version=5.0.1200.0,   
      Culture=neutral, PublicKeyToken=31bf385 6ad364e35" 
      Namespace="Microsoft.ContentManagement.WebControls" TagPrefix="cms"%>
    
  8. From the Build menu, choose Build Web Site. Notice that the website now builds free of any errors and that the cms tag prefix is understood.
  9. Your template file should now be as follows:
      <%@ Page language="c#" AutoEventWireup="false" CodeFile="Basic. aspx.cs" Inherits="Basic.Basic"%>
      <%@ Register Assembly="Microsoft.ContentManagement.WebControls, Version=5.0.1200.0,                                         
      Culture=neutral, PublicKeyToken= 31bf3856ad364e35"                                                                   
      Namespace="Microsoft.ContentManagement.WebControls" TagPrefix="cms"%>
     
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
     
      <head>
      <title>Basic</title>
      <cms:RobotMetaTag runat="server"></cms:RobotMetaTag>
     
      </head>
     
      <body>
      <form id="Form1" method="post" runat="server">
      </form>
      </body>
     </html>
    

[edit] Configuring the 'CMS' Virtual Directory

You may have noticed during the previous example that it takes quite some time to build the website. This is primarily due to the fact that the contents of the 'CMS' Virtual Directory are included within the project. These files provide, among other things, the Web Author functionality, and are required by MCMS applications. However, their inclusion in the project causes some problems, as the files themselves do not need rebuilding. In addition, these files will generate a large number of warnings, and in some cases when additional elements are installed (such as the excellent Telerik r.a.d. MCMS Controls Suite), prevent a successful build. Let's configure the 'CMS' Virtual Directory so that it is ignored by the Visual Studio 2005 when building the website.

  1. Open Windows Explorer.
  2. Navigate to C:\Program Files\Microsoft Content Management Server\Server\IIS_CMS.
  3. Press Ctrl+A to select all four folders, and press Alt+Enter.
  4. On the Properties dialog, click the Hidden checkbox.
  5. Click OK, and on the Confirm Attribute Change dialog, select the Apply changes to the selected items only radio button and click OK.
  6. Close Windows Explorer. Visual Studio will now ignore the content of the 'CMS' Virtual Directory when building websites, but the files will still be available to our MCMS applications. Remember to set the Hidden flag to all folders in the IIS_CMS directory if you install other third-party components into this folder in the future.

[edit] Creating Custom MCMS Application Templates

In order to save ourselves from the trouble of correcting the template file in the future, we shall create a new item template based on our corrected template. Start by checking if the Export Template item is contained within the File menu. If Export Template does not appear in the File menu, a common issue with Visual Studio 2005, take the following steps:

  1. From the Tools menu, choose Customize. In the Customize dialog, click the Commands tab.
  2. Select the File Category and scroll the Commands list box to locate Export Template
  3. Drag the Export Template item onto the File menu.
  4. Close the Customize dialog.

Now we can go ahead and create our custom template.

  1. From the File menu choose Export Template
  2. On the Choose Template Type page, select the Item template radio button, and click Next.
  3. On the Select Item To Export page, select the Basic.aspx checkbox, and click Next.
  4. On the Select Item References page, click Next.
  5. On the Select Template Options page, enter MCMS SP2 Template File in the Template name textbox, and a description (if you wish) in the Template description textbox. Deselect the Display an explorer window on the output files folder checkbox, and click Finish.
  6. The new item template is created.
  7. From the Solution Explorer in the Templates folder, right-click the Basic.aspx file and click Delete, followed by OK.
  8. From the Solution Explorer in the Templates folder, right-click the Basic.aspx.resx file and click Delete, followed by OK.
  9. Right-click the Template folder and click Add New Item
  10. In the Add New Item dialog, select the MCMS SP2 Template File item, enter Basic.aspx in the Name textbox, and click Add.
  11. From the Build menu, choose Build Web Site. Note that the website build succeeds.
  12. From the Solution Explorer, double-click the web.config file. Notice that the <compilers> element is highlighted with a warning by IntelliSense.
  13. Delete lines 24 through 26, and save the web.config file. <compilers> . . . </compilers>

Now that we have corrected all the problems with our MCMS SP2 application, let's build a Visual Studio 2005 project template.

  1. From the File menu choose Export Template
  2. On the Choose Template Type page, select the Project template radio button, and click Next.
  3. On the Select Template Options page, enter MCMS SP2 Web Application in the Template name textbox, and a description in the Template description textbox. Deselect the Automatically import the template into Visual Studio checkbox, and click Finish.
  4. Switch to and close Visual Studio 2005.
  5. In the Windows Explorer window opened by Visual Studio 2005, right-click the MCMS SP2 Web Application.zip file and click Extract All
  6. In the Extraction Wizard, click Next, followed by Next.
  7. Deselect the Show extracted files checkbox and click Finish.
  8. Delete the MCMS SP2 Web Application.zip file.
  9. Double-click the MCMS SP2 Web Application folder.
  10. Open the vwd.WebInfo file with Notepad.
  11. Replace <UserProperties CmsEnabled="1"/> with <UserProperties CmsEnabled="2"/>. Note - This setting allows Visual Studio 2005 to create the 'CMS' virtual directory when our application is created. It then sets the CmsEnabled attribute to 1. It may not be necessary to change this attribute if it is already set to CmsEnabled="2".
  12. Save and close vwd.WebInfo.
  13. Open the MyTemplate.vstemplate file in Notepad.
  14. Delete line 17: <Folder Name="CMS" TargetFolderName="CMS"/> Note - The export-template wizard automatically includes the contents of the 'CMS' virtual directory as part of the template; this step removes these files from the template.
  15. Save and close MyTemplate.vstemplate.
  16. Select all files in the MCMS SP2 Web Application folder.
  17. Right-click the selection and click Send To, followed by Compressed (zipped) Folder.
  18. Rename the ZIP file MCMS SP2 Web Application.zip.
  19. Right-click the MCMS SP2 Web Application.zip file and click Cut.
  20. Navigate to the My Documents\Visual Studio 2005\Templates\ProjectTemplates folder.
  21. From the Edit menu choose Paste.
  22. Delete the My Documents\Visual Studio 2005\My Exported Templates\MCMS SP2 Web Application folder.
  23. Open a Visual Studio 2005 command prompt.
  24. Execute the command devenv /setup and close the command prompt.

We have corrected the MCMS item and project templates which shipped with SP2, and are ready to create a simple test MCMS application based on our custom templates.

  1. Open Visual Studio 2005.
  2. From the File menu, choose New, followed by Web Site
  3. In the New Web Site dialog, select the MCMS SP2 Web Application item, select HTTP in the Location combo box, enter http://localhost/mcmssp2 in the textbox, and click OK.
  4. Open the MCMS Template Explorer using View | Other Windows | MCMS Template Explorer.
  5. Right-click the Templates folder and click New Template.
  6. Name the template Basic.
  7. In the Properties window, click the ellipsis ( ) button on the right of the TemplateFile property. If you do not see the Properties window, right-click the template and choose Properties.
  8. In the Select File dialog, select the Basic.aspx file inside the Templates folder, and click Select.
  9. Click the ellipsis ( ) button on the right of the PlaceholderDefinitions property.
  10. In the Placeholder Definition Collection Editor dialog, click Add, followed by OK.
  11. Click the Save icon at the top of the Template Explorer and click the Solution Explorer tab.
  12. Double-click the Basic.aspx file inside the Templates folder and click the Design button at the bottom of the code editor.
  13. Drag and drop DefaultConsole.ascx inside the Console folder from the Solution Explorer onto the design surface.
  14. From the Content Management Server toolbox, drag and drop an HtmlPlaceholderControl onto the design surface.
  15. Click the arrow on the PlaceholderToBind property within the Properties window, and select NewHtmlPlaceholderDefinition1.
  16. From the Build menu, choose Build Web Site.
  17. From the Tools menu, choose Content Management Server followed by Web Author. Internet Explorer will open at http://localhost/channels .
  18. Click Switch To Edit Site.
  19. Click Create New Page.
  20. In the Template Browser, click Templates followed by the select icon for the Basic template. The new posting opens in Edit mode.
  21. Enter some text into the HTML placeholder and click Save New Page.
  22. In the Save New Page dialog, enter test as the Name and click OK.
  23. Click Approve followed by Switch To Live Site. Note that the text entered in the placeholder is saved in a new posting.

We have now tested our custom MCMS web application project templates. Before moving on, it is a good idea to tidy up our test files and projects. We can delete our test posting, its template gallery item, and the two MCMS applications we have created: mcmstest and mcmssp2.

[edit] Source

The source of this content is 2-Creating Custom MCMS Application Templates of Enhancing Microsoft Content Management Server with ASP.NET 2.0 by Lim Mei Ying, Spencer Harbar, Stefan Goßner ( Packt Publishing, 2006).