Tuesday, January 3, 2012

Retrieving Top Five Records using SharePoint Linq to Dropdownlist

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Linq;
using Microsoft.SharePoint.Linq;
namespace LookUpWebPart.LookUpWebPart
{
[ToolboxItemAttribute(false)]
public class LookUpWebPart : WebPart
{
string strError = string.Empty;
DropDownList ddlConcepts;
protected override void Render(HtmlTextWriter writer)
{
writer.Write(strError);
try
{
writer.Write("<Table width='100%'>");
writer.Write("<Tr>");
writer.Write("<Td>");
ddlConcepts.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("</Table>");
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
protected override void CreateChildControls()
{
try
{
ddlConcepts = new DropDownList();
this.Controls.Add(ddlConcepts);
LinqSampleDataContext context = new LinqSampleDataContext(SPContext.Current.Web.Url);
string strTitle = string.Empty;
string strID = string.Empty;
var query = from cs in context.SharePoint2010Concepts orderby cs.Id descending
select new
{
strTitle=cs.Title,
strID=cs.Id,
};
if (query.Count() > 0)
{
ddlConcepts.DataValueField = "strID";
ddlConcepts.DataTextField = "strTitle";
ddlConcepts.DataSource = query.Take(5);
ddlConcepts.DataBind();
}
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
}
}

No comments:

Post a Comment