iLoveSharepoint Power Webpart allows Sharepoint 2010 to inject Powershell scripts directly to Sharepoint portal pages.

As behind Powershell is all the power of .NET framework(and not only that), it allows you to create interesting solutions, without opening Visual Studio and WSP deployment.

Let’s create some function in Powershell. For example – will greet the user depending on the time of day. Will print name of current user in friendly format.


# attachSharePoint snap-in in safe way
$sharePointSnapin =Get-PSSnapin | Where-Object { $_.Name-eq  "Microsoft.SharePoint.PowerShell"}
if($sharePointSnapin-eq $null)
{Add-PSSnapin Microsoft.SharePoint.PowerShell    
}


# say helloin accordance with the time of the day
FunctionGetGreeting()
{
    #curent time
    $actualtime = (Get-Date).TimeOfDay;

    # select time interval
    $evening =New-TimeSpan-Hours 17-Minutes 30-Seconds 0
    $morning =New-TimeSpan-Hours 8-Minutes 0-Seconds 0
    $dinner =New-TimeSpan-Hours 11-Minutes 0-Seconds 0
    $night =New-TimeSpan-Hours 22-Minutes 0-Seconds 0
    
    if ($actualtime-ge $morning-and $actualtime-le $dinner)
    {
        return "Good morning";
    }
    if ($actualtime-ge $dinner-and $actualtime-le $evening)
    {
        return "Good day";
    }
    if ($actualtime-ge $evening-and $actualtime-le  $night)
    {
        return "Good evening";
    }
    if ($actualtime-ge  $night-or $actualtime-le  $morning)
    {
        # let's imagine for complete example
        return "Good night :)";
    }
}

# To get display name we can make requests to the DC, but 
# Login User in current DC is in "Domain\Name.Surname" format
# so we can get friendlyDisplayName right here
FunctionGetDisplayName($name)
{
    if($name-match "\\")
    {
        $name = $name.remove(0, $name.LastIndexOf('\')+1);
    }
    if($name-match ".")
    {
        $name = $name.replace("."," ");
    }
           
    return $name
}

$hello = (GetGreeting)+ ", " + (GetDisplayName-name ([Environment]::UserName))


#render html
function Render($writer)
{
    $writer.Write("</pre>
<h1>$hello</h1>
<pre>")
}

The result of executing:

image

So we got dynamic behavior in Sharepoint, without writing code in VS and deploying WSP solutions

Click below to expand comments form..