Monday, March 17, 2008


Language-Integrated Query (LINQ) is a set of features in .NET Framework 3.5 (Visual Studio 2008) that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio 2008 includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.

In Visual Studio you can write LINQ queries in Visual Basic or C# with SQL Server databases, XML documents, ADO.NET Datasets, and any collection of objects that supports IEnumerable or the generic IEnumerable<(Of <(T>)>) interface. LINQ support for the ADO.NET Entity Framework is also planned, and LINQ providers are being written by third parties for many Web services and other database implementations.
You can use LINQ queries in new projects, or alongside non-LINQ queries in existing projects. The only requirement is that the project target version 3.5 of the .NET Framework.

There are different variants of LINQ as listed below:
LINQ to Objects
LINQ to ADO.NET (Dataset)

LINQ to SQL is a component of .NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects.

In LINQ to SQL, the data model of a relational database is mapped to an object model expressed in the programming language of the developer. When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution. When the database returns the results, LINQ to SQL translates them back to objects that you can work with in your own programming language.

See also

The DataContext
The DataContext is the main conduit by which you retrieve objects from the database and resubmit changes. You use it in the same way that you would use an ADO.NET Connection. In fact, the DataContext is initialized with a connection or connection string you supply. The purpose of the DataContext is to translate your requests for objects into SQL queries made against the database and then assemble objects out of the results. The DataContext enables language-integrated query by implementing the same operator pattern as the standard query operators such as Where and Select.

Implementing LINQ to SQL
LINQ to SQL is best implemented in a project using the built-in Object Relational Designer (O/R Designer) in Visual Studio 2008. O/R Designer auto generates the DataContext class and entity classes for all the tables in a specified database.

To launch it,
Visual Studio 2008 > Projects > Add New Item > Visual C# (Category) > LINQ to SQL Classes (Template)
(Eg Name: Northwind)

This will open a blank O/R Designer. Now open Server explorer and add a new data connection to the required database. Drag and drop the required tables to the O/R Designer and it will automatically generate all needed classes.

The DataContext class will be named as NorthwindDataContext and this can be used to do operations against the database.

Selecting Data

//Create the DataContext object
NorthwindDataContext db = new NorthwindDataContext();

//Write the Select Query
var prods = from p in db.Products
select p;

//Use the output in various ways
foreach (Product prod in prods)

//assigning as data source to a grid view
gvMain.DataSource = prod;

Updating Data

//Getting one row from the Products table
Product prod = db.Products.Single(p => p.ProductName == "First Prod");

//Updating two of its columns
prod.UnitPrice = 25;
prod.UnitsInStock = 50;

//Submitting the changes to database

Inserting Data

//Creating a new category
Category cat = new Category();
cat.CategoryName = "New Test Category";
cat.Description = "This is a new type of category";

//Creating two new products
Product p1 = new Product();
p1.ProductName = "First Prod";

Product p2 = new Product();
p2.ProductName = "Second Prod";

//Add the products to new category

//Add category to database and save changes

Deleting Data

//Get the required data to delete
var toyProds = from p in db.Products
where p.ProductName.Contains("toy")
select p;

//Delete it from database

So start using LINQ and refer the 101 samples at to help you with any syntax issues.

Thursday, March 13, 2008

SharePoint - Web Part Deployment

This post is applicable to Microsoft Office SharePoint Server (MOSS) 2007. See my previous posts on Installing SharePoint, Creating Web Site, and Developing Web Parts.

This walk-through provides the steps for deploying a basic custom SharePoint Web Part by either copying the assembly dll to the Server (if not strong named) or by deploying to GAC (if strong named) and configuring it through SharePoint administration site.

1. Copy the web part dll file to the bin directory on the server’s web location if the assembly is not strong named.
a. Log into the computer where SharePoint Server is installed.
b. Copy the web part dll (HelloWebPart.dll) to the SharePoint web’s bin directory. Usually this location will be at C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin.
c. If the SharePoint was installed on a different port (say 37729), this location will be at C:\Inetpub\wwwroot\wss\VirtualDirectories\37729\bin.
d. This location can be easily found from the IIS Manager (Locate the appropriate portal, for which u want to deploy the web part, identified with the port number. Right click and have Properties. Under the Home Directory Tab, note the path in Local path text box).
e. If the bin folder doesn’t exist at this location, then you can create one.
2. If the assembly is strong named, deploy it to the GAC. (Note that either Step-1 or Step-2 is required; not both.)
a. Log into the computer where SharePoint Server is installed.
b. Drag and drop the web part dll to the C:\Windows\Assembly folder using windows explorer. This will install the assembly to Global Assemble Cache. After that, right click on the assembly name from here and choose Properties. Copy the Public Key Token value for using in the configuration.
3. Add a Safe Control entry in the web configuration file of SharePoint Server.
a. Open the web.config file from the server’s web location which will be at C:\Inetpub\wwwroot\wss\VirtualDirectories\80\ or at a port number as mentioned above.
b. Add a new entry within the section as below:

