Create a Powershell command that downloads a Sharepoint Solution package from a Sharepoint 2010 Solution Store

You can download the source code from Codeplex, but we will create it from scratch.

In this lab, you will create a Powershell Cmdlet that will download a Sharepoint Solution package from the Sharepoint Solution store.

1. Start Visual Studio 2010 and create a new Class Library; delete the Class1.cs; name the project DownloadWsp.

2. Make sure the .Net framework 3.5 is selected and that the platform target in the project properties is Any CPU.

3. Add a reference to the Microsoft.Sharepoint assembly.

4. We need to add a reference to the System.Management.Automation assembly which is installed in the Gac, but not available in Visual Studio by default. Add a reference to System.Management (which is showing in the reference list).Save the project. Edit the project file with notepad and change "<Reference Include="System.Management" />" to "<Reference Include="System.Management.Automation" />.

5. Now we will create our custom Cmdlet : add a new public class (it is not public by default) , name it DownloadWspCmdlet and derived it from PSCmdlet; resolve the namespace:

clip_image002

1. Define 2 public properties WspFileName and StoreTo that will become the Cmdlet parameters by decorating them with the Parameter attribute:

clip_image004

2. Override the function ProcessRecord is the code that will run when the Cmdlet will be invoked:

clip_image006

3. Resolve the namespace Microsoft.Sharepoint.Administration.

4. We can also define the verb and the noun to use in order to invoke our Cmdlet :decorate the class with a Cmdlet attribute :

clip_image008

5. Now we need to create a SnapIn which is a class derived from PSSnapin :create a public class named DownloadWspSnapin, derive it from PSSnapIn and decorate it with the attribute RunInstaller:

clip_image010

6. RunInstaller is defined in the assembly System.Configuration.Install, ad a reference to this assembly.

7. PSSnapIn is an abstract class, implement its members:

clip_image012

clip_image014

8. Rebuild the project, add the generated dll into the Gac.

9. Run InstallUtil.exe against your dll; you will find InstallUtil.exe in the following folder :

clip_image016

clip_image018

10. Start a Powershell console and add the new snapin:

clip_image019

11. Test your Cmdlet by invoking Download-SPSolution in the Powershell console:

clip_image021

Code can be download from Codeplex.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s