Making a web part part 3 the CS code

namespace WEBPARTNAMESPACE
{
public class KWEBPARTCLASS: Microsoft.SharePoint.WebPartPages.WebPart
{
//LIVE VERSION
private HtmlTable table;
protected override void CreateChildControls()
{

base.CreateChildControls();
this.table = new HtmlTable();
table.Border = 1;
table.BorderColor = “black”;

HtmlTableRow row = null;
HtmlTableCell cell = null;

using (SPSite CurrentSite_d = SPContext.Current.Site)
{

using (SPWeb thesite_d = CurrentSite_d.OpenWeb())
{
if ((thesite_d.Name.ToString() == “systems”) || (thesite_d.Name.ToString() == “hosts”) || (thesite_d.Name.ToString() == “devices”))
{
foreach (SPWeb subsite in thesite_d.Webs)
{
SPList thelist = subsite.Lists[“Details”];
foreach (SPListItem theitems in thelist.Items)
{

row = new HtmlTableRow();
table.Rows.Add(row);
cell = new HtmlTableCell();
cell.InnerHtml = “System Title”;
row.Cells.Add(cell);

foreach (SPField thefields in theitems.Fields)
{
if ((!thefields.ReadOnlyField) && (!thefields.Hidden) && (thefields.Type != SPFieldType.Attachments) && (thefields.Title.ToString() != “System Name”) && (thefields.Title.ToString() != “Title”))
{

cell = new HtmlTableCell();
if (thefields.InternalName != null)
{
cell.InnerHtml = thefields.InternalName.ToString();

}
else
{
cell.InnerHtml = ” “;
}
row.Cells.Add(cell);

}
}
if (thesite_d.Name.ToString() == “hosts”)
{
cell = new HtmlTableCell();
cell.InnerHtml = “Related Device”;
row.Cells.Add(cell);
}
if (thesite_d.Name.ToString() == “devices”)
{
cell = new HtmlTableCell();
cell.InnerHtml = “Assigned Host”;
row.Cells.Add(cell);
}

}
//break here as we only need the first details list.

subsite.Dispose();
break;
}
}
}
}

using (SPSite CurrentSite_d = SPContext.Current.Site)
{
using (SPWeb thesite_d = CurrentSite_d.OpenWeb())
{
if ((thesite_d.Name.ToString() == “systems”) || (thesite_d.Name.ToString() == “hosts”) || (thesite_d.Name.ToString() == “devices”))
{
foreach (SPWeb subsite in thesite_d.Webs)
{
row = new HtmlTableRow();
cell = new HtmlTableCell();
table.Rows.Add(row);
cell.InnerHtml = “<a href=’http://sharepoint/sites/kms/” + thesite_d.Name.ToString() + “/” + subsite.Name.ToString() + “‘>” + subsite.Name.ToString() + “</a>”;
row.Cells.Add(cell);

SPList thelist = subsite.Lists[“Details”];
foreach (SPListItem theitems in thelist.Items)
{
foreach (SPField thefields in theitems.Fields)
{
if ((!thefields.ReadOnlyField) && (!thefields.Hidden) && (thefields.Type != SPFieldType.Attachments) && (thefields.Title.ToString() != “System Name”) && (thefields.Title.ToString() != “Title”))
{

cell = new HtmlTableCell();

if (theitems[thefields.Title] == null)
{
cell.InnerHtml = “”;
}
else
{

cell.InnerHtml = theitems[thefields.Title].ToString();
}
row.Cells.Add(cell);

}

}
//cell = new HtmlTableCell();
//cell.InnerHtml = theitems.Title.ToString();
//row.Cells.Add(cell);
}

if (thesite_d.Name.ToString() == “hosts”)
{
SPList thenewlist = subsite.Lists[“Related Device”];
foreach (SPListItem thenewitems in thenewlist.Items)
{
cell = new HtmlTableCell();
if (thenewitems[“RelatedDevice”] == null)
{
cell.InnerHtml = “”;
}
else
{

cell.InnerHtml = thenewitems[“RelatedDevice”].ToString();
}
row.Cells.Add(cell);
}
}
if (thesite_d.Name.ToString() == “devices”)
{
SPList thenewlist = subsite.Lists[“Assigned Host”];
foreach (SPListItem thenewitems in thenewlist.Items)
{
cell = new HtmlTableCell();
if (thenewitems[“RelatedHost”] == null)
{
cell.InnerHtml = “”;
}
else
{

cell.InnerHtml = thenewitems[“RelatedHost”].ToString();
}
row.Cells.Add(cell);
}
}

subsite.Dispose();

}
}
}
}
base.Controls.Add(table);
}

protected override void Render(HtmlTextWriter writer)
{

try
{
SPSite CurrentSite = SPContext.Current.Site;
SPWeb thesite = CurrentSite.OpenWeb();
if ((thesite.Name.ToString() == “systems”) || (thesite.Name.ToString() == “hosts”) || (thesite.Name.ToString() == “devices”))
{
this.table.RenderControl(writer);
//writer.Write(“<a href=\”_layouts/KMSCreateNewEntity/KMSCreateNew.aspx\”>Create new ” + thesite.Name.ToString() + “</a>”);
}
else
{
writer.Write(“This webpart only works with the main pages for Systems, Hosts and Devices in the KMS system. Please delete this webpart now.”);
}
}
catch (Exception ex)
{
writer.Write(ex.Message + ” ” + ex.StackTrace);
}
}
}
}

Comments are closed.