MinimizeVisual Studio Automatic DotNetNuke Module Build

Well earlier I had said, “I will begin working at a faster pace.” So here it goes.

Sub DNN_Module_Step_001()

Dim myprojectname As String

' ***** Portal Module Creation *****

' Powerhouse Data VSM Module Creation Macro
' Gets a Qualifier
' Gets a Module Name
' Automatically runs the Shaun Walker DotNetNuke Template

' ***** Open an Output Window
MyOutput = AddOutputPane("DNN_Module_Step_001")
Print(MyOutput, " ")
Print(MyOutput, " ***** Powerhouse Data VSM *****")
Print(MyOutput, " ")
Print(MyOutput, "Powerhouse Data VSM > Beginning at " & Now())

' ***** Get Company Name
s.Length = 0 'clear the stringbuilder
s.Append("A Company Name is used as an added prefix to various stored procedures, table names, namespaces, etc. ")
s.Append(" Please enter a company name now (e.g. phd).")
s.Append(vbNewLine)
s.Append(vbNewLine)
s.Append("What do you want to use as a Company Name?")
MyCompanyName = InputBox(s.ToString, "Text used for Company Name", "phd", 350, 350)

' ***** Get Module Prefix
s.Length = 0 'clear the stringbuilder
s.Append("Qualifiers (prefixes) are necessary in order to prevent new modules from overwriting ")
s.Append("existing modules. Normally something such as your company name is used once again (e.g. phd). ")
s.Append("Or you can make it slightly different if you want (e.g. Powerhouse). ")
s.Append(vbNewLine)
s.Append(vbNewLine)
s.Append("What do you want to use as a Prefix?")
QualifierName = InputBox(s.ToString, "Text used for Prefix", "Powerhouse", 350, 350)

' ***** Get Title of Module
s.Length = 0 'clear the stringbuilder
s.Append(vbNewLine)
s.Append("What do you want to use as a Module Name?")
ModuleTitle = InputBox(s.ToString, "Text used for Module Title", "PHD", 350, 350)

' ***** Create Module Name from Prefix and Title concatenation
ModuleName = QualifierName & "_" & ModuleTitle ' Store complete module name here.
Print(MyOutput, "Powerhouse Data VSM > MyCompanyName = " & MyCompanyName)
Print(MyOutput, "Powerhouse Data VSM > QualifierName = " & QualifierName)
Print(MyOutput, "Powerhouse Data VSM > ModuleTitle= " & ModuleTitle)
Print(MyOutput, "Powerhouse Data VSM > ModuleName = " & ModuleName)
Print(MyOutput, "Powerhouse Data VSM > ")

' ***** List all the projects in the solution
' There should only be one project in VS 2005 DNN IDE
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
projs = DTE.ActiveSolutionProjects()
soln = DTE.Solution
proj = projs.GetValue(0)
For Each proj In projs
myprojectname = proj.Name.ToString() ' This is where your Portal resides.
Print(MyOutput, vbNewLine & " > Current Project = " & myprojectname)
Next

' ***** Get the path and store it in "ProjectPath"
Dim prop As [Property]
prop = proj.Properties.Item(14) ' Full Path
ProjectPath = prop.Value.ToString() ' Store Full Path here.
s.Length = 0 'clear the stringbuilder
s.Append(" > Actual ProjectPath = " & ProjectPath & vbNewLine)

' ***** Take a look at all the properties
For Each prop In proj.Properties
s.Append(vbNewLine " > " prop.Name " : " prop.Value.ToString())
Next prop
Print(MyOutput, s.ToString())

' ***** Get the DotNetNuke Custom Module Template Directory and copy it
Print(MyOutput, vbNewLine " > Directory Operations")
Dim DNNModuleDirectory, NewModuleDirectory As String
DNNModuleDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)
NewModuleDirectory = DNNModuleDirectory & "\Visual Studio 2005\" & ModuleName
DNNModuleDirectory = DNNModuleDirectory & "\Visual Studio 2005\DNNModule"

