PowerShell  Web Part 

The PowerShellWebPart brings the power and flexibility of PowerShell to Microsoft SharePoint Server. The web part builds on the HTML template capabilities of PowerShellASP, providing a dynamic HTML content region powered by PowerShell scripts. It is similar in appearance and functionality to the standard SharePoint Content Editor Web Part, but far more powerful.

Using PowerShellWebPart

PowerShellWebPart is configured through the standard SharePoint Web Part property editor. The editor allows users to configure properties about the PowerShellWebPart and includes a pop-out input text area for PowerShellASP scripts. These scripts include the dynamic PowerShell ASP code that is executed when the Web Part is loaded.

Page authors can invoke as many instances of the Web Part as they like. Each instance is configured as a separate PowerShellASP Web Part template. The result, is a fully dynamic content region with low administrative overhead. Install PowerShellWebPart once and let your system administrators configure their own instances.

 

Check out the online web part demos to learn how you can use the PowerShellWebPart to quickly add dynamic content to your SharePoint sites using Windows PowerShell.   

 

Installing PowerShellWebPart

  1. Double-click the PowerShellWebPart setup utility to launch the installer. Click Next.
  2. Read the License Agreement and click "I Agree" to accept it.
  3. Change the destination folder, or accept the default installation path.
  4. Select the components you wish to install. Click Next.
  5. Select the Start Menu folder for the PowerShellWebPart shortcuts. Click Next.
  6. Click Install to complete the installation.
  7. The setup includes an option to automatically deploy PowerShellWebPart to the default SharePoint Server on the current machine.

After installing PowerShellWebPart, the specific components of the Web Part will need to be activated. To do this:

  1. Log into SharePoint as the SharePoint Site Administrator.
  2. Select Site ActionsSite SettingsSite Collection AdministrationSite Collection Features
  3. Click Activate next to the PowerShellWebPart to let Page Authors include the web part on their pages.

Prerequisites

PowerShellWebPart requires either:

  • Windows SharePoint Services 3.0 or above, or
  • Microsoft Office SharePoint Server (MOSS) 2007 or above.
  • Installation requires SharePoint Administrator privileges. Once the installation is complete, SharePoint Page Authors can configure and use the Web Part without administrative privileges.


    Working with the SharePoint Context

    PowerShellWebPart provides access to all of the major SharePoint constructs through the $spcontext object. The $spcontext object accesses the current SharePoint context and is identical to using the Microsoft.SharePoint.SPContext Class.

    The sample below demonstrates using PowerShellWebPart to list the documents available in shared documents:

    								<ul>
    								<%
    								   $docsCount = 0;
    								   $docList = $spcontext.Web.Lists['Shared Documents'];
    								   $docList.Items
    								   | sort @{ expression={$_['Modified']} } -descending
    								   | %{
    									  $docsCount = $docsCount + 1;
    									  if ( $docsCount -gt 5 ) {
    										 break;
    									  }
    								%>
    								   <li><a href="<%=$_.URL %>"> <%=$_['Name'] %> </a> (<%=$_['Modified'] %>)</li>
    								<% } %>
    								</ul>
    								

    Intrinsic Objects

    Besides running standard PowerShell code, you will want to interact with the HTTP runtime through the use of the ASP.NET Intrinsic objects like HttpRequest and HttpResponse. Because of the threading model used by PowerShell, those objects aren’t accessible directly through HttpContext.Runtime.

    • $Request: Contains the HttpRequest object.
    • $Server: Contains the HttpUtility object.
    • $Session: Contains the HttpSession object.
    • $Application: Contains the HttpApplication object.
    • $Response: Contains the HttpResponse object. You can write directly to the response stream from code if you want, or you can use write-host and friends as well.
    • $Cache: Contains the HttpCache object.
    • $Context: The HttpContext object associated with the current request.

    Using these objects works exactly the same as in regular ASP.NET applications. The following script will dump all of the values in the HttpRequest object to a simple HTML page:

        <% $request.params | %{
    	    write "$_ = $($request[$_])<br/>"
    	} %>
    

    As you can see, this sample uses less of the templating capabilities and instead uses simple PowerShell expressions to generate the output.