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.

Wednesday, February 06, 2013

Create a New Image Next to GridView Column if the SharePoint List Item Created is the new one

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="NewImageUserControl.ascx.cs" Inherits="NewImage.NewImage.NewImageUserControl" %>
<asp:GridView ID="dgvNewImage" runat="server" AutoGenerateColumns="False"
    Width="100%" BackColor="White" BorderColor="#CC9966" BorderStyle="None"
    BorderWidth="1px" CellPadding="4" EnableModelValidation="True"
    GridLines="Both" onrowdatabound="dgvNewImage_RowDataBound" DataKeyNames="ID">
<Columns>
<asp:BoundField HeaderText="Employee Name" DataField="Title" />
<asp:BoundField HeaderText="Employee Department" DataField="Department" />
<asp:BoundField HeaderText="Employee Age" DataField="Age" />
</Columns>
    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
    <RowStyle BackColor="White" ForeColor="#330099" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
</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;

namespace NewImage.NewImage
{
    public partial class NewImageUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            getData();
        }
        public void getData()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["Employees"];
                    SPListItemCollection myColl = lst.Items;
                    if (myColl.Count > 0)
                    {
                        dgvNewImage.DataSource = myColl.GetDataTable();
                        dgvNewImage.DataBind();
                    }
                });
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }
        protected void dgvNewImage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["Employees"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<Where><Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">" + dgvNewImage.DataKeys[e.Row.RowIndex].Value.ToString() + "</Value></Eq></Where>";
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        SPListItem item = myColl[0];
                        DateTime dtCreatedDate = Convert.ToDateTime(item["Created"].ToString());
                        DateTime dtConvertedDateTime = Convert.ToDateTime(dtCreatedDate.ToShortDateString());
                        DateTime dtToday = Convert.ToDateTime(DateTime.Now);
                        DateTime dtConvertedToday = Convert.ToDateTime(dtToday.ToShortDateString());
                        if (dtConvertedDateTime==dtConvertedToday)
                        {
                            e.Row.Cells[0].Text = e.Row.Cells[0].Text + "<Img src='/_layouts/1033/Images/new.gif' />";
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }
    }
}

No comments:

Post a Comment