<SafeControl Assembly="HelloWebPart" Namespace="HelloWebPart" TypeName="*" Safe="True" />

This indicates that all(*) classes in the HelloWebPart namespace in the HelloWebPart assembly are safe web parts.

If the assembly is strong named, it should have the Public Key Token copied from the previous step.

<SafeControl Assembly="ControlSampleWebPart, Version=, Culture=neutral, PublicKeyToken=c627d6df943a8393" Namespace="ControlSampleWebPart" TypeName="*" Safe="True" />

c. Save the web.config file and close it.
4. Configure the server to use this new web part.
a. Open the SharePoint Central Administration site from a browser using http://servername or http://servername:port ensuring that the current logged in user has the administrative rights on the portal site.
b. Go to Site Actions > Site Settings (top right menu button).
c. Go to Galleries > Web Parts to open the web part gallery.
d. Click on the New button.
e. The New Web Parts page displays all the web parts marked as safe on the server. Scroll down to the required web part in the list, check the check box on the left and click on the Populate Gallery button at the top of the page. This will result in the Web Part entry creation in the Web Part Gallery list, and hence it can be used from now on from the gallery. Notice that the Web Parts developed in latest .NET Frameworks has .webpart as extension while the older ones has .dwp as extension.
5. Add this web part to a page in a site.
a. Go to Site Actions > Edit Page (top right menu button). This displays the current page in edit mode.
b. Click on the Add a Web Part button on the desired section of the page.
c. Select the required web part (Hello) from the All Web Parts > Miscellaneous section of the web part gallery.
d. Click on the Add button.
e. Click on the Exit Edit Mode link on the top right corner to exit the edit mode of this page.

Wednesday, March 12, 2008

Developing SharePoint Web Part using Visual Studio

Refer my earlier posts on installing Microsoft Office SharePoint Server (MOSS) 2007 and creating website if you need more information.

This walkthrough provides the steps for creating a basic custom SharePoint Web Part using Visual Studio 2008 or 2005. It is a very simple Web Part that displays the logged-in user name with a Hello message.

1. Create a new Visual C# Class Library Project in Visual Studio.
  • Start Visual Studio.
  • On the File menu, point to New, and then click Project.
  • In the New Project dialog box, click Visual C# Project Type, and then select the Class Library template.
  • Type HelloWebPart as the name and specify the location for the project files, and then click OK.
2. Add reference to System.Web..
  • On the Project menu, click Add Reference.
  • On the .NET tab, double-click System.Web.
  • Click OK.
3. Add Namespace Directives.
  • First, rename the class file from Class1.cs to Hello.cs (Confirm by clicking yes).
  • Add the following using directives near the top of your code.
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
4. Inherit From the WebPart Class.
  • Derive the Hello class from System.Web.UI.WebControls.WebParts.WebPart class.
    namespace HelloWebPart
    public class Hello: WebPart

5. Override the RenderContents base class method.
  • This method is used to render contents on the web part. Here we are displaying a Hello message to the current user.
    protected override void RenderContents(HtmlTextWriter writer)
    writer.Write("Hello, " + this.Context.User.Identity.Name);
6. Build this project to get the assembly HelloWebPart.dll
If we need to render a control instead of contents, override the CreateChildControls and RenderControl method as shown below:
     protected override void CreateChildControls()
//Create a Calendar control and set its properties.
Calendar helloCalendar = new Calendar();
helloCalendar.Enabled = true;
helloCalendar.ShowGridLines = true;
helloCalendar.ShowTitle = true;
helloCalendar.EnableViewState = true;
helloCalendar.SelectedDate = DateTime.Now;

//Add this Calendar to the Web Part.
public override void RenderControl(HtmlTextWriter writer)
In the above example, we are creating a calendar control and setting its properties and then adding it to the web part. The RenderControl method is used to call the RenderChildren method, which causes the children controls to be rendered on the particular HtmlTextWriter passed as a parameter to the method.

