Getting Column Names and Data Dynamically in SharePoint List
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="SpFieldsExUserControl.ascx.cs" Inherits="SpFieldsEx.SpFieldsEx.SpFieldsExUserControl"
%>
<style type="text/css">
.HeaderClass
{
background-color:Green;
font-weight:bold;
color:White;
}
.InnerDataClass
{
background-color:Orange;
font-weight:bold;
color:White;
}
.BlankTRClass
{
background-color:Red;
height:1px;
}
</style>
<asp:Panel ID="pnlDynamicData" runat="server"></asp:Panel>
|
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.Collections;
namespace SpFieldsEx.SpFieldsEx
{
public partial class SpFieldsExUserControl : UserControl
{
protected
void Page_Load(object
sender, EventArgs e)
{
try
{
string
strBindData = string.Empty;
string
strHeaderData = string.Empty;
string
strInnerData = string.Empty;
ArrayList
arrData = new ArrayList();
ArrayList
arrColumnType = new ArrayList();
SPWeb
currentWeb = SPContext.Current.Web;
SPList
lst = currentWeb.Lists["Employees Data"];
SPQuery
sQuery = new SPQuery();
sQuery.Query = "<OrderBy><FieldRef Name='ID'
/></OrderBy>";
SPListItemCollection
mycoll = lst.GetItems(sQuery);
int
count = 0;
strHeaderData += "<Tr class='HeaderClass'>";
if
(mycoll.Count > 0)
{
for
(int i = 0; i < lst.Fields.Count - 1; i++)
{
if (!lst.Fields[i].Hidden &&
!lst.Fields[i].ReadOnlyField && lst.Fields[i].Type != SPFieldType.Attachments &&
lst.Fields[i].Title != "Content Type")
{
strHeaderData += "<Td>";
strHeaderData +=
lst.Fields[i].Title;
strHeaderData += "</Td>";
arrData.Add(lst.Fields[i].StaticName);
arrColumnType.Add(lst.Fields[i].Type.ToString());
count++;
}
}
strHeaderData += "</Tr>";
strBindData += "<Table width='100%' cellpadding='1'
cellspacing='0'>";
strBindData += "<Tr class='BlankTRClass'>";
strBindData += "<Td Colspan='"+count+"'>";
strBindData += "</Td>";
strBindData += "</Tr>";
strBindData +=
strHeaderData;
foreach
(SPListItem item in
mycoll)
{
strInnerData += "<Tr class='InnerDataClass'>";
for (int k = 0; k
< arrData.Count; k++)
{
strInnerData += "<Td>";
if
(arrColumnType[k].ToString() == "Lookup")
{
string strLookUpColumn =
item[arrData[k].ToString()].ToString();
SPFieldLookupValue strLookUp = new SPFieldLookupValue(strLookUpColumn);
strInnerData
+= strLookUp.LookupValue;
}
if (arrColumnType[k].ToString() == "DateTime")
{
DateTime strDtJoingDate=Convert.ToDateTime(item[arrData[k].ToString()]);
strInnerData
+= strDtJoingDate.ToShortDateString();
}
if (arrColumnType[k].ToString() == "Text")
{
strInnerData
+= item[arrData[k].ToString()].ToString();
}
if (arrColumnType[k].ToString() == "Choice")
{
strInnerData
+= item[arrData[k].ToString()].ToString();
}
strInnerData += "</Td>";
}
strInnerData += "</Tr>";
strInnerData += "<Tr class='BlankTRClass'>";
strInnerData += "<Td Colspan='" + count + "'>";
strInnerData += "</Td>";
strInnerData += "</Tr>";
}
strBindData +=
strInnerData;
strBindData += "</Table>";
for
(int l = 0; l < arrColumnType.Count; l++)
{
Response.Write("The Column Type Of " +
arrData[l].ToString()+" is "+
arrColumnType[l].ToString()+"<Br
/>");
}
pnlDynamicData.Controls.Add(new LiteralControl(strBindData));
}
}
catch
(Exception Ex)
{
Response.Write(Ex.ToString());
}
}
}
}
|
No comments:
Post a Comment