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.
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:
3 | foreach (SPUserSolution solution in site.Solutions) |
5 | Console.WriteLine(solution.Name); |
6 | Console.WriteLine(solution.SolutionId); |
7 | Console.WriteLine(solution.Status); |
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.
1 | foreach (SPSolution solution in SPFarm.Local.Solutions) |
3 | Console.WriteLine(solution.Name); |
4 | Console.WriteLine(solution.SolutionId); |
5 | Console.WriteLine(solution.Status); |
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:
3 | SPDocumentLibrary gallery |
4 | =(SPDocumentLibrary)site.GetCatalog(SPListTemplateType.SolutionCatalog); |
5 | SPFile file = gallery.RootFolder.Files.Add( "SandboxedSolution.wsp" , |
6 | File.ReadAllBytes( "SandboxedSolution.wsp" )); |
8 | SPUserSolution solution = site.Solutions.Add(file.Item.ID); |
Remove Sandboxed Solution
For removing a solution & deactivating its features, following code can be used:
3 | SPUserSolution solution = site.Solutions.Cast<SPUserSolution>(). |
4 | Where(s => s.Name == "Your Solution" ).First(); |
5 | site.Solutions.Remove(solution); |
Install Farm Solution
For installing Farm Solution, following code can be used:
1 | private static void InstallFarmSolution() |
3 | SPSolution solution = SPFarm.Local.Solutions.Add( "File Path here" ); |
4 | solution.Deploy(DateTime.Now, true , GetAllWebApplications(), true ); |
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.
01 | public static Collection<SPWebApplication> GetAllWebApplications() |
03 | Collection<SPWebApplication> result = new Collection<SPWebApplication>(); |
05 | SPServiceCollection services = SPFarm.Local.Services; |
07 | foreach (SPService s in services) |
09 | if (s is SPWebService) |
11 | SPWebService webService = (SPWebService)s; |
13 | foreach (SPWebApplication webApp in webService.WebApplications) |
Remove Farm Solution
Removing Farm Solution is termed as Retract Solution. You can find the appropriate method in:
1 | private void RetractFarmSolution(SPSolution solution) |
3 | solution.Retract(DateTime.Now); |
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.
1 | private void RetractFarmSolution(SPSolution solution, Collection<SPWebApplication> webApplications) |
3 | solution.Retract(DateTime.Now, webApplications); |
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