in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Tech MOSS Team

SharePoint Programmatically: Managing Solutions

Managing SharePoint 2007 Solutions programmatically isn’t a daily-matter: some of you might even participate on quite a few projects without even coming close to it. As I have worked with it recently a bit I would like to share my findings with you.

I assume you know what the SharePoint solutions (.wsp packages) are and how do they work. What I’d like to focus on is working with the Solutions programmatically (through the SharePoint Object Model): adding new solutions to the Solution Store, deploying Solutions to Web Applications, retracting and removing them.

First of all let’s take a look at accessing the local Solution Store. For simplicity I will use the local farm. You will probably use the same in your code:

SPFarm.Local.Solutions

Solutions contain all available solutions on the chosen farm.

You can add a Solution to the Solution Store by simply passing the local path to the .wsp file:

string solutionPath = @"C:\somesolution.wsp";
SPSolution solution = SPFarm.Local.Solutions.Add(solutionPath);

Adding a solution to the Solution Store will not overwrite the earlier versions. So if you would like to replace your old .wsp file with a new one, you will need to retract (if deployed) and remove the old one. I will show you how to do it later on.

Adding a solution doesn’t really triggers anything: it just makes the solution available – you still can’t use it though. You need to deploy the solution to a Web Application to actually use it. In the following example I first obtain the Content Web Service using the ContentService property. It allows me to obtain the Web Applications used by SharePoint. Then I select some Web Application where I would like to deploy the solution to.

Collection<SPWebApplication> selectedWebApps = new Collection<SPWebApplication>();
SPWebService ws = SPWebService.ContentService;
webApps = ws.WebApplications;
selectedWebApps.Add(webApps["My Web App"]);
selectedWebApps.Add(webApps["SharePoint - 80"]);
solution.Deploy(DateTime.Now, true, selectedWebApps, true);

You need to set the second parameter to true only if your solution needs to deploy some assemblies to the GAC. Otherwise it can be set to false: the assemblies will be deployed to the bin directory of the selected Web Applications.

Your solution has just been added to the local Solution Store and deployed to the selected Web Applications. Now I would like to show you how to retract your solution from the Web Applications where it has been deployed to.

First of all you need to get your solution as an instance of the SPSolution object. Personally I use the custom GetSolutionByName method which allows me to obtain the required solution as object by passing the solution’s name.

private SPSolution GetSolutionByName(string solutionName)
{
       SPSolutionCollection sc = SPFarm.Local.Solutions;
       foreach (SPSolution s in sc)
       {
              if (s.Name.ToLower() == solutionName.ToLower())
                     return s;
       }

       return null;
}

To retract the solution:

SPSolution solution = GetSolutionByName(solutionName);
if (solution != null)
{
       if (solution.DeployedWebApplications != null)
       {
              solution.RetractLocal(solution.DeployedWebApplications);
       }

       ExecuteJobDefinitions();
       SPFarm.Local.Solutions.Remove(solution.Id);
}

First of all I use the DeployedWebApplications property of SPSolution to check whether the solution has been deployed at all. If so (the collection is not null), I use the same property to pass the collection of Web Applications where I would like to retract the solution from. As retracting a Solution creates a timer job on the farm you might want to speed it up a bit – especially if you retract the solution right before adding the new version of it:

private void ExecuteJobDefinitions()
{
       if (SPFarm.Local.TimerService.JobDefinitions != null)
       {
              foreach (SPJobDefinition job in SPFarm.Local.TimerService.JobDefinitions)
              {
                     try
                     {
                           job.Execute(SPServer.Local.Id);
                     }
                     catch { }
              }

       }
}

The last part is to remove the solution from the Solution store by passing its ID to the Remove method. Retracting and removing a Solution prevent you from getting an error while installing a Solution that already exists.

So, where could you make use of all this? The above might come handy while deploying your solutions on your customers' machine - especially if you have created a product-like solution and you would like to ship it with a user-friendly setup. Another usage scenario could be using features for deploying solutions and scaling the deployment.

Comments

 

Links (9/27/2007) « Steve Pietrek’s SharePoint Stuff said:

Pingback from  Links (9/27/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

September 27, 2007 7:59 PM
 

Paul Jones said:

Hello, shouldn't the above code actually call solution.Retract() instead of solution.RetractLocal() ? The solution wasn't deployed on the local server because you used Deploy() instead of DeployLocal().

Thanks for the code though, quite useful.

October 3, 2007 5:01 AM
 

Waldek Mastykarz said:

You are absolutely right. I just made a wrong assumption. In real life you would definitely use the pairs: Deploy<->Retract; DeployLocal<->RetractLocal.

October 3, 2007 12:41 PM
 

Dave said:

Yes. Programmatically managing SharePoint isn't an easy task. There are plenty of tools for different aspects of SharePoint administration for the non-programming community. I recommend that you take a look at this <a href="www.virtual-generations.com/.../sharepoint-2007-link-love-09-29-2007">list of handy tools</a> for SharePoint. Another good tool for managing SharePoint security permissions you can get <a href="www.scriptlogic.com/.../a>

October 11, 2007 10:49 AM
 

Philippe Sentenac [MVP SharePoint] said:

En parcourant les mises à jours des projets Codeplex sur SharePoint, je tombe ce matin sur la mise à

June 17, 2008 7:06 AM
 

Philippe Sentenac [MVP SharePoint] said:

En parcourant les mises à jours des projets Codeplex sur SharePoint, je tombe ce matin sur la mise à

June 17, 2008 7:09 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

About Waldek Mastykarz

Waldek Mastykarz is a Dutch SharePoint 2007 developer specialized in Web Content Management solutions in Microsoft Office SharePoint Server 2007, web standards and accessibility.

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts