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:
- Sandboxed Solutions
- Farm Solutions
Are Sandboxed & Farm Solutions retrieved differently?
Yes! They both are deployed through various methods & they are retrieved through various object models.
Note: 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:
using (SPSite site = new SPSite("http://localhost"))
{
foreach (SPUserSolution solution in site.Solutions)
{
Console.WriteLine(solution.Name);
Console.WriteLine(solution.Status);
}
}
How to get Farm Solutions?
Farm Solutions exist at the Farm Level. The following is the property to get Farm Solutions:
SPFarm.Local.Solutions
The following is the code to retrieve all Farm Solutions:
foreach (SPSolution solution in SPFarm.Local.Solutions)
{
Console.WriteLine(solution.Name);
Console.WriteLine(solution.SolutionId);
Console.WriteLine(solution.Status);
}
Now let us see the installion of solutions through the Server Object Model.
Install Sandboxed Solution
Installation of a solution is a 2-step process consisting of:
-
Adding Solution to Gallery
-
Activating Solution
The following is the code to add a solution to the gallery:
using (SPSite site = new SPSite("http://localhost"))
{
SPDocumentLibrary gallery
=(SPDocumentLibrary)site.GetCatalog(SPListTemplateType.SolutionCatalog);
SPFile file = gallery.RootFolder.Files.Add("SandboxedSolution.wsp",
File.ReadAllBytes("SandboxedSolution.wsp"));
SPUserSolution solution = site.Solutions.Add(file.Item.ID);
}
Remove Sandboxed Solution
For removing a solution & deactivating its features, the following code can be used:
using (SPSite site = new SPSite("http://localhost"))
{
SPUserSolution solution = site.Solutions.Cast<SPUserSolution>().
Where(s => s.Name == "Your Solution").First();
site.Solutions.Remove(solution);
}
Install Farm Solution
For installing a Farm Solution, the following code can be used:
private static void InstallFarmSolution()
{
SPSolution solution = SPFarm.Local.Solutions.Add("File Path here");
solution.Deploy(DateTime.Now, true, GetAllWebApplications(), true);
}
We need to specify the solution path for the preceding method. The solution will be installed to all web applications as per the code above. The body of the GetAllWebApplications() method is given below.
public static Collection<SPWebApplication> GetAllWebApplications()
{
Collection<SPWebApplication> result = new Collection<SPWebApplication>();
SPServiceCollection services = SPFarm.Local.Services;
foreach (SPService s in services)
{
if (s is SPWebService)
{
SPWebService webService = (SPWebService)s;
foreach (SPWebApplication webApp in webService.WebApplications)
{
result.Add(webApp);
}
}
}
return result;
}
Remove Farm Solution
Removing a Farm Solution is called a Retract Solution. You can find the appropriate method in:
private void RetractFarmSolution(SPSolution solution)
{
solution.Retract(DateTime.Now);
}
A timer job will be created to Retract the solution. You can specify the time to start the retraction.
For removing the solution only from a specified web application, there is an overloaded method. See:
private void RetractFarmSolution(SPSolution solution, Collection<SPWebApplication> webApplications)
{
solution.Retract(DateTime.Now, webApplications);
}
References
http://msdn.microsoft.com/en-us/library/ee361616.aspx
http://msdn.microsoft.com/en-us/library/hh528516(v=office.14).aspx
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:
-
A Sandboxed Solution is represented as a SPUserSolution
-
A Farm Solution is represented as a SPFarmSolution