Monday, March 8, 2010

LastestCoPAnnunser

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
//using System.Collections.Generic;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Web.UI.WebControls;

namespace CoPAnnouncer
{
[Guid("0c6833bc-aa4a-4b77-b2c6-e6d81e263cec")]
public class CoPAnnouncer : Microsoft.SharePoint.WebPartPages.WebPart
{
//private bool _error = false;
//private string _myProperty = null;
public DataTable tbl;
int countDocList = 0;
Label lblName = new Label();
HyperLink hlnk = new HyperLink();


//[Personalizable(PersonalizationScope.Shared)]
//[WebBrowsable(true)]
//[System.ComponentModel.Category("My Property Group")]
//[WebDisplayName("MyProperty")]
//[WebDescription("Meaningless Property")]
//public string MyProperty
//{
// get
// {
// if (_myProperty == null)
// {
// _myProperty = "CoP Announcer";
// }
// return _myProperty;
// }
// set { _myProperty = value; }
//}


public CoPAnnouncer()
{
this.ExportMode = WebPartExportMode.All;
}

///
/// Create all your controls here for rendering.
/// Try to avoid using the RenderWebPart() method.
///

///
protected override void Render(HtmlTextWriter writer)
{
///Creating the main table and adding step by step rows
///along with column and rendering the controls within the
///columns.
writer.Write(""); // width='100%'
writer.Write("");
writer.Write("
Total Number of CoPs : " + countDocList + "
Latest CoP " + lblName.Text + "
");
base.Render(writer);
}

protected override void CreateChildControls()
{
DataTable tbl = new DataTable();
DataRow row;
tbl.Columns.Add("UrlName", typeof(string));
tbl.Columns.Add("Url", typeof(string));
tbl.Columns.Add("LastDate", typeof(DateTime));
// SPWebApplication webApp = SPContext.Current.Site.WebApplication;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication webApp = SPContext.Current.Site.WebApplication;
// SPSiteCollection siteCollections = webApplication.Sites;
for (int i = 0; i < webApp.Sites.Count; i++)
{
try
{
SPSite site = webApp.Sites[i];
string[] sSite = site.ServerRelativeUrl.Split(new char[] { '/' });
if (sSite[1] == "cops")
{
countDocList++;
row = tbl.Rows.Add();
row["UrlName"] = site.RootWeb.Title;
row["Url"] = site.Url;
row["LastDate"] = site.RootWeb.Created;
//Convert.ToDateTime(webApp.Sites[i].WebApplication.LastContentModifiedDate);
}
}
catch
{
continue;
}
}
});

tbl.AcceptChanges();
DataView dv = new DataView();
dv = tbl.DefaultView;
dv.Table.Columns["LastDate"].DataType = Type.GetType("System.DateTime");
dv.RowFilter = "LastDate = Max(LastDate)";
DataTable newtbl = new DataTable();
newtbl = dv.ToTable();
lblName.Text = newtbl.Rows[0][0].ToString();
hlnk.Text = newtbl.Rows[0][1].ToString();
base.CreateChildControls();
}




}
}

No comments:

Post a Comment