.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to get SharePoint Solutions through Code in SharePoint

Posted By:Jean Paul       Posted Date: January 31, 2014    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we can explore how to get SharePoint Solutions through code. We are using Server Object Model to retrieve the solutions.
 

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:

 

	     using (SPSite site = new SPSite("http://localhost"))
            {
                foreach (SPUserSolution solution in site.Solutions)
                {
                    Console.WriteLine(solution.Name);
                    Console.WriteLine(solution.SolutionId);
                    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.

 

          foreach (SPSolution solution in SPFarm.Local.Solutions)
            {
                Console.WriteLine(solution.Name);
                Console.WriteLine(solution.SolutionId);
                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:

 

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, 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 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)
               {
                result.Add(webApp);
        }
        }
    }

    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)
{
    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.

 

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:

1. Sandboxed Solution is represented as SPUserSolution

2. Farm Solution is represented as SPFarmSolutionv


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend