How to manage SharePoint solutions in C # code

Source: Internet
Author: User

How to manage SharePoint solutions in C # code This article we'll learn how to manage SharePoint solutions in code.        We use the server-side object model to extract the solution. There are two types of workarounds in SharePoint: Sandbox resolution and farm resolution.        Sandboxed Solutions and farm solutions are deployed in different ways, and are extracted through different object models. Note: Here spusersolution is used to represent the sandbox solution, and the spfarmsolution represents the field resolution method. How to get sandboxed solutions sandboxed solutions are deployed at the site collection level. Here's how to extract all the user solutions in a site set:
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 get the farm solution to extract the entire farm solution is as follows:
foreach (Spsolution solution in SPFarm.Local.Solutions) {    Console.WriteLine (solution. Name);    Console.WriteLine (Solution. SolutionID);    Console.WriteLine (Solution. Status);}
Next look at how to install the solution through the server-side object model.        Install sandboxed Solutions There are two steps to installing the solution: Add to Library, activate. Here's how to add a workaround to the library code:
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 the sandbox workaround remove the workaround and disable the feature using 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);}
Install Farm Solution Install Farm workaround use the following code:
private static void Installfarmsolution () {    spsolution solution = SPFarm.Local.Solutions.Add ("File Path Here");    Solution. Deploy (DateTime.Now, True, Getallwebapplications (), true);}
We need to specify a workaround path. The above code allows the workaround to be installed in all Web applications. Getallwebapplication () method bodies such as the following:
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;< C7/>foreach (spwebapplication webApp in webservice.webapplications)            {                result. ADD (WEBAPP);    }}} return result;}
Removing a farm solution removes the farm solution as a retract solution. This is the right approach:
private void Retractfarmsolution (Spsolution solution) {    solution. Retract (DateTime.Now);}
Create a timer job to retract the workaround.        You can specify the time to start recovering. To remove a workaround from the specified Web application only, take this approach:
private void Retractfarmsolution (Spsolution solution, collection<spwebapplication> webapplications) {    Solution. Retract (DateTime.Now, webapplications);}
In this article, we learned how to use the server-side object model to extract sandboxed solutions and farm solutions.
Reference: About the difference between the sandbox solution and the field solution, http://msdn.microsoft.com/en-us/library/ee361616.aspx using code to activate the SharePoint 2010 Sandbox solution, take the photo http:/ /msdn.microsoft.com/en-us/library/hh528516 (v=office.14). aspx

How to manage SharePoint solutions in C # code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.