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>”);

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: