Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

How to get SharePoint Solutions through Code?

Posted by Jean Paul on April 21, 2013

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.


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)







How to get Farm Solutions?

Farm Solutions exists in the Farm level. Following is the property to get Farm Solutions.


Following is the code to retrieve all Farm Solutions.

foreach (SPSolution solution in SPFarm.Local.Solutions)






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:

using (SPSite site = new SPSite("http://localhost"))


SPDocumentLibrary gallery


SPFile file = gallery.RootFolder.Files.Add("SandboxedSolution.wsp",


SPUserSolution solution = site.Solutions.Add(file.Item.ID);


Remove Sandboxed Solution

For removing a solution & deactivating its features, 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();



Install Farm Solution

For installing Farm Solution, 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 above method. The solution will be installed to all web applications as per the above code. The body of 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)






return result;


Remove Farm Solution

Removing Farm Solution is termed as Retract Solution. You can find the appropriate method in:

private void RetractFarmSolution(SPSolution solution)




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.

private void RetractFarmSolution(SPSolution solution, Collection<SPWebApplication> webApplications)


solution.Retract(DateTime.Now, webApplications);




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 SPFarmSolution

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s