Thursday, October 16, 2008

improve speed of development for SharePoint (part2)

Stop continuously copying your files from your project directory to SharePoint 12

This is one of the chapter of the series Improve speed of development for SharePoint. You will find a complete summary in Improve speed of development for SharePoint Part(1).
When you are developping pages for SharePoint using Visual Studio, you always have to copy it somewhere in the SharePoint 12 directory in order to test it. It would be great if, while working on your project file, your code would be automaticaly copied to the operationnel file in the SharePoint 12 of your development machine when you type a Ctrl + S. So I did it.
Here is the VB .Net code for a Macro you can add to Visual Studio.
Prerequisites: As in a WSPBuilder project, create a 12 directory in your Visual Studio project, and respect the directories you will have in your post deployment environments.
This Macro will test if the file you are going to save is somewhere under the 12 directory, and if so, will save it in your project context of course, but will also copy it to the corresponding directory under the SharePoint 12 of your development machine.
How to install it? Create a Macro in visual studio then replace the module code by the following code, then attach the Ctrl + S Keyboard shorcut to it.
cf.: How to: Record Macros (MSDN)

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module Module1
    Sub SaveTo12()
        Dim myFile As String = DTE.ActiveDocument.Path
        Dim maTable() As String
        maTable = myFile.Split("\")
        'MsgBox("matebleLength : " & maTable.Length)
        Dim copyPath As String
        Dim is12Found As Boolean = False
        For i As Integer = 0 To maTable.Length - 2
            If maTable(i) = "12" Then
                is12Found = True
            End If
            If is12Found Then
                copyPath &= maTable(i) & "\"
            End If
        Next i
        Dim _12Path As String = "C:\Program Files\Common Files\Microsoft Shared\web server extensions\"
        copyPath = _12Path & copyPath
        If is12Found Then
            If Not System.IO.Directory.Exists(copyPath) Then
            End If

            System.IO.File.Copy(myFile & "\" & DTE.ActiveDocument.Name, copyPath & DTE.ActiveDocument.Name, True)
        End If
    End Sub

End Module

No comments: