Geeks With Blogs
Marc Schuricht Killer Cubes

More on MOSS...

In order to build our MOSS application we have decided to make the application as loosly-coupled from SharePoint as possible primarily because of the performance problems trying to develop on a Virtual Machine.  So, how do we do it most effectively?  With the help of some sound design patterns and a little PowerShell, it becomes pretty simple.  Let's begin with the design pattern.

First we need to create a standard ASP.NET website project. 

Then we need to create a "SolutionBuilder" project which is a standard Class Library project.

On the property window of each of the two projects we need to make the default namespace to be identical.  We'll try Chadster.SharePoint.

At this point neither project has a reference to each other.  Now on the web site we'll create a class called ChadsterBasePage.  Its implementation is pretty simple:

using System.Web.UI;

 

namespace Chadster.SharePoint

{

    public class ChadsterBasePage : Page

    {

        public ChadsterBasePage()

        {

            //

            // TODO: Add constructor logic here

            //

        }

    }

}

Now, on in the SolutionBuilder Project, we need to add references to Microsoft.SharePoint.dll and System.Web.dll and we need to create the same class as above:

using Microsoft.SharePoint.WebControls;

 

namespace Chadster.SharePoint

{

    public class ChadsterBasePage : LayoutsPageBase

    {

        public ChadsterBasePage()

        {

            //

            // TODO: Add constructor logic here

            //

        }

    }

}

The obvious difference is that our SolutionBuilder inherits LayoutsPageBase (so that we can deploy as an application page.)  In our base pages, we can now expose properties that could be exposed using the SharePoint API such as site title or site Id, etc.

Now, in the website project, we can create as many pages as we like all inheriting from our base page.  Here is where the magic of powershell comes in.  We can write a simple script to copy template files and *.cs files to appropriate locations within the "SolutionBuilder" project to be build into a dll.

In addition, the PowerShell script will also modify our manifest.xml file so that when we build the WSP file as described HERE.

The PowerShell Script looks like this:

$solutionBuilder = "C:\Safe\SolutionBuilder"

$12HiveLayouts = "$solutionBuilder\TEMPLATE\LAYOUTS"

$12HiveLayoutsGraphics = "$12HiveLayouts\[ProjectName]"

$manifestFile = "$solutionBuilder\manifest.xml"

$csFolder = "$solutionBuilder\[ProjectName]"

$asciiEncoding = "ascii"

 

Get-ChildItem  $csFolder | Foreach-Object { Remove-Item $_.FullName -force -recurse }

Get-ChildItem  $12HiveLayoutsGraphics | Foreach-Object { Remove-Item $_.FullName -force -recurse }

 

 

Get-ChildItem C:\Safe\Website\Graphics\*.cs | ForEach-Object { Copy-Item $_.FullName $csFolder }

 

Get-ChildItem C:\Safe\Website\*.ascx | ForEach-Object { Copy-Item $_.FullName $12HiveLayoutsGraphics -force }

Get-ChildItem C:\Safe\Website\*.aspx | ForEach-Object { Copy-Item $_.FullName $12HiveLayoutsGraphics -force }

 

[xml]$manifest = Get-Content $manifestFile

$templateFiles = $manifest.Solution.TemplateFiles

$templateFiles.RemoveAll()

 

Get-ChildItem $12HiveLayoutsGraphics | Foreach-Object {

      $templateFile = $manifest.CreateElement("TemplateFile")

      $templateFiles.AppendChild($templateFile)

      $templateFile.SetAttribute("Location", "LAYOUTS\[ProjectName]\$($_.Name)")

}

 

$manifest | Format-Xml  | Out-File $manifestFile -encoding $asciiEncoding

 

$manifestText = get-content $manifestFile

$manifestText -replace 'xmlns=""',''  | set-content $manifestFile

 

 Enjoy!

 

Posted on Wednesday, December 12, 2007 10:48 PM Emerging Technologies , MOSS 2007 Application Development , Powershell | Back to top


Comments on this post: What about PowerShell?

# re: What about PowerShell?
Requesting Gravatar...
Cool stuff. The script is very readable.

What are you using for your format-xml cmdlet?

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Left by Jeffrey Snover on Dec 13, 2007 7:39 AM

# re: What about PowerShell?
Requesting Gravatar...
Jeffrey,

the format-xml cmdlet comes from the Powershell Community Extensions:

http://www.codeplex.com/PowerShellCX
Left by Matt on Jan 08, 2008 9:56 AM

Your comment:
 (will show your gravatar)


Copyright © Marc Schuricht | Powered by: GeeksWithBlogs.net