Programmatically build SharePoint projects   

Tags: SharePoint 2010
Technorati Tags:

 .NET4.0 makes it possible to build SharePoint 2010 projects programmatically. 
I experimenting  in developing a tool to create installation scripts automatically. By adding SharePoint projects, my tool will build the project and get the wsp file needed to install your SharePoint project on the farm.

When I build my project, it has to a build and package the project. So first step is open an existing SharePoint project. When the project is loaded we are going to unload the project in visual studio.

The preparation

Right click on the project and then select “Unload Project”
UnloadProject

 

Right click on the unload project and select “Edit YourProjectName.csproj”
EditProject

 

An xml file will open in visual studio. In the xml file search for

Insert this part of xml beneath the part we searched for:

    $(BuildDependsOn);CreatePackage

When you have done that you will get the following peace of xml:


  $(BuildDependsOn);CreatePackage

Save the file and reload the project back to the solution. Right click on the unloaded solution and click Reload Project
ReloadProject

 

The code

Open a new console application and the framework has to be .NET4.0. Add the following reference: “Microsoft.Build” and put the following using in your code:

Using Microsoft.Build.Evaluation

To load our project we have to use the class ProjectCollection. With ProjectCollection we can load multiple projects that need to build, but in this example where going to use one project.

You also need a log class and a great example can be found at http://msdn.microsoft.com/en-us/library/microsoft.build.framework.ilogger.aspx

//In collection we are going to load our projects
 ProjectCollection collection = new ProjectCollection();
 
//Load a project in the collection
//parameter needed : path to project file
collection.LoadProject(@"PathToYourProject.csproj");
 
//custom Logger a great example the can be found at 
//http://msdn.microsoft.com/en-us/library/microsoft.build.framework.ilogger.aspx
 Logging logger = new Logging();
 
//give path where you can find the log file. If the log file doesn't exists the file will be created
logger.Parameters = @"c:\logging.txt";
 
//Add the logger to the collection so it can write to the log file.
collection.Loggers.Add(logger);
 
//Looping through the added projects
foreach (Project item in collection.LoadedProjects)
{
         //Building project and return true or false as success status
         bool success = item.Build();
               
          if (success)
          {
                Console.WriteLine("build completed");
           }
           else
           {
                Console.WriteLine("build failed");
            }
}
 
//remove loggers from the collection
collection.UnregisterAllLoggers();
 
//remove the projects from the projectcollection
collection.UnloadAllProjects();
 
//Dispose collection to prevent memory leaks
collection.Dispose();
            
//Tell the logger to stop writing 
logger.Shutdown();
 
Posted by  Gilissen Timmy  on  3/31/2011
0  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:        
 

Links to this post

Comments

Name *:
URL:
Email:
Comments:


CAPTCHA Image Validation