If this assembly needs to be strong named (to enable GAC deployment), follow the below steps:
  • Launch the Visual Studio Command Prompt.
  • Type the following command to generate a key pair file:
    sn –k c:\keypair.snk
  • That will generate the key pair file at c:\
  • Refer this file in the web part project’s AssemblyInfo.cs file as below:
    [assembly: AssemblyKeyFile("c:\keypair.snk")]
  • Make sure that the version is also specified here as below:
    [assembly: AssemblyVersion("")]
    [assembly: AssemblyFileVersion("")]
  • Build the project to generate a strong named assembly.
If you are using Visual Studio 2008 or higher, you can can skip all these steps and then go to the Signing tab of the project properties, and choose 'Sign the assembly' option.
We will see how to deploy this web part on SharePoint Server in the coming posts.

Tuesday, March 11, 2008

SharePoint - Creating Web Site

A new web site can be created in Microsoft Office SharePoint Server (MOSS) 2007 easily by following below steps:
1. Launch the SharePoint Central Administration site from a browser using http://servername or http://servername:port.
2. Go to Site Actions > Site Settings (top right menu button).
3. Go to Site Administration > Sites and Workspaces.
4. Click on the Create button.
5. Provide a Title, URL Name, and select a Template. (Choose Team Site for a team web site)
6. Choose the Permissions and Navigation Styles. (Leave it at the default values.)
7. Click on the Create button to create the site.

Web Parts are the building blocks of a SharePoint site. You can add a web part to an existing page in the site by editing the page. Whatever web parts installed and configured on the server will be available to use in any pages.
1. Go to Site Actions > Edit Page (top right menu button). This displays the current page in edit mode.
2. To delete an existing web part, click on the Edit > Delete menu of that web part.
3. To add a new web part, click on the Add a Web Part button on the desired section of the page. All the available web parts will be populated in a popup window. Choose the required one and click the Add button.
4. Click on the Exit Edit Mode link on the top right corner to exit the edit mode of this page.

You can add more pages to the site following below steps:
1. Go to Site Actions > Create (top right menu button).
2. Click on Web Pages > Basic Page link.
3. Provide a Name, and it will be saved under the default Document Library called Shared Documents.
4. Click on the Create button. This will create a blank page where you can add contents.
More such items can be added to a site to make it more interactive. For example, the Wiki Page Library creates a set of wiki pages where the end user can edit the contents of the pages.

Monday, March 10, 2008

MOSS (Microsoft Office SharePoint Server) 2007

Today I got some time to see MOSS in action. Microsoft Office SharePoint Server 2007 is a new server program that is part of the 2007 Microsoft Office system. We can use Office SharePoint Server 2007 to facilitate collaboration, provide content management features, implement business processes, etc.

You need Windows Server 2003 to get this installed. Choose the Basic option to install a stand alone version. Advanced option is to install this in a server farm. You get to choose specific SQL Server in this case. There are no other things to remember during installation. Installation automatically creates a new instance of SQL Server (\OfficeServers) and around seven databases.

After the installation, configuration window pops up. Everything is configured automatically including all the services required for MOSS.

The server can be administered from the admin web site through the shortcut at:
Start > Programs > Microsoft Office Server > SharePoint 3.0 Central Administration
or directly accessing it from a browser using http://servername:port

Reset the Internet Explorer Security to Low (Tools > Internet Options > Security > Custom Level > Reset to Medium-low) to enable all the administrative tasks in MOSS.

Adding other Administrators:
In the admin portal, we can add more admins to the site using following link:
Site Actions > Site Settings > Site collection administrators

The users are picked from the active directory of the server's domain.

Creating a web site with multiple web parts (items like calendar, discussion forum, etc) is also as easy as clicking few links from the admin portal.

Saturday, March 08, 2008

Silverlight – Lighting up the web!

“Microsoft Silverlight is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive applications for the Web.

By using Expression Studio and Visual Studio, designers and developers can collaborate more effectively using the skills they have today to light up the Web of tomorrow.”

That’s the quote from Microsoft on about this new technology that takes the richness and power of WPF to the web world.

When I first learnt about Silverlight, the available version was 1.0 and was using Javascript as its coding language. After knowing that .NET support is on its way and getting chance to try my hands on the pre-release builds, I waited for version 2.0 to come. The beta version of Silverlight 2.0 was released last week.

Silverlight is a browser plug-in that allows the browser to use some of the user’s local computational horsepower. The plug-in has no external dependencies. For example, Mac users do not need Windows Media Player to play back WMV content. We are already familiar with plug-in technologies like Java Applets and Flash applications. But Silverlight brings all the power of WPF in animation, graphics, video, etc. And with version 2.0, the CLR (Common Language Runtime) is going cross platform and developers can use their favorite .NET language to write code that runs on the client from within a browser.