How to install a farm solution, in 2010 and 2013 mode, for SharePoint 2013

sharepoint farm solution

The SharePoint 2013 Upgrade Model allows administrators to create true “SharePoint 2010 sites” instead of Visual Upgrade sites. This method is fundamentally different from Visual Upgrade in that there is actually a “14” root folder that is deployed and all SharePoint 2010 features and files are also deployed.

SharePoint 2013 allows you to run your sites in 2010 and 2013 mode with different functionalities and features. When creating new sites, you can choose which version you would like to use: 2010 or 2013. SharePoint 2013 introduces a new concept called Compatibility Level. Right now the Compatibility Level value is either 14 or 15. When deploying this solution from SharePoint 2010, just deploy them in the old way by using Add-SPSolution and Install-SPSolution as you did in SharePoint 2010. In this way the solutions will be deployed within the 2010 mode. If the Compatibility Level is not specified in the PowerShell command, then the value of the “SharePointProductVersion” attributed in the manifest file of the solution will be used as the Compatibility Level. With the “CompatibilityLevel” parameter in the Install-Solution command you can deploy the solution to either 2010 mode, 2013 mode or both.

Example
Install-SPSolution -Identity [SolutionName].wsp -GACDeployment -CompatibilityLevel 15
Install-SPSolution -Identity [SolutionName].wsp -GACDeployment -CompatibilityLevel {14, 15}

How to install a farm solution in 2010 and 2013 mode programmatically

By using the .Net Reflector, I found an internal method SPSolutionLanguagePack.CreateSolutionDeployTimerJob that can help us to work it out. See the following code snippet.

public void Deploy (Guid solutionId, Collection applications, int maxCompat)
{
    SPSolution solution = SPFarm.Local.Solutions[solutionId];
    SPSolutionLanguagePack languagePack = solution.GetLanguagePack(0);
    SPCompatibilityRange compatibilityRange = new SPCompatibilityRange(14, maxCompat);
    Type deployType = languagePack.GetType();
    Type[] argumentTypes = new Type[] { typeof(DateTime), applications.GetType(), typeof(SPSolutionDeploymentJobType), typeof(bool), typeof(bool), typeof(bool), compatibilityRange.GetType() };
    ParameterModifier[] modifiers = new ParameterModifier[] { new ParameterModifier(7) };
    MethodInfo deployMethod = deployType.GetMethod("CreateSolutionDeployTimerJob", BindingFlags.Instance | BindingFlags.NonPublic, null, argumentTypes, modifiers);
    object[] args = new object[] { GetImmediateJobTime(), applications, SPSolutionDeploymentJobType.Deploy, true, true, false, compatibilityRange };
    deployMethod.Invoke(languagePack, args);
}

Reference
http://technet.microsoft.com/en-us/library/ff607534.aspx
http://technet.microsoft.com/en-us/library/ee617150.aspx