Extend SharePoint Portals With Dynamic Content
Extend SharePoint Portals With Dynamic Content
  • Quickly add dynamic Web content to SharePoint Portals with PowerShell ASP templates.
  • Develop dynamic content with easy-to-configure Web Part templates based on (HTML, XML, etc.) and inline PowerShell code.
  • Configurable through a simple 'Wiki-like' editing experience, no Visual Studio development required.
Download Free Trial

The PowerShell Web Part is a highly customizable SharePoint Web Part that allows users to customize their SharePoint portals with dynamic content. The PowerShell Web Part provides a container for PowerShell ASP script which is executed just before rendering, bringing the connectivity capabilities of PowerShell and the .NET Framework to the end user.

Using The PowerShell Web Part

PowerShell Web Part is configured through the standard SharePoint Web Part property editor. The editor allows users to configure properties about the PowerShell Web Part and includes a pop-out input text area for PowerShell ASP 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 PowerShell ASP Web Part template. The result, is a fully dynamic content region with low administrative overhead. Install PowerShell Web Part once and let your system administrators configure their own instances.

 

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

Installing PowerShell Web Part

  1. Double-click the PowerShell Web Part 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 PowerShell Web Part shortcuts. Click Next.
  6. Click Install to complete the installation.
  7. The setup will automatically deploy and activate the PowerShell Web Part to the default SharePoint Server on the current machine.
Prerequisites

PowerShell Web Part requires either:

  1. Windows SharePoint Services 3.0 or above, or
  2. 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

PowerShell Web Part 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 PowerShell Web Part 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.