Tuesday, June 24, 2008

SPList fields - parsing source-code

Here is the source-code to obtain an html table of fields for any list:

(Please note that this example takes place inside the Render(HtmlTextWriter writer) method:

using (SPSite site = new SPSite(SPContext.Current.Site.Url))

{

using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl))

{

SPList list = web.Lists["YourList"];

SPListItemCollection iCol = list.Items;


writer.Write("<table border=\"1\">");

writer.Write("<tr><td><strong>Field name</strong></td><td><strong>Sample value (if any)</strong></td></tr>");

for (int i = 0; i < iCol[0].Fields.Count; i++)

{

writer.Write("<tr><td><strong>");

writer.Write(iCol[0].Fields[i].Title);

writer.Write("</strong><br/>" + iCol[0].Fields[i].Description);

writer.Write("</td><td>");

writer.Write(iCol[0].GetFormattedValue(iCol[0].Fields[i].Title));

writer.Write("</td></tr>");

}

writer.Write("</table>");

}

}


No comments: