CloudShare Blog

How to create a SharePoint 2013 custom cmdlet!

As you know, SharePoint is not only a product but also a development platform. Being a platform you can extend it. You do this by creating custom elements at any level using a rich object model. For instance, you can create custom SharePoint 2013 cmdlets to add extra functionalities and capacities to the existing ones. In this article, I will show you how to create a custom cmdlet that allows you to extract Central SharePoint solutions from the solution catalog in an existing SharePoint 2013 farm.

  • First, you need to access to your CloudShare account and start one of your environments.
  • Assuming you have SharePoint 2013 and Visual Studio 2012 installed, just start Visual Studio 2012 and create a “Class Library” project either selecting .NET Framework 4.0 or 4.5 as .NET Framework version.
  • Add the following assemblies references to your project:
    • System.Management.Automation.dll that you can find at “C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v3.0”.
    • System.Configuration.Install.dll located at “C:\Windows\Microsoft.NET\Framework\v2.0.50727”.
    • Microsoft.SharePoint.dll that you can find in the 15 hive (“..\15\ISAPI\”).
    • Microsoft.SharePoint.PowerShell.dll located in the Global Assembly Cache (GAC) “C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.PowerShell\v4.0_15.0.0.0__71e9bce111e9429c”.
    • Add the following “using” sentences to your class file “Microsoft.SharePoint”, “Microsoft.SharePoint.Administration”, “Microsoft.SharePoint.PowerShell” and “System.Management.Automation”.
    • Code your class as follows:
<span class="lnum"> 1: </span><span class="kwrd">using</span> System;
<span class="lnum"> 2: </span><span class="kwrd">using</span> System.Collections.Generic;
<span class="lnum"> 3: </span><span class="kwrd">using</span> System.Linq;
<span class="lnum"> 4: </span><span class="kwrd">using</span> System.Text;
<span class="lnum"> 5: </span>
<span class="lnum"> 6: </span><span class="rem">//Namespaces</span>
<span class="lnum"> 7: </span><span class="kwrd">using</span> Microsoft.SharePoint;
<span class="lnum"> 8: </span><span class="kwrd">using</span> Microsoft.SharePoint.Administration;
<span class="lnum"> 9: </span><span class="kwrd">using</span> Microsoft.SharePoint.PowerShell;
<span class="lnum"> 10: </span><span class="kwrd">using</span> System.Management.Automation;
<span class="lnum"> 11: </span>
<span class="lnum"> 12: </span><span class="kwrd">namespace</span> SPCmdletCopySolutions
<span class="lnum"> 13: </span>{
<span class="lnum"> 14: </span>     [Cmdlet(VerbsCommon.Get, <span class="str">"SPSolutionsFromSolutionStore"</span>)]
<span class="lnum"> 15: </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> SPCmdletCopySolutions : SPGetCmdletBase&lt;<span class="kwrd">string</span>&gt;
<span class="lnum"> 16: </span>    {
<span class="lnum"> 17: </span>         <span class="kwrd">protected</span> <span class="kwrd">override</span> IEnumerable&lt;<span class="kwrd">string</span>&gt; RetrieveDataObjects()
<span class="lnum"> 18: </span>         {
<span class="lnum"> 19: </span>             <span class="kwrd">try</span>
<span class="lnum"> 20: </span>             {
<span class="lnum"> 21: </span>                 Console.WriteLine(<span class="str">"Starting the extraction process"</span>);
<span class="lnum"> 22: </span>                 <span class="kwrd">int</span> iNumeroSoluciones = 0;
<span class="lnum"> 23: </span>                 SPSolutionCollection spColeccionSoluciones =
<span class="lnum"> 24: </span>                     SPFarm.Local.Solutions;
<span class="lnum"> 25: </span>                 Console.WriteLine(<span class="str">"There are {0} solutions to be extracted"</span>,
<span class="lnum"> 26: </span>                     spColeccionSoluciones.Count);
<span class="lnum"> 27: </span>                 <span class="kwrd">foreach</span> (SPSolution spSolucion <span class="kwrd">in</span> spColeccionSoluciones)
<span class="lnum"> 28: </span>                 {
<span class="lnum"> 29: </span>                     SPPersistedFile spArchivoSolucion =
<span class="lnum"> 30: </span>                         spSolucion.SolutionFile;
<span class="lnum"> 31: </span>                     spArchivoSolucion.SaveAs(<span class="str">"C:\\Demos\\" + spArchivoSolucion.DisplayName);</span>
<span class="lnum"> 32: </span>                     iNumeroSoluciones += 1;
<span class="lnum"> 33: </span>                 }
<span class="lnum"> 34: </span>                 return new string[]
<span class="lnum"> 35: </span>                {
<span class="lnum"> 36: </span>                    "Extraction process finished<span class="str">",</span>
<span class="lnum"> 37: </span>                    "Number of solutions extracted: " +
<span class="lnum"> 38: </span>                    iNumeroSoluciones.ToString()
<span class="lnum"> 39: </span>                };
<span class="lnum"> 40: </span>
<span class="lnum"> 41: </span>             }
<span class="lnum"> 42: </span>             <span class="kwrd">catch</span> (Exception ex)
<span class="lnum"> 43: </span>             {
<span class="lnum"> 44: </span>                 <span class="kwrd">return</span> <span class="kwrd">new</span> <span class="kwrd">string</span>[]
<span class="lnum"> 45: </span>                {
<span class="lnum"> 46: </span>                   ex.Message
<span class="lnum"> 47: </span>                };
<span class="lnum"> 48: </span>             }
<span class="lnum"> 49: </span>         }
<span class="lnum"> 50: </span>    }
<span class="lnum"> 51: </span>}
  • As you can see in the code above, your class must inherit from one of the base classes available for SharePoint cmdlets. In this case, since we are going to create a cmdlet that reads from the central SharePoint solution our class inherits from SPGetCmdletBase.
  • You need to overwrite some of the methods available in the base class. In this case, we have overwritten the RetrieveDataObjects() method that will be in charge of extract the solutions stored.
  • Build your project in order to check there are not any errors in your code.
  • In order to import your custom cmdlet and use in the SharePoint 2013 Administration Console, just execute the following PowerShell sentence:

Import-Module “C:\Users\Administrator\Documents\Visual Studio 2012\Projects\SP2013AdminDemos\SPCmdletCopySolutions\bin\Debug\SPCmdletCopySolutions.dll”

  • Supposing everything worked as expected when registering your custom cmdlet, just execute it in the SharePoint 2013 Administration Console and check it works as expected:


  • Finally, review the solutions have been copied to the specific folder:


And that’s all about how to create a custom cmdlet for SharePoint 2013. I recommend you to check it in your SharePoint 2013 CloudShare environment. Happy CloudSharing!

Topics: SharePoint Demos & POCs