Vertically align divs in the middle when using Bootstrap

By default bootstrap aligns everything to the top so when you have an html like below the container div will be top aligned leaving white space at the bottom.

            <div id="main">
                <div class="container">
                    <div class="row">
                       ........
                    </div>
               </div>
            </div>

 The container can be easily aligned vertically middle using java script. First we need to put entire div inside another div and then set the top-margin of the outer div using code below

        $(window).on('load resize', function () {
            var windowheight = $(window).height();
            var mainheight = $('#main').height();

            var diffheight = windowheight - mainheight - 100;
            if (diffheight > 0) {
                $('#main').css('margin-top', diffheight / 2);
            } else {
                $('#main').css('margin-top', 0);
            }
        });

Redirect to a different page when using sign in as different user in SharePoint 2010

The need was to redirect users to a default landing page of our site collection whenever they used "Sign in as Different User" option from any page in the application.

Following code snippet can be used which used SharePoint java script object model to get site collection URL and then replaces LoginAsAnother action method with a Source Url passing the second paramater as 1 which tells SharePoint to not append Source url (otherwise it will add the current page Url to the Source query string paramater in the URL)

<script>

function GetSiteUrl()
  {
    var ctx = new SP.ClientContext();
    this.siteCol = ctx.get_site();
    ctx.load(this.siteCol);
    ctx.executeQueryAsync(Function.createDelegate(this, setLoginAsAnother), Function.createDelegate(this, onFailed));
  }

function setLoginAsAnother()
  {
    var objects = document.getElementsByTagName("ie:menuitem");
    for (var i = 0; i < objects.length; i++) {
    itm = objects[i];
    if ($('#' + itm.id).attr("text") == "Sign in as Different User")
    {
        var newurl = "\u002f_layouts\u002fcloseConnection.aspx?loginasanotheruser=true&Source=" + this.siteCol.get_url();
        $('#' + itm.id).attr( "onMenuClick", "javascript:LoginAsAnother('"+newurl + "', 1)");
    }
    }

  }

    function onFailed()
    {
       //alert('Failed to retrieve the server relative URL.');
    }

ExecuteOrDelayUntilScriptLoaded(GetSiteUrl, 'sp.js');

</script>


Quick XAML Overview


Extensible Application Markup Language (XAML) is a markup language for declarative application programming. XAML is XML-based language created by Microsoft which can be used to initialize structured values and objects. Windows Presentation Foundation (WPF) implements a XAML processor implementation, and provides XAML language support. If you are familiar with HTML tag syntax, you would find it easy to jump into XAML territory. Since XAML is XML based, it must be well-formed and must adhere to rules of case sensitivity, open/closing tags etc.

A very simple example to make things clear would be to understand a code snippet below
<Canvas>
  <TextBlock>Hello World!!!</TextBlock>
</Canvas>


As you see you could simply misunderstand it for some XML which is what it is. There will definitely be some essential code blocks that you would be required to include in every XAML file but the core content can be as simple as the code above.

 XAML follows Object Element Syntax that instantiates a CLR class or structure by declaring an XML element. It resembles element syntax of other markup languages such as HTML where you define an element by putting the type name of the class or structure between left (<) and right (>) angle brackets. Each element has then to be closed by using one of the two following methods.
<Button x:Name="Submit"/>
<Button x:Name="Submit"></Button>

The example above instantiates a new instance of the Button class, and also specifies a Name attribute and a value for that attribute.

Another example:
<TextBox>This is a test text box</TextBox>

I am assuming that readers would have a good understanding of HTML and XML to follow the examples here. If you don’t, please take a quick read at

Each element or control has properties which can be further used to customize the control. The properties can be of following types

1.       Attribute Syntax
2.       Property Element Syntax
3.       Content Element Syntax
4.       Collection Syntax

Now let’s dig deeper into each of the four types.

1.       Attribute Syntax

The control Textbox below has “Width” attribute whose value has been set to 100.
<TextBox x:Name=”textBox” Width=”100”/>
The important thing to remember here is x:Name attribute. This attribute can be used to apply a unique identifier to the control. This value of x:Name attribute can be used to refer to the control in the code behind. How? We will see shortly.

2.       Property Element Syntax

In the example above we saw attributes defined inline for the control. What if we could define it as a child of the control? This is called Property Element Syntax. The example below would help you understand the difference.
<TextBox x:Name=”textBox” Width=”100”/>
                < TextBox.Name >textBox</ TextBox.Name >
                < TextBox.Width>100</ TextBox.Width >
</TetxBox>

3.       Content Element Syntax

This syntax is used to define the content between the tags as below
<TextBlock>A crash course in XAML</TextBlock>

4.       Collection Syntax

This is used for attributes where the value type is a collection. For ex
     <LinearGradientBrush>
       <LinearGradientBrush.GradientStops> 
         <GradientStopCollection> 
           <GradientStop Offset="0.0" Color="Red" /> 
           <GradientStop Offset="1.0" Color="Blue" /> 
         </GradientStopCollection> 
       </LinearGradientBrush.GradientStops> 
     </LinearGradientBrush> 

