In this task, you create a Visual Web Part SharePoint 2010 project in Microsoft Visual Studio 2010.
Desired to gain proficiency on SharePoint? Explore the blog post on the "SharePoint Training" course to become a pro in SharePoint.
In this task, you add SharePoint and ASP.NET controls to the Web Part. Then you add code to query the Projects list and return those items that are less than or equal to a specified date.
XML <SharePoint:ListViewByQuery runat="server" ID="ProjectsListView" /> <br /> <asp:Label ID="Label1" runat="server" Text="Items due by:" /> <SharePoint:DateTimeControl ID="DueDate" AutoPostBack="true" OnDateChanged="OnDate_Changed" DateOnly="true" runat="server"> </SharePoint:DateTimeControl>
using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using System.Web;
DateTime dueDate = DateTime.Now; if (!string.IsNullOrEmpty(Request.QueryString["date"])) { dueDate = DateTime.Parse(Request.QueryString["date"]); } DueDate.SelectedDate = dueDate; SPWeb home = SPContext.Current.Web; ProjectsListView.List = home.Lists["Projects"]; SPQuery query = new SPQuery(ProjectsListView.List.DefaultView); query.ViewFields = "<FieldRef Name="Title" /><FieldRef Name="Due_x0020_Date" />"; query.Query = string.Format( "<Where><Leq><FieldRef Name="Due_x0020_Date" /><Value Type="DateTime">{0}</Value></Leq></Where>" , dueDate.ToString("s")); ProjectsListView.DisableFilter = true; ProjectsListView.DisableSort = true; ProjectsListView.Query = query;
protected void OnDate_Changed(object sender, EventArgs e) { SPUtility.Redirect(SPContext.Current.ListItem.Name , SPRedirectFlags.Default , HttpContext.Current , string.Format("date={0}", DueDate.SelectedDate.ToString("d"))); }
When the project deploys successfully, go to the next section.
In this task, you test the solution by adding the Web Part to the Home page of the website specified previously.
Open the website.
Figure 1: Web part insert screen
You now have your new Web Part listing projects as shown in Figure 2. Figure 2: Web part listing projects
In this task, create a Visual Web Part in Microsoft SharePoint Designer 2010.
In the code screen, scroll to the bottom of the page and place the pointer before the </ContentTemplate> end tag (after the </WebPartPages:WebPartZone> closing tag) as shown in below Figure 1. Figure 1. Place the pointer before the </ContentTemplate> tag
Figure 2. XsltListViewWebPart Web Part lists employees
In this task, modify the Web Part to add formatting and to limit the number of items displayed.
Figure 3. Apply a new style to the employee's list
In the code view of the designer, scroll up to WebPartPages:XsltListViewWebPart and type the ChromeType attribute, and then select TitleAndBorder as the chrome type as shown in Figure 4. Figure 4. Select the chrome type
In this task, you verify that the Web Part is displayed and that only five items are shown per page.
You liked the article?
Like : 0
Vote for difficulty
Current difficulty (Avg): Medium
1/3
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.
Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox