Friday, March 1, 2013

Intellisense for SharePoint Libraries in PowerShell using PowerGUI and PowerSE

If, like me, you have become addicted to Intellisense and autocompletion from years of Visual Studio, you will be quite a bit disconcerted when you begin working with PowerShell (at least v. 2.0) even in IDEs such as PowerGUI (http://powergui.org/index.jspa) and PowerSE (http://powerwf.com/products/powerse.aspx)

Unlike, Visual Studio which creates the Intellisense and autocompletion of the libraries at design time, most of the Intellisense in PowerShell is actually generated after some code has been executed.  Therefore, while you are simply typing and referencing libraries you will not have that wonderful autocompletion and dynamic help for object members like you have in Visual Studio.

Even worse, when you are referencing these libraries, due to the implicit typing of PowerShell, you will not be able to figure out the output of methods and properties when referencing libraries such as SharePoint within PowerShell.  This makes it particularly difficult to look up the appropriate reference APIs in the MSDN documentation or determine what is not working correctly when something breaks or throws an exception.

Unfortunately, while you cannot overcome ALL of the limitations of using PowerShell, you can certainly overcome some of the Intellisense problems (even if only partially).

So how do you do that?  By using the capabilities of Executing a Selection of code in PowerShell to load up the appropriate library and object references to display Intellisense in either PowerGUI or PowerSE.


In the above screenshot, I highlight a selection of PowerShell code and selected "Execute Selection" in PowerSE.  In this manner, I can get PowerSE (or PowerGUI depending on your preferred PowerShell editor of choice) to display the Intellisense for any subsequent SharePoint libraries that I need to use.


So while doing this for every few segments of code that you want to use is INCREDIBLY annoying, at least it is better than nothing and offers you some additional error checking while you are typing and referencing various SharePoint libraries as well as provides the added convenience of being able to leverage PowerShell scripts for executing various tasks in SharePoint without having to compile and deploy .NET assemblies to accomplish those same tasks.

NOTE: It is best to execute selections of code after every few lines that you type so that variables and their respective values remain in memory throughout the composition of your script.  Otherwise, you will have to create local variables and assign them values if the referenced variables have not already been loaded into PowerShell memory.  If the variables do not exist for a particular segment of PowerShell code, you will get an error/exception message just as you would with .NET (since PowerShell is based on .NET, this makes logical sense)

No comments:

Post a Comment