Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Programming with Solutions

Solutions are the means by which collections of Features are installed on SharePoint farms and deployed to servers within those farms. After being installed on a farm, Solutions can be deployed to any server within that farm automatically using the web-based Solution management interface.

Installing and Removing Solutions

If you don't want to use the stsadm.exe command-line tool, or you simply can't for some reason, you can still programmatically manipulate the list of Solutions available for deployment within a farm.

As shown earlier in the chapter, any time you need to work with the SPFarm class, you need to pass a configuration database connection string to the constructor:

string dbConn = @"server=localhost\OfficeServers;initial catalog=SharePoint_Config_66140120-a9bf-4191-86b6-ec21810ca019;IntegratedSecurity=SSPI;";

_rootCollection = new SPSite(siteUrl.Text);
SPFarm farm = SPFarm.Open(dbConn);

After you have a reference to the farm, you can then access the list of installed Solutions in the farm with the Solutions property. To install a Solution, just add it to the Solutions collection. To uninstall a Solution, simply remove it from the collection.

The SPSolution class cannot be instantiated directly with a constructor. Instead, you have two options when adding to the Solutions collection. You can pass the Solution filename as a parameter, or you can pass the Solution filename and a locale identifier (a UInt32, such as 1033):

farm.Solutions.Add("myapplication.cab");
farm.Solutions.Add("myapplication.cab", 1033);

When removing a Solution, you can either pass the name of the Solution as a parameter, or you can pass the GUID of the Solution:

farm.Solutions.Remove(new Guid("..."));

The Add and Remove methods of the Solutions collection correspond directly to the functionality provided by the stsadm.exe commands "-o addsolution" and "-o deletesolution".

Enumerating Solutions

When working with Solutions, you might want to take a look at the list of Solutions currently installed in the farm. From this list, you can then control the deployment status (shown in the next section) of the Solution or inspect the various properties of the Solutions. The following code is a simple illustration of how to examine the list of installed Solutions in a farm:

Console.WriteLine("Solution:\tCAS Policy\tGAC Assembly\tWeb Resource\n");
foreach (SPSolution solution in farm.Solutions)
{
    Console.WriteLine("{0}:\t{1}\t{2}\t{3}",
        solution.DisplayName,
        solution.ContainsCasPolicy,
        solution.ContainsGlobalAssembly,
        solution.ContainsWebApplicationResource);
    foreach (SPServer deployedServer in solution.DeployedServers)
    {
        Console.WriteLine("\t\tDeployed to {0}", deployedServer.DisplayName);
    }                    
}

Table 3.1 describes many of the properties of the SPSolution class.

Table 3.1. SPSolution Properties

Property

Description

Added

Indicates whether a language-neutral Solution package has been added to the Solution

ContainsCasPolicy

Indicates whether the Solution contains a Code Access Security policy

ContainsGlobalAssembly

Indicates whether the Solution installs Assemblies into the GAC

ContainsWebApplicationResource

Indicates whether the Solution contains any application-specific resources

Deployed

Indicates whether the Solution has been deployed to one or more locations within the farm

DeployedServers

Indicates the list of servers to which the Solution has been deployed

DeployedWebApplications

Indicates the list of web applications to which the Solution has been deployed

DeploymentState

Indicates the current state of deployment for the Solution

Id

Indicates the GUID of the Solution

IsWebPartPackage

Indicates whether the Solution is a Web Part package

LastOperationDetails

Represents the details of the last operation performed while deploying the Solution

LastOperationEndTime

Indicates the time the last operation completed

LastOperationResult

Indicates the results of the last operation during deployment

Name

Indicates the name of the Solution

Properties

Indicates the property bag containing custom properties for the Solution

SolutionFile

Indicates the file associated with the Solution

SolutionId

Indicates the ID of the Solution as indicated by the Solution's manifest file

Controlling Solution Deployment

Controlling Solution deployment really boils down to two different methods on the SPSolution class: Deploy and Retract. The Deploy method deploys a Solution to the given location, whereas the Retract method removes the Solution from the given location while still remaining installed within the farm.

The deploy methods take the following arguments:

  • dt—The date and time when the deployment should take place
  • globalInstallWPPackDlls—A Boolean indicating whether to install the dynamic link libraries (DLLs) in the GAC (for Web Part packages only)
  • force—A Boolean indicating whether the Solution can be redeployed

You can also optionally supply a collection of SPWebApplication instances to further refine the deployment. The Retract() method schedules a job for when the Solution should be retracted and can optionally take a list of web applications from which to retract the solution.

  • + Share This
  • 🔖 Save To Your Account