Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Sunday, March 03, 2013

Using SharePoint Objects within a Server Side Linq




Ascx
<%@ 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="LinqWithOutLoopUserControl.ascx.cs" Inherits="LinqWithOutLoop.LinqWithOutLoop.LinqWithOutLoopUserControl" %>
<asp:GridView ID="dgvLinqWithOutLoop" runat="server"
    AutoGenerateColumns="False" Width="100%" CellPadding="4"
    EnableModelValidation="True" ForeColor="#333333" GridLines="Both">
    <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Title" DataField="Title" />
<asp:BoundField HeaderText="Department" DataField="Department" />
<asp:BoundField HeaderText="Age" DataField="Age" />
<asp:BoundField HeaderText="Country" DataField="Country" />
<asp:BoundField HeaderText="Joining Date" DataField="Joiningdate" DataFormatString="{0:G}" />
<asp:BoundField HeaderText="Favourite Color" DataField="FavouriteColor" />
<asp:BoundField HeaderText="Author Name" DataField="AuthorName" />
<asp:BoundField HeaderText="Change Age" DataField="ChangeAge" />
</Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
Ascx.Cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
using System.Linq;

namespace LinqWithOutLoop.LinqWithOutLoop
{
    public partial class LinqWithOutLoopUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                getData();
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }

        public void getData()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
               string Title, Department, Age, Country, Joiningdate, FavouriteColor, AuthorName, ChangeAge, ID = string.Empty;
               var strQuery = (from SPListItem item in SPContext.Current.Web.Lists["Employees Data"].Items
                                orderby item.ID descending
                                select new
                                {
                                  Title = item.Title.ToString(),
                                  Department = item["Department"].ToString(),
                                  Age = item["Age"].ToString(),
                                  Country=new SPFieldLookupValue(item["Country"].ToString()).LookupValue,
                                  Joiningdate = item["Joiningdate"].ToString(),
                                  FavouriteColor = item["FavouriteColor"].ToString(),
                                  AuthorName = (item["AuthorName"] == null) ? "--" : new SPFieldLookupValue(item["AuthorName"].ToString()).LookupValue,
                                  ChangeAge = (item["ChangeAge"] == null) ? "0" : item["ChangeAge"].ToString(), ID=item.ID.ToString()
                                });
                dgvLinqWithOutLoop.DataSource = strQuery;
                dgvLinqWithOutLoop.DataBind();
            });
        }
    }
}

No comments:

Post a Comment