February 28, 2020
Hot Topics:

Creating Solutions for SharePoint 2007

  • By Gustavo Velez
  • Send Email »
  • More Articles »

Using Solutions Programmatically

The SharePoint user interface and "stsadm" are not the only means of installing and activating Solutions. SharePoint's Object Model provides all the classes and methods to accomplish the task programmatically.

The SPSolution and SPSolutionCollection classes represent, respectively, one Solution and the collection of Solutions in the SharePoint farm. The SPSolutionLanguagePack and SPSolutionLanguagePackCollection classes have a similar structure, but are used exclusively for the management of Language Packs. There are also three specialized enumerations—SPSolutionDeploymentJobType, SPSolutionDeploymentState, and SPSolutionOperationResult—to define the types, states, and possible outcomes of Solutions.

All the classes and enumerations belong to the Microsoft.SharePoint.Administration NameSpace in the Microsoft.SharePoint.dll assembly. For an enumeration (without explanation) of the different classes, methods, and properties, consult the SDK of WSS 2007 (SPSolution Class and SPSolutionCollection Class).

To enumerate the Solutions and their properties in the farm, the code is as follows:

 1. SPSolutionCollection mySolutionColl = SPFarm.Local.Solutions;
 2. foreach(SPSolution mySolution in mySolutionColl)
 3. {
 4.    Console.WriteLine("Name - " + mySolution.DisplayName +
 5.                      ", Deployed - " +
                         mySolution.Deployed.ToString() +
 6.                      mySolution.DeploymentState.ToString() +
 7.                      ", Status - " +
 8. }

The first line demonstrates a created object that contains the collection of Solutions in the local farm. The 'foreach' statement loops through each Solution illustrating some of its properties. There are a myriad of properties and methods to control all the potential of the Solution, for example: methods to Deploy and Retract Solutions; properties to discern whether the Solution contains CAS policies; resources or assemblies to be installed in the GAC; a test to determine whether the Solution has been deployed; a flag if there is a task to be deployed; its status, and so on.

To install a new Solution in the Solutions Store, the following code can be used:

 9. SPSolution mySolution =
10. Console.WriteLine("Solution "   + mySolution +
11.                   " installed:" + mySolution.Added.ToString() +
12.                   ", deployed:" + mySolution.Deployed.ToString());

The "Add" method employs three constructors that allow for installing a Solution; using a previously created SPSolution object; utilizing the path to the .wsp file; and thirdly, the .wsp path and language identification. The return value is an object of the SPSolution type.

To deploy the Solution, the code with the Object Model is:

13. SPSolution mySolution = SPFarm.Local.Solutions["HelpDesk.wsp"];
14. Console.WriteLine("Before: DeploymentState -
                      " + mySolution.DeploymentState.ToString()); 
15. mySolution.Deploy(DateTime.Now, false, false);
16. Console.WriteLine("After: DeploymentState - " +

Note that if there is no Object with a reference to the Solution, as shown in Line 13, it is possible to craft a new one. The Deploy method has two constructors: one to deploy the Solution to all the Web Applications in the farm and another permitting the user to choose in which Web Application it will be deployed. In fact, this method does not literally deploy the Solution but it creates a SharePoint task to carry out the deployment. As a result of this situation, the second message of the Console.WriteLine will always indicate "NotDeployed": a confirmation that there is a time gap between running the code and when the job engine initializes the task.

The first parameter of the method "Deploy" indicates when the activity will commence. The "globalInstallWPPAckDll" parameter (the second parameter in the example) is used exclusively in deploying WebParts and permits installing the assembly of the WebPart in the GAC. The third is a "force" (Boolean) parameter; if this parameter is 'false', and the Solution has already been deployed, the procedure will raise an exception.

If the Web Interface of the Central Administration (refer to Figure 2) is utilized, Solutions containing assemblies to be deployed in the GAC will display a warning message. If the "stsadm" administration tool is selected, it is essential to use the "-allowgacdeployment" flag to deploy to the GAC. If the Object Model is chosen to deploy the Solution, no warnings will be issued.

If it is necessary to execute an "iisreset" to reset the application pools of IIS, it must be programmed independently or done manually as the "Deploy" method does not do it automatically.

Updating a Solution can be done by using the SPSolution: Upgrade (System.String) method if the Solution has not yet been deployed and the SPSolution.Upgrade(System.String,System.DateTime) method if it already has been deployed.

Finally, to retract and remove the Solution, the corresponding methods of the SPSolution class are applied:

17. SPSolution mySolution = SPFarm.Local.Solutions["HelpDesk.wsp"];
18. mySolution.Retract(DateTime.Now);
19. mySolution.Delete();

Line 18 retracts a deployed Solution. This method can have one parameter (the date and time to start the retracting task) or two parameters (the timer for the job and a reference to the Web Application where the Solution has been deployed). Line 19 simply removes the Solution entirely from the farm.

It is important to note that employing the Object Model to install, activate, and retract Solutions has some inherent risks. The timing of the job engine can be substantially slower than running the code of the Object Model. It is critical that care is taken with the events ordering: always wait until one process is completely terminated before initializing the next one; if not, the solutions engine can generate an error, and some of the errors can be terminal. Also, the Solution will be in a 'hold' status preventing further steps and a success implementation.


SharePoint Solutions is a bona fide option to deploy customizations into SharePoint 2007, and it is a successful counter to the problems present in SharePoint 2003. There are diverse ways of implementing Solutions; applying the administration tool of SharePoint, utilizing the Web interface or operating programmatically. My next article will demonstrate how to create Solutions, elucidate the differing components of the schema, and discuss aspects relevant to the construction of the .wsp file.

About the Author

Gustavo Velez is a MCSD Senior Application Developer for Winvision (http://www.winvision.nl), a Microsoft Gold Partner in the Netherlands. He has had many years experience in developing Windows and Office applications, and more than five years of daily programming experience with SharePoint. The author's articles, in English, Dutch, and Spanish, can be found in many of the leading trade magazines and he's pleased to be Webmaster of http://www.gavd.net/servers, the only Spanish-language dedicated SharePoint site. Spanish-language readers may want to consult the author's new book; Programación con SharePoint 2007.

Page 2 of 2

This article was originally published on April 18, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date