Juan Carlos

How to create a SharePoint 2013 custom cmdlet!

September 24th, 2012 | Author: | Filed under: Demos & POCs, SharePoint | Tags: , , , , , , ,


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:

image

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

image

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!

About the author:
With more than 10 years of experience in the ICT sector, what best define me is the interest and knowledge of new technologies as a natural way for attending and solving customer problems and needs. My career in the TIC sector, and particularly in the .NET platform started in 2003, just after finishing my studies at the university, in the global worldwide consultancy company Accenture. There, I had the opportunity of working during 3 years in big national and international projects performing different task and roles: development of VB.NET applications, application test and deployment, management of small development teams and so on. In May 2006 I left Accenture and started a new adventure at the Microsoft Innovation Center in Cantabria (CIIN) as a Solutions Architect. In this new stage, I had the opportunity of knowing deeply a variety of Microsoft technologies such as SharePoint, Office 365, Windows Azure, Visual Studio, SQL Server Reporting Services, BizTalk, LINQ, Entity Framework, etc. At the CIIN I haved performed evangelism activities just around these technologies, and I have had the honor of belonging to one of the more specialized organizations in SharePoint in Spain. In October 2013 I joined LKS where I perform a Consultant and Solutions Architect role skilled in SharePoint and Office 365 platforms. My daily work in LKS is focused on providing SharePoint & Office 365 advice and consultancy, specialized training and evangelism in the different projects and LKS customers where I’m involved.