How to create a SharePoint 2013 custom cmdlet!
September 24th, 2012 | Author: Juan Carlos | Filed under: Demos & POCs, SharePoint | Tags: cmdlet, code, commands, cusom, Features, object model, SharePoint, SharePoint 2013
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:
-
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: //Namespaces
7: using Microsoft.SharePoint;
8: using Microsoft.SharePoint.Administration;
9: using Microsoft.SharePoint.PowerShell;
10: using System.Management.Automation;
11:
12: namespace SPCmdletCopySolutions
13: {
14: [Cmdlet(VerbsCommon.Get, "SPSolutionsFromSolutionStore")]
15: public class SPCmdletCopySolutions : SPGetCmdletBase<string>
16: {
17: protected override IEnumerable<string> RetrieveDataObjects()
18: {
19: try
20: {
21: Console.WriteLine("Starting the extraction process");
22: int iNumeroSoluciones = 0;
23: SPSolutionCollection spColeccionSoluciones =
24: SPFarm.Local.Solutions;
25: Console.WriteLine("There are {0} solutions to be extracted",
26: spColeccionSoluciones.Count);
27: foreach (SPSolution spSolucion in spColeccionSoluciones)
28: {
29: SPPersistedFile spArchivoSolucion =
30: spSolucion.SolutionFile;
31: spArchivoSolucion.SaveAs("C:\\Demos\\" + spArchivoSolucion.DisplayName);
32: iNumeroSoluciones += 1;
33: }
34: return new string[]
35: {
36: "Extraction process finished",
37: "Number of solutions extracted: " +
38: iNumeroSoluciones.ToString()
39: };
40:
41: }
42: catch (Exception ex)
43: {
44: return new string[]
45: {
46: ex.Message
47: };
48: }
49: }
50: }
51: }
-
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!

