How to use C # code to manage SharePoint solutions This article describes how to use code to manage SharePoint solutions. We use the server-side object model extraction solution. There are two solutions in SharePoint: Sandbox solution and field solution. Sandbox solutions and field solutions are deployed in different ways and extracted using different object models. Note: spusersolution is used here to represent the sandbox solution; spfarmsolution represents the field solution. How to obtain the sandbox solution is deployed at the website set level. The following is a centralized solution for extracting all users from the website:
using (SPSite site = new SPSite("http://localhost")){ foreach (SPUserSolution solution in site.Solutions) { Console.WriteLine(solution.Name); Console.WriteLine(solution.Status); }}
The code for how to obtain a field solution to extract all the field solutions is as follows:
foreach (SPSolution solution in SPFarm.Local.Solutions){ Console.WriteLine(solution.Name); Console.WriteLine(solution.SolutionId); Console.WriteLine(solution.Status);}
Next, let's take a look at how to install the solution through the server-side object model. To install the sandbox solution, follow these steps: add to database and activate. The following code adds a solution to the database:
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 sandbox solution remove solution and disable function use the following code:
using (SPSite site = new SPSite("http://localhost")){ SPUserSolution solution = site.Solutions.Cast<SPUserSolution>(). Where(s => s.Name == "Your Solution").First(); site.Solutions.Remove(solution);}
Use the following code to install the on-site solution:
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. The above Code allows the solution to be installed in all web applications. The main body of the getallwebapplication () method is as follows:
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 a farm solution remove a farm solution become a recovery solution. This is an appropriate method:
private void RetractFarmSolution(SPSolution solution){ solution.Retract(DateTime.Now);}
Create a timer job recovery solution. You can specify the start time. Only remove the solution from the specified web application. Refer to this method:
private void RetractFarmSolution(SPSolution solution, Collection<SPWebApplication> webApplications){ solution.Retract(DateTime.Now, webApplications);}
In summary, we learned how to use the server-side object model to extract sandbox solutions and field solutions.
Reference: for differences between sandbox solution and field solution, see http://msdn.microsoft.com/en-us/library/ee361616.aspxuse code to activate SharePoint 2010 sandbox solution, refer to http://msdn.microsoft.com/en-us/library/hh528516 (V = office.14). aspx