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.

Monday, January 28, 2013

Getting Column Names and Data Dynamically in SharePoint List



Note:-
  1.  The Column Type of Single Lines of Text is Text.
  2. The Column Type of Lookup Column is LookUp.
  3. The Column Type of People Picker Column is User.
  4. The Column Type of Choice Field is Choice.
  5. The Column Type of Date Column is DateTime.
  6. The Column Type of Number Column is Number.
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 Valign='Top'>";
                            if (arrColumnType[k].ToString() == "Lookup")
                            {
                                SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(item[arrData[k].ToString()].ToString());
                                string strMultiLookUpData = "";
                                foreach (SPFieldLookupValue value in values)
                                {
                                    strMultiLookUpData += "<Div>" +value.LookupValue + "</Div>";
                                }
                                strInnerData += strMultiLookUpData;
                            }
                            if (arrColumnType[k].ToString() == "User")
                            {
                                string strLookUpColumn = item[arrData[k].ToString()].ToString();
                                 SPFieldLookupValue strLookUp = new SPFieldLookupValue(strLookUpColumn);
                                 strInnerData += strLookUp.LookupValue;
                              
                            }
                            if (arrColumnType[k].ToString() == "Number")
                            {
                                string strNumberFromDataBase = item[arrData[k].ToString()].ToString();
                                int i = int.Parse(strNumberFromDataBase);
                                strInnerData += i.ToString();
                            }
                            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>";

                    string strColumnType = string.Empty;
                    for (int l = 0; l < arrColumnType.Count; l++)
                    {
                        strColumnType+="The Column Type Of " + arrData[l].ToString()+" is "+ arrColumnType[l].ToString()+"<Br />";
                    }
                    pnlControlTypes.Controls.Add(new LiteralControl(strColumnType));
                    pnlDynamicData.Controls.Add(new LiteralControl(strBindData));
                }
            }
            catch (Exception Ex)
            {

                Response.Write(Ex.ToString());
            }
        }
    }
}

No comments:

Post a Comment