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”


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


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:


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


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


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

//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
//custom Logger a great example the can be found at 
 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.
//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");
                Console.WriteLine("build failed");
//remove loggers from the collection
//remove the projects from the projectcollection
//Dispose collection to prevent memory leaks
//Tell the logger to stop writing 
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


Name *:

CAPTCHA Image Validation