17.1.12

SharePoint and Linq : speed code in visual web part

proxy generation


//in solution folder

set path=%path%;c:\program files\common files\microsoft shared\web server extensions\14\bin

spmetal.exe /web:{url sharepoint site} /namespace:{solution namespace} /code:{codeClassName}.cs


.aspx


<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>
<SharePoint:SPGridView id="spGridView" runat="server" AutoGenerateColumns="false">
  <HeaderStyle HorizontalAlign="Left" ForeColor="Navy" Font-Bold="true" />
  <Columns>
    <SharePoint:SPBoundField  DataField="Title" HeaderText="Title"></SharePoint:SPBoundField>
    <SharePoint:SPBoundField DataField="JobTitle" HeaderText="JobTitle"></SharePoint:SPBoundField>
    <SharePoint:SPBoundField DataField="ProjectTitle" HeaderText="ProjectTitle"></SharePoint:SPBoundField>
    <SharePoint:SPBoundField DataField="DueDate" HeaderText="DueDate"></SharePoint:SPBoundField>
  </Columns>
</SharePoint:SPGridView>


.cs


using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;
using System.Linq;

{codeClassName} dc = new {codeClassName}(SPContext.Current.Web.Url); 
EntityList<EmployeesItem> Employees = dc.GetList<EmployeesItem>("Employees"); 
var empQuery = from emp in Employees
               where emp.Project.DueDate < DateTime.Now.AddMonths(6)
               select new
               {
                   emp.Title,
                   emp.JobTitle,
                   ProjectTitle = emp.Project.Title,
                   DueDate = emp.Project.DueDate.Value.ToShortDateString()
               }; 
spGridView.DataSource = empQuery;
spGridView.DataBind();

No comments:

Post a Comment