Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Friday, December 02, 2011

Working With Solutions through Code


This post will show you how to install and deploy solutions with code. The API is very rich in its objects and makes the solution framework available to install and deploy solutions in code. You can also see what servers and web applications that a solutions has been deployed to.
The first thing we do is get a reference to our local farm.
SPFarm oFarm = SPFarm.Local;
The next thing we do is actually add the solution to the local farm.
Replace the C:MySolutionFile.wsp with the full path to your solution file.
SPSolution oSolution = oFarm.Solutions.Add(@"C:MySolutionFile.wsp");
The next thing we need to do is tell SharePoint to go ahead and provision the solution. This will let SharePoint go through its provisioning process for the added solution.
oSolution.Provision();
Now we can actually deploy the solution. We can deploy the solution to the local farm globally, meaning all the web applications, or we can deploy the solution to specific web applications. The method DeployLocal has 2 different calling options. One to take a couple boolean paramaters and second to take the boolean parameters as well as a Collection of SPWebApplication object. This will tell SharePoint what web application to actually deploy the solution to.
This code will deploy the solution locally to all web applications in the farm. The first parameter tells SharePoint to install the web part packages and the second tells SharePoint to force the deployment if the same solution already exists.
oSolution.DeployLocal(true, true);
This code will deploy the solution to specific web applications. The first parameter tells SharePoint to install the web part packages, the second is a collection of all the SPWebApplication objects that you wish to deploy the solution to and the third tells SharePoint to force the deployment if the same solution already exists
oSolution.DeployLocal(true, <>, true);
Now that you have deployed your solutions, you can look at specific properties on the SPSolution object and tell what servers the solution is deployed to as well as what web application the solution is deployed to.
To look at what servers a solution is deployed to, you look through the DeployedServers property on the solution object. This is just a collection of SPServer objects.
foreach (SPServer oServer in oSolution.DeployedServers)
{
    // Do your processing of each server object
}
To look at what web applications a solution is deployed to, you look through the DeployedWebApplications property on the solution object. This is just a collection of SPWebApplication objects.
foreach (SPWebApplication spWebApplication in CurrentSolution.DeployedWebApplications)
{
    // Do your processing of each web application object
}
Here at iDevFactory we have also been getting alot of request on how you do certain tasks in our SharePoint Work Acceleration Toolkit. For those of you interested in how to accomplish the same tasks above in SWAT read on.
In SWAT there is a Solutions window. This shows you all the solutions that are installed in your farm.

In the solutions window, you can click the add button to bring up the Add Solution window.

On the Add Solution window, click the Select Solution button and browse to your solution file.

On the Add Solution window, after you have selected your solution file to add, you can select specific web applications to deploy the solution to as well as deploying the solution locally. After you have made your specific selected, click the Add button to add the solution to your SharePoint farm.

Back at the Solutions window, you can right click on a specific solution and select Solution Manager.

On the Solution Manager window, the Detail tab will show you specific details about your solution General Information, Solution Information, Deployment Information and Job Information.

On the Solution Manager window, the Servers tab will show you all the servers that this solution is deployed to.

On the Solution Manager window, the Web Applications tab will show you all the web application that this solution is deployed to. You can also deploy to other web applications as well as retract from deployed web applications.

No comments:

Post a Comment