Thursday, February 25, 2010

How can we create a new SharePoint 2010 Site using Power Shell scripts?

Step 1: We need to create a Power Shell script file, say, createsite.ps1

Write-Host
# define variables for script
$SiteTitle = "My First Site using Power Shell"
$SiteUrl = "http://www.mydomain.com/sites/SiteName"
$SiteTemplate = "STS#1"

# check to ensure Microsoft.SharePoint.PowerShell is loaded
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}

if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snapin"
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}

# delete any existing site found at target URL
$targetUrl = Get-SPSite | Where-Object {$_.Url -eq $SiteUrl}

if ($targetUrl -ne $null)
{
Write-Host "Deleting existing site at " $SiteUrl
Remove-SPSite -Identity $SiteUrl -Confirm:$false
}

# create new site at target URL
Write-Host "Creating new site at " $SiteUrl
$NewSite = New-SPSite -URL $SiteUrl -OwnerAlias Administrator -Template $SiteTemplate -Name $SiteTitle
$RootWeb = $NewSite.RootWeb

# display site info
Write-Host
Write-Host "A new SharePoint Site has been created successfully" -foregroundcolor Green
Write-Host "-------------------------------------" -foregroundcolor Green
Write-Host "Site URL:" $RootWeb.Url -foregroundcolor Yellow
Write-Host "Site ID:" $RootWeb.Id.ToString() -foregroundcolor Yellow
Write-Host "Site Title:" $RootWeb.Title -foregroundcolor Yellow
Write-Host "-------------------------------------" -foregroundcolor Green

Step 2: To execute this script, you can create a bat file, say, setup.bat and execute the batch file

powershell -Command "& {Set-ExecutionPolicy bypass}" -NoExit
powershell -Command "& {.\createsite.ps1}" -NoExit
pause

Wednesday, February 24, 2010

Free SharePoint 2007 Video Tutorials

Today I was googling to find some video tutorials so that the begenners can have some initial understandings on SharePoint and found the following sites really helpful. And above all they are absolutely free to access.

http://sptraining.sharepointsite.net/

Thursday, February 18, 2010

How to add dynamic silverlight controls in runtime?

In Silverlight, While it is possible to create all your controls and objects in code, best practices dictate that it is usually better to do so in Xaml. The most compelling reason is that Xaml is highly "toolable" - that is, it lends itself to round-trip modification in tools such as Visual Studio and Expression and thus is easier to scale, modify and maintain.

On the other hand, there are times that you can't know at design time which or how many objects you'll need, and the ability to create objects dynamically can be a fundamental requirement.

So let's create a Silverlight page and add the following:

<Canvas x:Name="myCanvas">
<Button x:Name="myButton" Content="Hello" Canvas.Left="10" Canvas.Top="10"/>
<Button x:Name="Another" Content="Add Another" Canvas.Left="10" Canvas.Top="50" />
</Canvas>

The objective is to add a button to the page with the words "Add Another." When the user clicks on this button, we want to add a button to the UI and we want that button to have its own size, position and behavior. We do all of that in the code behind.

The first thing to do is to add an event handler for the new button in the Page Load event,

Another.Click += new RoutedEventHandler(Another_Click);

And to add its implementation, in which you'll create a new button and set its properties,

void Another_Click(object sender, RoutedEventArgs e)
{
Button b = new Button();
b.Content = "I live!";
b.SetValue(Canvas.LeftProperty, 10.0);
b.SetValue( Canvas.TopProperty,
this.newButtonPosition);
this.newButtonPosition += 30.0;
b.Width = 100;
b.Height = 20;
b.Click +=new RoutedEventHandler(new_button_click);
myCanvas.Children.Add(b);
}

void new_button_click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
btn.Content = "Don't do that!";
btn.IsEnabled = false;
}

Because the canvas's Left and Top are not actually properties of the Button (but are extended properties) you set them with the SetValue method, which takes two parameters. The first is the actual name of the property you want to set (Canvas.LeftProperty and Canvas.TopProperty) which is not difficult to find as Intellisense will supply it) and the second is the value (in this case a double).

Also we have added an event handler registration to the button. This means that every button that is added is registered to call new_button_click when it is clicked. As the buttons are clicked, they change their message and become disabled.

Thursday, February 4, 2010

Anonymous Types

The ability to create new types 'on the fly' is done using Anonymous Types.

All of this happens at compile time, so anonymous types are still strongly typed. In reality, the compiler automatically creates a class that represents the anonymous type.

Example: var product = new {Name = "C# Rocks!", Price = 3};

-> When an anonymous type is assigned to a variable, that variable must be initialized with the var construct.

-> Anonymous types are reference types that derive directly from object.

-> If two or more anonymous types have the same number and type of properties in the same order, the compiler treats them as the same type and they share the same compiler-generated type information.

-> Anonymous types cannot contain unsafe types as properties.

Note: While anonymous types might not seem all that useful on the surface, they are the cornerstone for LINQ. Without anonymous types, LINQ would not be able to create types dynamically. This is what allows LINQ to query for an arbitrary set of data, without first having the structure declared.

Implicitly typed local variable in C#

Implicitly typed local variable is a variable that can be declared without specifying the .NET type explicity. The type of that variable will be inferred by the complier from the expression on the right side of initialization statement.

Examples:

var i = 5;

When the compiler sees this in the code, it tries to figure out the type of the variable based on the value you assigned it. Hence, if you call GetType() on i, the type returned is actually System.Int32.

The following will give you an error because the compiler can't figure out what type the variable is:

var i = null;

You have to initialize the variable with something that the compiler understands so it can figure out the type. Calling GetType() on firstName here would give you System.String:

var firstName = "David";

Limitations:

-> The declarator must include an initializer. Unlike normal declarations, you can’t declare the implicitly type variable without initializing.

-> The compile-time type of the initializer expression cannot be the null type.

-> If the local variable declaration includes multiple declarators, the initializers must all have the same compile-time type.The implicitly-type local variable cann’t be initialized with different types more than one time. You can’t assign the string to varaible “test” after initializing with integer value “1".

-> Cannot initialize an implicitly-typed local variable with an array initializer like var test = { 1, 2, 3 }; Can only use array initializer expressions to assign to array types, like a new expression var test1 = new[] { 1, 2, 3 };

-> an implicitly typed local variable cannot be Nullable variable which means the following syntax to create a Nullable variable in C# will not work with implicitly typed variables - var? age = null;

-> Implicit Typed Variable cannot be used as class members such as field. They can only be used as local variables inside methods.

Extension Methods

It is a special kind of static method that allows you to add new methods to existing types without deriving, recompiling or modifying the original type. The extension methods are called as if they were instance methods from the extended type.

public static class MyExtensionClass
{
public static int Multiply(this int num)
{
return num * num;
}
}

class Program
{
static void Main(string[] args)
{
int i = 3;
Console.WriteLine(i.Multiply());
Console.ReadLine();
}
}

Here are the few things we should keep in mind while using Extension Methods:

-> This keyword has to be the first parameter in the extension method parameter list. The type of the first parameter will be the type that is extended.
-> Extension methods can't access the private methods in the extended type.
-> Extension methods can be defined only in a static class.
-> If you want to add new methods to a type, you don't have the source code for it, the ideal solution is to use and implement extension methods of that type.
-> If you create extension methods that have the same signature methods inside the type you are extending, then the extension methods will never be called. Extension methods cannot be used to override existing methods.
-> If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced i.e. extension methods.
-> The concept of extension methods cannot be applied to fields, properties or events.