How to get SharePoint Solutions through Code in SharePoint


In this article we can explore how to get SharePoint Solutions through code. We are using Server Object Model to retrieve the solutions.
As you know there are 2 types of Solutions in SharePoint:
1. Sandboxed Solutions
2. Farm Solutions
Are Sandboxed & Farm Solutions retrieved differently?
Yes! They both are deployed through different methods & they are retrieved through different object models.
clip_image002
Sandboxed Solution is represented by SPUserSolution class & Farm Solution is represented by SPFarmSolution class.
How to get Sandboxed Solutions?
Sandboxed Solutions are deployed in the Site Collection level. (Feature Activation is different where we activate both for Site Collection & Site levels)
Following is the property to get User Solutions.
site.Solutions
Following is the code to retrieve all User Solutions in a Site Collection:

1using (SPSite site = new SPSite("http://localhost"))
2   {
3       foreach (SPUserSolution solution in site.Solutions)
4       {
5           Console.WriteLine(solution.Name);
6           Console.WriteLine(solution.SolutionId);
7           Console.WriteLine(solution.Status);
8       }
9   }
How to get Farm Solutions?
Farm Solutions exists in the Farm level. Following is the property to get Farm Solutions.
SPFarm.Local.Solutions
Following is the code to retrieve all Farm Solutions.

1foreach (SPSolution solution in SPFarm.Local.Solutions)
2  {
3      Console.WriteLine(solution.Name);
4      Console.WriteLine(solution.SolutionId);
5      Console.WriteLine(solution.Status);
6  }
Now let us see installing solutions through the Server Object Model.
Install Sandboxed Solution
Installation of a Solution is a 2 step process consisting of:
1. Adding Solution to Gallery
2. Activating Solution
Following is the code to add solution to gallery:

1using (SPSite site = new SPSite("http://localhost"))
2{
3    SPDocumentLibrary gallery
4        =(SPDocumentLibrary)site.GetCatalog(SPListTemplateType.SolutionCatalog);
5    SPFile file = gallery.RootFolder.Files.Add("SandboxedSolution.wsp",
6    File.ReadAllBytes("SandboxedSolution.wsp"));
7 
8    SPUserSolution solution = site.Solutions.Add(file.Item.ID);
9}
Remove Sandboxed Solution
For removing a solution & deactivating its features, following code can be used:

1using (SPSite site = new SPSite("http://localhost"))
2       {
3        SPUserSolution solution = site.Solutions.Cast<SPUserSolution>().
4Where(s => s.Name == "Your Solution").First();
5site.Solutions.Remove(solution);
6}
Install Farm Solution
For installing Farm Solution, following code can be used:

1private static void InstallFarmSolution()
2{
3            SPSolution solution = SPFarm.Local.Solutions.Add("File Path here");
4            solution.Deploy(DateTime.Now, true, GetAllWebApplications(), true);
5}
We need to specify the solution path for the above method. The solution will be installed to all web applications as per the above code. The body of GetAllWebApplications() method is given below.

01public static Collection<SPWebApplication> GetAllWebApplications()
02{
03            Collection<SPWebApplication> result = new Collection<SPWebApplication>();
04 
05            SPServiceCollection services = SPFarm.Local.Services;
06 
07            foreach (SPService s in services)
08            {
09                if (s is SPWebService)
10                {
11                 SPWebService webService = (SPWebService)s;
12 
13                 foreach (SPWebApplication webApp in webService.WebApplications)
14               {
15                result.Add(webApp);
16        }
17        }
18    }
19 
20    return result;
21}
Remove Farm Solution
Removing Farm Solution is termed as Retract Solution. You can find the appropriate method in:

1private void RetractFarmSolution(SPSolution solution)
2{
3    solution.Retract(DateTime.Now);
4}
A timer job will be created to Retract the solution. You can specify the time to start retraction.
For removing solution only from a specified web applications, there is an overloaded method.

1private void RetractFarmSolution(SPSolution solution, Collection<SPWebApplication> webApplications)
2{
3    solution.Retract(DateTime.Now, webApplications);
4}
References
Summary
In this article we have explored how to use the Server Object Model to retrieve Sandboxed & Farm Solutions through code.
For reference please note that:
1. Sandboxed Solution is represented as SPUserSolution
2. Farm Solution is represented as SPFarmSolutionv

Comments