If System.IO.Directory.Exists(DNNModuleDirectory) Then
Print(MyOutput, " > DNNModule Directory was located at " & DNNModuleDirectory)
Try
If My.Computer.FileSystem.DirectoryExists(NewModuleDirectory) Then
My.Computer.FileSystem.DeleteDirectory(NewModuleDirectory, FileIO.DeleteDirectoryOption.DeleteAllContents)
End If
My.Computer.FileSystem.CopyDirectory(DNNModuleDirectory, NewModuleDirectory, True)
Print(MyOutput, " > New Template Directory created at " & NewModuleDirectory)
Catch ex As System.Exception
Print(MyOutput, " > Exception Caught = " & ex.Message)
End Try
Else
Print(MyOutput, " > DNNModule Directory was not located at " & DNNModuleDirectory)
Print(MyOutput, " > Please extract a copy of Module_VB.zip to this location & try again.")
Exit Sub
End If

' ***** Rename the two "ModuleName" directories inside both \App_Code and \DesktopModules
Dim RenameDirectory(2) As String
RenameDirectory(1) = NewModuleDirectory & "\App_Code\ModuleName"
RenameDirectory(2) = NewModuleDirectory & "\DesktopModules\ModuleName"
For i = 1 To 2
strtxt = Replace(RenameDirectory(i), "ModuleName", ModuleName)
Try
'SetAttr(NewModuleDirectory, FileAttribute.Normal)
My.Computer.FileSystem.RenameDirectory(RenameDirectory(i), ModuleName)
Print(MyOutput, " > Directory " & strtxt & " has been created.")
Catch ex As System.Exception
Print(MyOutput, " > Exception Caught = " & ex.Message)
End Try
Next

' ***** Rewrite the Documentation file
Dim DocumentFile As String
DocumentFile = NewModuleDirectory & "\DesktopModules\" & ModuleName & "\Documentation\Documentation.html"
Print(MyOutput, " > DocumentFile = " & DocumentFile)

s.Length = 0 'clear the stringbuilder
s.Append("" & vbNewLine)
s.Append("" & vbNewLine)
s.Append("" & vbNewLine)
s.Append(" " & vbNewLine)
s.Append("" & vbNewLine)
s.Append("" & vbNewLine)
s.Append("" & vbNewLine)

