Getting SharePoint BlogPost item via LinQ without SPMetal

My Criteria:
a. Getting SharePoint BlogPost items without generating SPMetal
b. Have a custom column which inherit from SPFieldURL

//Remember to add the following using in your CS file:
/*

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.Services;
using System.Linq;
*/

Here goes the code:
On Page Load Event:

    SPList list = SPContext.Current.Web.Lists["Posts"];

//Row: to get the first 20 records:pagination
 var items = list.Items.OfType<SPListItem>().Take(Rows).Select(
    i => new {
       ID = i.ID,
       Picture1 =(new SPFieldUrlValue(i["Picture1"].ToString())).Url,
       Title = i.Title});    
 
       //Bind to a DataList
   DtBlogPost.DataSource = items.ToList();
   DtBlogPost.DataBind();


There you go, this is very simple code.

                           

Comments

Popular posts from this blog

SharePoint 2013: Distributed Cache Services

AWS: On Demand Capacity Reservation (ODCR) and preventing Insufficient Capacity Error (ICE)

SharePoint 2013: Execution Policy for PowerShell