How to use C # code to manage SharePoint Solutions,
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
How to use C ??
Switch with if:
# Include <stdio. h>
Void main ()
{
Int I, j;
Printf ("Enter your exam score :");
Scanf ("% d", & I );
If (I> = 0 & I <= 59)
J = 1;
If (I> = 60 & I <69)
J = 2;
If (I> = 70 & I <= 79)
J = 3;
If (I> = 80 & I <= 89)
J = 4;
If (I> = 90 & I <= 99)
J = 5;
Switch (j)
{
Case 1:
{
Printf ("your score is: E \ n ");
Break;
}
Case 2:
{
Printf ("your score is: D \ n ");
Break;
}
Case 3:
{
Printf ("your score is C \ n ");
Break;
}
Case 4:
{
Printf ("your score is B \ n ");
Break;
}
Case 5:
{
Printf ("your score is A \ n ");
Break;
}
Default:
Printf ("your score is not within the exam score range! \ N ");
}
}
If multi-branch structure statements are written separately:
# Include <stdio. h>
Void main ()
{
Int I;
Printf ("Enter the exam score :");
Scanf ("% d", & I );
If (I <= 0 | I> = 100)
Printf ("beyond the Score Input range! \ N ");
If (I <= 99 & I> = 90)
{
Printf ("score :");
Printf ("A \ n ");
}
If (I <= 89 & I> = 80)
{
Printf ("score :");
Printf ("B \ n ");
}
If (I <= 79 & I> = 70)
{
Printf ("score :");
Printf ("C \ n ");
}
If (I <= 69 & I> = 60)
{
Printf ("score :");
Printf ("D \ n ");
}
If (I <= 59 & I> = 0)
{
Printf ("score :");
Printf ("E \ n ");
}
}
Separate switch statements:
# Include <stdio. h>
Void main ()
{
Int I, j;
Printf ("Enter your exam score :");
Scanf ("% d", & I );
J = I/10;
Switch (j)
{
Case 9:
{
Printf ("your exam score is: A \ n" ...... the remaining full text>
How to use C to implement unescape Encryption
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Char c, d;
Int;
Printf ("Enter the string to be converted: \ n ");
Do {
C = getchar ();
D = getchar ();
If (c = '\ 0 ')
{
Printf ("\ n ");
System ("pause ");
Continue;
} // If there are even numbers of Conversion characters, the Terminator will not be used, because the second and second characters will be % u0000.
If (d = '\ 0 '){
Printf ("\ % u0 % x \ n", d, c); // convert % u00xx to an odd number of characters.
System ("pause ");}
Else printf ("\ % u % x", d, c );
If (c = '\ n' | d =' \ n') printf ("\ n ");
} While (c! = EOF & d! = EOF );
}