s.Append("

" & vbNewLine)
s.Append("" & vbNewLine)
s.Append(ModuleName & " has been created." & vbNewLine)
s.Append("
" & vbNewLine & "

" & vbNewLine & "

" & vbNewLine)

s.Append("
" & vbNewLine)
s.Append(myprojectname & "
" & vbNewLine)
s.Append(ProjectPath & "
" & vbNewLine)
s.Append(MyCompanyName & "
" & vbNewLine)
s.Append(QualifierName & "
" & vbNewLine)
s.Append(ModuleTitle & "
" & vbNewLine)
s.Append(ModuleName & "
" & vbNewLine)
s.Append(Now() & "
" & vbNewLine)
s.Append("
" & vbNewLine)

s.Append("
" & vbNewLine)
s.Append("VS Project:
" & vbNewLine)
s.Append("VS ProjectPath:
" & vbNewLine)
s.Append("MyCompanyName:
" & vbNewLine)
s.Append("QualifierName:
" & vbNewLine)
s.Append("ModuleTitle:
" & vbNewLine)
s.Append("ModuleName:
" & vbNewLine)
s.Append("Created:
" & vbNewLine)
s.Append("
" & vbNewLine)

s.Append("" & vbNewLine)
s.Append("")
s.Replace("Untitled Page", ModuleName)

My.Computer.FileSystem.WriteAllText(DocumentFile, s.ToString(), False) 'Rewrite the file

' ***** Move focus if it is not correct.....
DTE.ActiveWindow.Object.GetItem(myprojectname).Select(vsUISelectionType.vsUISelectionTypeSelect)

' ***** Get the newly copied DotNetNuke Module.vstemplate created by Shaun Walker
Dim myDNNTemplate As String
myDNNTemplate = NewModuleDirectory & "\DotNetNuke Module.vstemplate"

' ***** Customize this DotNetNuke Module.vstemplate
s.Length = 0 'clear the stringbuilder
s.Append(My.Computer.FileSystem.ReadAllText(myDNNTemplate))
s.Replace("ModuleName", ModuleName)
s.Replace("$fileinputname$", ModuleName)
My.Computer.FileSystem.WriteAllText(myDNNTemplate, s.ToString(), False)

' ***** Add in the customized DotNetNuke Template to our project
Try
proj.ProjectItems.AddFromTemplate(myDNNTemplate, ModuleName)
Catch ex As System.Exception
'Error HRESULT E_FAIL has been returned from a call to a COM component. ==> ignore this VS bug.
If ex.Message <> "Error HRESULT E_FAIL has been returned from a call to a COM component." Then
Print(MyOutput, " > Exception Caught = " & ex.Message)
End If
End Try

' ***** Replace all occurances of "YourCompany" with MyCompanyName string
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
DTE.ActiveWindow.Object.GetItem(myprojectname).Select(vsUISelectionType.vsUISelectionTypeSelect)
DTE.Find.FindWhat = "yourcompany"
DTE.Find.ReplaceWith = MyCompanyName
DTE.Find.Target = vsFindTarget.vsFindTargetCurrentProject
DTE.Find.MatchCase = False
DTE.Find.MatchWholeWord = False
DTE.Find.MatchInHiddenText = True
DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
DTE.Find.KeepModifiedDocumentsOpen = False
DTE.Find.FilesOfType = "*.*"
DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
DTE.Find.Execute()

DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
DTE.Windows.Item(Constants.vsWindowKindWebBrowser).Activate()
DTE.Windows.Item(Constants.vsWindowKindOutput).Activate()

' ***** Run SQL Database Statements
Print(MyOutput, " ")

Dim SQLPath, SQLFileName As String
Dim connectionString As String = GetMyConnectionString()
Dim command As System.Data.SqlClient.SqlCommand

Using connection As SqlClient.SqlConnection = New SqlClient.SqlConnection(connectionString)
connection.Open()

' Start a local transaction.
Dim sqlTran As SqlClient.SqlTransaction = connection.BeginTransaction()

' Enlist the command in the current transaction.
command = connection.CreateCommand()
command.Transaction = sqlTran

'Get Path to the SQL Files
SQLPath = ProjectPath & "\DesktopModules\" & ModuleName & "\"

Try
' Register the Module
SQLFileName = SQLPath & ModuleName & ".SqlDataProvider"
s.Length = 0 'clear the stringbuilder
s.Append(My.Computer.FileSystem.ReadAllText(SQLFileName))
s.Replace("{databaseOwner}", "")
s.Replace("{objectQualifier}", "")
command.CommandText = s.ToString
command.ExecuteNonQuery()

' Add Module Table
s.Length = 0 'clear the stringbuilder
s.Append("create table " & MyCompanyName & "_" & ModuleName & vbNewLine)
s.Append("(" & vbNewLine)
s.Append("[ModuleID] [int] NOT NULL," & vbNewLine)
s.Append("[ItemID] [int] NOT NULL IDENTITY(1, 1)," & vbNewLine)
s.Append("[Content] [ntext] NOT NULL," & vbNewLine)
s.Append("[CreatedByUser] [int] NOT NULL," & vbNewLine)
s.Append("[CreatedDate] [datetime] NOT NULL" & vbNewLine)
s.Append(")" & vbNewLine)
command.CommandText = s.ToString
command.ExecuteNonQuery()

strtxt = "ALTER TABLE [" & MyCompanyName & "_moduleidentity] ADD CONSTRAINT [PK_" & MyCompanyName & "_moduleidentity] PRIMARY KEY CLUSTERED ([ItemID])"
s.Length = 0 'clear the stringbuilder
s.Append(strtxt)
s.Replace("moduleidentity", ModuleName)
command.CommandText = s.ToString
command.ExecuteNonQuery()

strtxt = " CREATE NONCLUSTERED INDEX [IX_" & MyCompanyName & "_moduleidentity] ON [" & MyCompanyName & "_moduleidentity] ([ModuleID])"
s.Length = 0 'clear the stringbuilder
s.Append(strtxt)
s.Replace("moduleidentity", ModuleName)
command.CommandText = s.ToString
command.ExecuteNonQuery()

strtxt = "ALTER TABLE [" & MyCompanyName & "_moduleidentity] WITH NOCHECK ADD CONSTRAINT [FK_" & MyCompanyName & "_moduleidentity_Modules] FOREIGN KEY ([ModuleID]) REFERENCES [Modules] ([ModuleID]) ON DELETE CASCADE NOT FOR REPLICATION"
s.Length = 0 'clear the stringbuilder
s.Append(strtxt)

s.Replace("moduleidentity", ModuleName)
command.CommandText = s.ToString
command.ExecuteNonQuery()

' Create Stored Procedures

' Gets
s.Length = 0
s.AppendFormat("create procedure " & MyCompanyName & "_Getmoduleidentitys{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("{0}@ModuleId int{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("as{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("select ModuleId,{0}", Environment.NewLine)
s.AppendFormat(" ItemId,{0}", Environment.NewLine)
s.AppendFormat(" Content,{0}", Environment.NewLine)
s.AppendFormat(" CreatedByUser,{0}", Environment.NewLine)
s.AppendFormat(" CreatedDate,{0}", Environment.NewLine)
s.AppendFormat(" 'CreatedByUserName' = Users.FirstName ' ' ")
s.AppendFormat("Users.LastName{0}", Environment.NewLine)
s.AppendFormat("from " & MyCompanyName & "_moduleidentity{0}", Environment.NewLine)
s.AppendFormat("inner join Users on " & MyCompanyName & "_moduleidentity.CreatedByUser ")
s.AppendFormat("= Users.UserId{0}", Environment.NewLine)
s.AppendFormat("where ModuleId = @ModuleId")
s.Replace("moduleidentity", ModuleName)
command.CommandText = s.ToString
command.ExecuteNonQuery()

' Get
s.Length = 0
s.AppendFormat("create procedure " & MyCompanyName & "_Getmoduleidentity{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("{0}@ModuleId int,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat(" @ItemId int{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("as{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("select ModuleId,{0}", Environment.NewLine)
s.AppendFormat(" ItemId,{0}", Environment.NewLine)
s.AppendFormat(" Content,{0}", Environment.NewLine)
s.AppendFormat(" CreatedByUser,{0}", Environment.NewLine)
s.AppendFormat(" CreatedDate,{0}", Environment.NewLine)
s.AppendFormat(" 'CreatedByUserName' = Users.FirstName ' ' ")
s.AppendFormat("Users.LastName{0}", Environment.NewLine)
s.AppendFormat("from " & MyCompanyName & "_moduleidentity{0}", Environment.NewLine)
s.AppendFormat("inner join Users on " & MyCompanyName & "_moduleidentity.CreatedByUser ")
s.AppendFormat("= Users.UserId{0}", Environment.NewLine)
s.AppendFormat("where ModuleId = @ModuleId{0}", Environment.NewLine)
s.AppendFormat("and ItemId = @ItemId")
s.Replace("moduleidentity", ModuleName)
command.CommandText = s.ToString
command.ExecuteNonQuery()

' Add
s.Length = 0
s.AppendFormat("create procedure " & MyCompanyName & "_Addmoduleidentity{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("{0}@ModuleId int,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}@Content ntext,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}@UserID int{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("as{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("insert into " & MyCompanyName & "_moduleidentity ({0}", Environment.NewLine)
s.AppendFormat("{0}ModuleId,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}Content,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}CreatedByUser,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}CreatedDate{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat(") {0}", Environment.NewLine)
s.AppendFormat("values ({0}", Environment.NewLine)
s.AppendFormat("{0}@ModuleId,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}@Content,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}@UserID,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}getdate(){1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat(")")
s.Replace("moduleidentity", ModuleName)
command.CommandText = s.ToString
command.ExecuteNonQuery()

' Update
s.Length = 0
s.AppendFormat("create procedure " & MyCompanyName & "_Updatemoduleidentity{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("{0}@ModuleId int,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat(" @ItemId int,{0}", Environment.NewLine)
s.AppendFormat("{0}@Content ntext,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}@UserID int{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("as{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("update " & MyCompanyName & "_moduleidentity{0}", Environment.NewLine)
s.AppendFormat("set Content = @Content,{0}", Environment.NewLine)
s.AppendFormat(" CreatedByUser = @UserID,{0}", Environment.NewLine)
s.AppendFormat(" CreatedDate = getdate(){0}", Environment.NewLine)
s.AppendFormat("where ModuleId = @ModuleId{0}", Environment.NewLine)
s.AppendFormat("and ItemId = @ItemId")
s.Replace("moduleidentity", ModuleName)
command.CommandText = s.ToString
command.ExecuteNonQuery()

' Delete
s.Length = 0
s.AppendFormat("create procedure " & MyCompanyName & "_Deletemoduleidentity{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("{0}@ModuleId int,{1}", ControlChars.Tab, Environment.NewLine)
s.AppendFormat(" @ItemId int{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("as{0}", Environment.NewLine)
s.AppendFormat("{0}", Environment.NewLine)
s.AppendFormat("delete{0}", Environment.NewLine)
s.AppendFormat("from " & MyCompanyName & "_moduleidentity{0}", Environment.NewLine)
s.AppendFormat("where ModuleId = @ModuleId{0}", Environment.NewLine)
s.AppendFormat("and ItemId = @ItemId")
s.Replace("moduleidentity", ModuleName)
command.CommandText = s.ToString
command.ExecuteNonQuery()

sqlTran.Commit()
Print(MyOutput, " > SQL transactions have been run.")
Catch ex As Exception
Print(MyOutput, " > " & ex.Message)
Print(MyOutput, " > No SQL transactions have been run.")
sqlTran.Rollback()
End Try
connection.Close()
End Using

' ***** Cleanup and End
DTE.ExecuteCommand("File.SaveAll")
s.Length = 0 'clear the stringbuilder
s.Append(vbNewLine & " The initial Module Design for " & ModuleName & " has completed." & vbNewLine)
Print(MyOutput, s.ToString)
Try
If My.Computer.FileSystem.DirectoryExists(NewModuleDirectory) Then ' Delete temporary template directory
My.Computer.FileSystem.DeleteDirectory(NewModuleDirectory, FileIO.DeleteDirectoryOption.DeleteAllContents)
End If
Catch ex As System.Exception
Print(MyOutput, " > Exception Caught = " & ex.Message)
End Try
Print(MyOutput, vbNewLine " > End " & Now())

' ***** Automatically start the DotNetnuke Portal
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
DTE.ActiveWindow.Object.GetItem(myprojectname).Select(vsUISelectionType.vsUISelectionTypeSelect)
DTE.ActiveWindow.Object.GetItem(myprojectname & "\Default.aspx").Select(vsUISelectionType.vsUISelectionTypeSelect)
DTE.ExecuteCommand("File.ViewinBrowser")

End Sub

Function GetMyConnectionString() As String
Return "Data Source=.\SQLEXPRESS;AttachDbFilename=c:\inetpub\wwwroot\PHDnet1\App_Data\Database.mdf;" _
& "Integrated Security=True;User Instance=True"
End Function


If you are writing your own code to match mine, you must add a reference to system.data for your PHD_Macros Project. Otherwise, just download now (recommended). Open the DNN_Module_Step_001 macro and examine the contents. Here are the important comment lines summarized:

  1. ' ***** Open an Output Window
  2. ' ***** Enter Company Name
  3. ' ***** Enter Module Prefix
  4. ' ***** Enter Title of Module
  5. ' ***** Create Module Name from Prefix and Title concatenation
  6. ' ***** List all the projects in the solution
    1. ' There should only be one project in VS 2005 DNN IDE
  7. ' ***** Get the path and store it in "ProjectPath"
  8. ' ***** Take a look at all the properties
  9. ' ***** Get the DotNetNuke Custom Module Template Directory and copy it
  10. ' ***** Rename the two "ModuleName" directories inside both \App_Code and \DesktopModules
  11. ' ***** Rewrite the Documentation file
  12. ' ***** Move focus if it is not correct.....
  13. ' ***** Get the newly copied DotNetNuke Module.vstemplate created by Shaun Walker
  14. ' ***** Customize this DotNetNuke Module.vstemplate
  15. ' ***** Add in the customized DotNetNuke Template to our project
  16. ' ***** Replace all occurances of "YourCompany" string with Company Name
  17. ' ***** Build
  18. ' ***** Rewrite the Documentation.html file
  19. ' ***** Run SQL Database Statements
    1. ' Register the Module
    2. ' Add Module Tables and Stored Procedures
  20. ' ***** Automatically start the DotNetnuke Portal
  21. ' ***** Cleanup and End

When you are ready Double-Click the DNN_Module_Step_001 macro and enter your information as prompted (see comments above). You will automatically have your portal opened in a web browser. Sign in as “host”. Once you are logged in your new module will be available. Here is how the dropdown list of module names will appear if you’ve chosen the defaults:

screen8.jpg

Now you can very quickly add a DotNetNuke Module and delete it – then add it in once again if you wish.

How to Delete the Module
  • Log in as host
  • Delete it from any content panes.
  • Go to Module Definitions and Delete.
  • Return to Visual Studio
  • Delete the two directories having the “modulename” names in the Solution Explorer Window.

You can now build it once more by merely Double-clicking the DNN_Module_Step_001 macro a second time!

continued page 8.....

Print  

Friday, July 03, 2009

Copyright 2008 by Powerhouse Data   *  Terms Of Use  *  Privacy Statement