BANGALORE, INDIA: Web parts are one of the most important features of SharePoint. In SharePoint 2010 along with Visual Studio 2010, you get the ability to create new webparts visually rather than completely in code. We will take a look at doing this in this article. We will also explore the new LINQ to SharePoint capability – allowing developers to query SharePoint data using Language Integrated Query rather than writing failry obtuse CAML code.
Visual Web Parts
Visual Web Parts are a project type in Visual Studio 2010 that allow you to create Web parts quickly and easily. Open up VS2010 and create a new “Visual Web Part” solution. When prompted, enter the URL of the SharePoint site to which this Web Part will belong. Once the project is created, expand the “VisualWebPart1” element in Solution Explorer. Open the VisualWebPart1.webpart file. Change the Title property to “My LINQ Web Part” or something appropriate. Now open up the VisualWebPart1.ascx file, add some appropriate SharePoint or ASP.NET controls in it, add some code behind in the .cs file and compile the project. For instance, I added a SharePoint DateTimeControl and a label and added some code to display the selected date in the label every time. Right click the solution in VS2010 and select “Deploy”. This will build the solution and deploy it to the SharePoint site you had configured in the beginning. Browse over to this site ,and on a page click the “Edit page” button on the Ribbon. Select your Webpart from the “Custom” folder and click the Add button. You will see the Web part showing up on the page. Simply save the page and then interact with your Web part. It should work just fine.
For instance in my Web Part, the .ascx file looks like this:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="LinqVWP.VisualWeb Part1.VisualWebPart1UserControl" %>
asp:Label>
The last two lines are the ones that specify the SharePoint date time control and a label. The code behind for this looks like this:
protected void Page_Load(object sender, EventArgs e)
{ DateTime dt = DateTime.Now;
if (!String.IsNullOrEmpty(Request.QueryString["date"]))
dt = DateTime.Parse(Request.QueryString["date"]);
DateTimeControl1.SelectedDate = dt;
}
protected void Date_Changed(object sender, EventArgs e)
{
Label1.Text = DateTimeControl1.SelectedDate.ToString();
When this WebPart is run, it displays the selected Date and Time in the label.
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.