You can see LinearGradientBrush control contains a collection of GradientStop objects.

Events in XAML

Like ASP.NET XAML controls also have events which can be used for server side processing.

<Button Click="Button_Click" >Date (Event on Root)</Button>

The code-behind must contain the event handler function as below

private void Button_Click(object sender, MouseEventArgs e)
{
}


The event handler function has two parameters. The first value is sender, which is a reference to the object where the handler is attached. This value can be cast to a more precise type.

The second value is event data, which generally appears in signatures as the e parameter. The event data is captured as an instance of EventArgs class or a class that inherits EventArgs. For input events Silverlight has support for mouse and keyboard events. If you notice in the example above the event data was captured as MouseEventArgs. For keyboard events, key presses on the keyboard raise the same KeyUp and KeyDown events. In order to determine which key was pressed, you must access the KeyEventArgs that is available to the event handler.
 
Note:  XAML is not the only way to add event to a control. The event handlers can also be added in managed code (in C# or VB) using += operator.
In order to add event to the control below
<TextBlock Name="textBlock1">Put the mouse over this text</TextBlock>

Simply add the statement below in page load
textBlock1.MouseEnter += new MouseEventHandler(textBlocks_MouseEnter);
Make sure to define textBlocks_MouseEnter method in the managed code.

In certain circumstances you may want to programmatically remove the event handlers which is again a simple statement using -= operator.
textBlock1.MouseEnter -= textBlocks_MouseEnter;

This was a quick crash course of events in XAML. For now this would serve our purpose. We will take up more concepts as we dig deeper into the application we are trying to build while we learn Silverlight. For a more detailed study I would advise to visit MSDN. 
http://msdn.microsoft.com/en-us/library/cc189018%28v=vs.95%29.aspx#Y1125

Now think about a real world scenario and the events you would use more frequently. You will be pressing/releasing a key/mouse, moving mouse, drag/drop action, bringing a control to focus or lose focus on a certain control. I am putting a list of most common events whose names are self-explanatory and I won’t be giving any explanation. I will explain the events in detail when we jump to code.
 

  •     KeyDown
  •     KeyUp
  •     GotFocus
  •     LostFocus
  •     MouseLeftButtonDown
  •     MouseLeftButtonUp
  •     MouseRightButtonDown
  •     MouseRightButtonUp
  •     MouseMove
  •     MouseWheel
  •     BindingValidationError
  •     DragEnter
  •     DragLeave
  •     DragOver
  •     Loaded
  •     BindingValidationError
 After a brief introduction of XAML we will jump to create our first Silverlight application. Though we should now be comfortable with basic concepts of XAML, the actual details of Silverlight controls and event types which can be used with the controls are yet to be discovered. The best way would be to create applications, add controls and review them. Once we have created and run a few sample applications we should be comfortable with using them in general.

Introduction to Silverlight

Silverlight is a powerful platform to develop rich, web-based internet applications. Initially seen as a competitor to Adobe flash it has evolved as a powerful tool for developers to leverage .NET framework to create real time dynamic applications by integrating with other .NET technologies. Silverlight applications are built using XAML (a declarative markup language; stands for Extensible Application Markup Language) with code-behind files in a .NET language like C# or VB. Just like Adobe Flash browser plug-in, users are required to install a Silverlight browser plug-in before the application can be previewed. Users are given a link to install browser plug-in if it is not already installed when connecting to a Silverlight application.
 

Developer Tools


Visual Studio 2010 comes packaged with Silverlight specific templates. You will also have to install Silverlight runtime for developers before you can create a Silverlight application in VS 2010. 

Link to download Silverlight Developer Runtime

Visual Studio 2010 comes with Silverlight 3. Install "Silverlight 4 Tools for Visual Studio" which will add  Silverlight 4 tools and components to VS 2010. If you do not install "Silverlight 4 Tools for Visual Studio" you will get only one option for Silverlight version when creating a Silverlight application. After installing "Silverlight 4 Tools for Visual Studio" you would have a choice to select either Silverlight 3 or Silverlight 4 as Silverlight application version. You also get additional templates and more importantly themes with Silverlight 4 version.

Alternatively you can use Visual Studio 2008 SP1 where you can add Silverlight tools/templates as add-ons. We will discuss additional tools as and when they are required.

Read data from CSV and create hashtable in PowerShell

Let us assume we have a CSV file with data in two columns and we need to read the data and create a hashtable for use in PowerShell. The first row in CSV is a header row with values as "Feature" and "Type".

First step is to read the CSV file using the line below
$t = Import-Csv -Path 'C:\Temp\features.csv' -Header "Feature","Type"

Now create a blank hashtable
$HashTable = @{}

Iterate through the CSV rows and add the corresponding data to hashtable as following
foreach($r in $t)
{
    Write-Host $r.Feature $r.Type
    $HashTable.Add($r.Feature,$r.Type)
}