MOSS站点下所有用户文档上传日志
一个简单的WebPart,功能大致为获取当前站点下所有用户,解析当前登录用户的权限。如权限足够,查询当前用户所管理人员的所有文档上传记录,如果权限不够,临时提升权限,获取自己上传文档的记录。
Code
/**//***************************************
* 功能:查询并统计用户一定时间内上传个数
* 作者:梁亮
* 时间:2009.04.23
* 修改:
* *************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
namespace Symbio.SharePoint
{
/**//// <summary>
/// 统计用户时间内上传WebPart
/// </summary>
public partial class UserUploadLog : System.Web.UI.UserControl
{
public bool truePermission=false;
/**//// <summary>
/// 页面加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (SPSite site = SPContext.Current.Site)
{
SPWeb web = SPContext.Current.Web;
//是否具有查看网站列表项的权限(必须为管理员)
bool ishavepermission = web.DoesUserHavePermissions(SPBasePermissions.FullMask);
truePermission = ishavepermission;
}
initPage();
}
}
/**//// <summary>
/// 页面初始化
/// </summary>
private void initPage()
{
//小时列表
for (int HourIndex = 0; HourIndex < 24; HourIndex++)
{
if (HourIndex < 10)
{
DDLFirstHours.Items.Add(new ListItem("0"+HourIndex.ToString()));
DDLLastHours.Items.Add(new ListItem("0"+HourIndex.ToString()));
}
else
{
DDLFirstHours.Items.Add(new ListItem(HourIndex.ToString()));
DDLLastHours.Items.Add(new ListItem(HourIndex.ToString()));
}
}
//分列表
for (int MinIndex = 0; MinIndex < 60; MinIndex++)
{
if (MinIndex < 10)
{
DDLFirstMinutes.Items.Add(new ListItem("0"+MinIndex.ToString()));
DDLLastMinutes.Items.Add(new ListItem("0"+MinIndex.ToString()));
}
else
{
DDLFirstMinutes.Items.Add(new ListItem(MinIndex.ToString()));
DDLLastMinutes.Items.Add(new ListItem(MinIndex.ToString()));
}
}
//用户列表
getSiteUser();
}
/**//// <summary>
/// 以管理员身份查询
/// </summary>
private void getSiteUser()
{
// 以管理员身份运行以下代码
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite siteColl = SPContext.Current.Site;
SPWeb site = SPContext.Current.Web;
using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
{
using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
{
//当前网站集合
SPWebCollection allWebs = ElevatedsiteColl.AllWebs;
DDLUserName.Items.Add(new ListItem("所有用户"));
//当前网站
foreach (SPWeb subWeb in allWebs)
{
//当前网站用户集合
SPUserCollection allUsers = subWeb.AllUsers;
//当前用户
foreach (SPUser user in allUsers)
{
//不是管理员
if (!truePermission)
{
if (!DDLUserName.Items.Contains(new ListItem(this.Page.User.Identity.Name)))
{
DDLUserName.Items.Add(new ListItem(this.Page.User.Identity.Name));
}
DDLUserName.SelectedIndex = 1;
DDLUserName.Enabled = false;
}
else
{
if (!DDLUserName.Items.Contains(new ListItem(user.LoginName)))
{
DDLUserName.Items.Add(new ListItem(user.LoginName));
}
}
}
}
}
}
});
}
/**//// <summary>
/// 起始时间选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButFirstDate_Click(object sender, EventArgs e)
{
CalendarFirstDate.Visible = true;
}
/**//// <summary>
/// 截止时间选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButLastDate_Click(object sender, EventArgs e)
{
CalendarLastDate.Visible = true;
}
/**//// <summary>
/// 起始时间事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CalendarFirstDate_SelectionChanged(object sender, EventArgs e)
{
TextBoxFirstDate.Text = CalendarFirstDate.SelectedDate.ToShortDateString();
CalendarFirstDate.SelectedDate = default(DateTime);
CalendarFirstDate.Visible = false;
}
/**//// <summary>
/// 截止时间选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CalendarLastDate_SelectionChanged(object sender, EventArgs e)
{
TextBoxLastDate.Text = CalendarLastDate.SelectedDate.ToShortDateString();
CalendarLastDate.SelectedDate = default(DateTime);
CalendarLastDate.Visible = false;
}
/**//// <summary>
/// 根据条件查询所有数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButSearch_Click(object sender, EventArgs e)
{
string dateStart = DateTime.Now.ToShortDateString();
string dateEnd = DateTime.Now.ToShortDateString(); ;
string strUserName = DDLUserName.Items[DDLUserName.SelectedIndex].Text;
if (TextBoxFirstDate.Text.Trim().Length == 0)
{
dateStart = dateStart + " " + DDLFirstHours.Items[DDLFirstHours.SelectedIndex].Text + ":" + DDLFirstMinutes.Items[DDLFirstMinutes.SelectedIndex].Text;
}
else
{
dateStart = TextBoxFirstDate.Text + " " + DDLFirstHours.Items[DDLFirstHours.SelectedIndex].Text + ":" + DDLFirstMinutes.Items[DDLFirstMinutes.SelectedIndex].Text;
}
if (TextBoxLastDate.Text.Trim().Length == 0)
{
dateEnd = dateEnd + " " + DDLLastHours.Items[DDLLastHours.SelectedIndex].Text + ":" + DDLLastMinutes.Items[DDLLastMinutes.SelectedIndex].Text;
}
else
{
dateEnd = TextBoxLastDate.Text + " " + DDLLastHours.Items[DDLLastHours.SelectedIndex].Text + ":" + DDLLastMinutes.Items[DDLLastMinutes.SelectedIndex].Text;
}
initDataGrid(dateStart, dateEnd, strUserName);
}
private void initDataGrid(string dateStart,string dateEnd,string UserName)
{
List<SPFile> docItemList = GetSiteDocItems(dateStart,dateEnd,UserName);
//设置datagrid
dgSite.AutoGenerateColumns = false;
dgSite.AllowPaging = true;
dgSite.PageSize = 10;
BoundColumn bc = new BoundColumn();
bc.HeaderText = "文件名";
bc.DataField = "Name";
dgSite.Columns.Add(bc);
bc = new BoundColumn();
bc.HeaderText = "创建人";
bc.DataField = "Author";
dgSite.Columns.Add(bc);
bc = new BoundColumn();
bc.HeaderText = "创建时间";
bc.DataField = "TimeCreated";
dgSite.Columns.Add(bc);
bc = new BoundColumn();
bc.HeaderText = "文件路径";
bc.DataField = "ServerRelativeUrl";
dgSite.Columns.Add(bc);
dgSite.DataSource = docItemList;
dgSite.DataBind();
}
private static List<SPFile> GetSiteDocItems(string dateStart, string dateEnd, string UserName)
{
List<SPFile> docItemList = new List<SPFile>();
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = SPContext.Current.Site;
SPWeb spweb = SPContext.Current.Web;
spweb.AllowUnsafeUpdates = true;
using (SPSite ElevatedsiteColl = new SPSite(site.ID))
{
using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(spweb.ID))
{
SPListCollection listcollection = spweb.Lists;
foreach (SPList list in listcollection)
{
if (list.BaseType == SPBaseType.DocumentLibrary && list.BaseTemplate != SPListTemplateType.ListTemplateCatalog
&& list.BaseTemplate == SPListTemplateType.DocumentLibrary)
{
SPListItemCollection itemcollection = list.Items;
foreach (SPListItem item in itemcollection)
{
//if (item.ParentList.Title == "文档")
if (item.ParentList.BaseType == SPBaseType.DocumentLibrary)
{
if ((Convert.ToDateTime(dateStart) <= item.File.TimeCreated) && (item.File.TimeCreated <= Convert.ToDateTime(dateEnd)))
{
if (item.File.Author != null)
{
if (UserName == "所有用户")
{
docItemList.Add(item.File);
}
else
{
SPUser su = item.File.Author;
if ((su.LoginName.ToLower() == UserName.ToLower()))
{
docItemList.Add(item.File);
}
}
}
}
}
}
}
}
}
}
});
return docItemList;
}
/**//// <summary>
/// 页面数据绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void dgSite_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink linkbut = new HyperLink();
linkbut.NavigateUrl = e.Item.Cells[3].Text;
linkbut.Text = e.Item.Cells[3].Text;
e.Item.Cells[3].Controls.Add(linkbut);
//添加自定义属性,当鼠标移过来时设置该行的背景色为"#ffffff",并保存原背景色
e.Item.Attributes.Add("onmouseover", "StyleColor=this.style.backgroundColor;this.style.backgroundColor='#ffffff'");
//添加自定义属性,当鼠标移走时还原该行的背景色
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=StyleColor");
}
}
/**//// <summary>
/// 页面功能
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void dgSite_ItemCommand(object source, DataGridCommandEventArgs e)
{
}
/**//// <summary>
/// 分页
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void dgSite_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
dgSite.CurrentPageIndex = e.NewPageIndex;
dgSite.DataBind();
}
}
/**//// <summary>
/// 文件类
/// </summary>
public class FileClass
{
public string FileName;
private string _FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
}
}
public string FileCreatTime;
private string _FileCreatTime
{
get
{
return _FileCreatTime;
}
set
{
_FileCreatTime = value;
}
}
public string FilePath;
private string _FilePath
{
set
{
_FilePath = value;
}
get
{
return _FilePath;
}
}
}
}
/**//***************************************
* 功能:查询并统计用户一定时间内上传个数
* 作者:梁亮
* 时间:2009.04.23
* 修改:
* *************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
namespace Symbio.SharePoint
{
/**//// <summary>
/// 统计用户时间内上传WebPart
/// </summary>
public partial class UserUploadLog : System.Web.UI.UserControl
{
public bool truePermission=false;
/**//// <summary>
/// 页面加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (SPSite site = SPContext.Current.Site)
{
SPWeb web = SPContext.Current.Web;
//是否具有查看网站列表项的权限(必须为管理员)
bool ishavepermission = web.DoesUserHavePermissions(SPBasePermissions.FullMask);
truePermission = ishavepermission;
}
initPage();
}
}
/**//// <summary>
/// 页面初始化
/// </summary>
private void initPage()
{
//小时列表
for (int HourIndex = 0; HourIndex < 24; HourIndex++)
{
if (HourIndex < 10)
{
DDLFirstHours.Items.Add(new ListItem("0"+HourIndex.ToString()));
DDLLastHours.Items.Add(new ListItem("0"+HourIndex.ToString()));
}
else
{
DDLFirstHours.Items.Add(new ListItem(HourIndex.ToString()));
DDLLastHours.Items.Add(new ListItem(HourIndex.ToString()));
}
}
//分列表
for (int MinIndex = 0; MinIndex < 60; MinIndex++)
{
if (MinIndex < 10)
{
DDLFirstMinutes.Items.Add(new ListItem("0"+MinIndex.ToString()));
DDLLastMinutes.Items.Add(new ListItem("0"+MinIndex.ToString()));
}
else
{
DDLFirstMinutes.Items.Add(new ListItem(MinIndex.ToString()));
DDLLastMinutes.Items.Add(new ListItem(MinIndex.ToString()));
}
}
//用户列表
getSiteUser();
}
/**//// <summary>
/// 以管理员身份查询
/// </summary>
private void getSiteUser()
{
// 以管理员身份运行以下代码
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite siteColl = SPContext.Current.Site;
SPWeb site = SPContext.Current.Web;
using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
{
using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
{
//当前网站集合
SPWebCollection allWebs = ElevatedsiteColl.AllWebs;
DDLUserName.Items.Add(new ListItem("所有用户"));
//当前网站
foreach (SPWeb subWeb in allWebs)
{
//当前网站用户集合
SPUserCollection allUsers = subWeb.AllUsers;
//当前用户
foreach (SPUser user in allUsers)
{
//不是管理员
if (!truePermission)
{
if (!DDLUserName.Items.Contains(new ListItem(this.Page.User.Identity.Name)))
{
DDLUserName.Items.Add(new ListItem(this.Page.User.Identity.Name));
}
DDLUserName.SelectedIndex = 1;
DDLUserName.Enabled = false;
}
else
{
if (!DDLUserName.Items.Contains(new ListItem(user.LoginName)))
{
DDLUserName.Items.Add(new ListItem(user.LoginName));
}
}
}
}
}
}
});
}
/**//// <summary>
/// 起始时间选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButFirstDate_Click(object sender, EventArgs e)
{
CalendarFirstDate.Visible = true;
}
/**//// <summary>
/// 截止时间选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButLastDate_Click(object sender, EventArgs e)
{
CalendarLastDate.Visible = true;
}
/**//// <summary>
/// 起始时间事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CalendarFirstDate_SelectionChanged(object sender, EventArgs e)
{
TextBoxFirstDate.Text = CalendarFirstDate.SelectedDate.ToShortDateString();
CalendarFirstDate.SelectedDate = default(DateTime);
CalendarFirstDate.Visible = false;
}
/**//// <summary>
/// 截止时间选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CalendarLastDate_SelectionChanged(object sender, EventArgs e)
{
TextBoxLastDate.Text = CalendarLastDate.SelectedDate.ToShortDateString();
CalendarLastDate.SelectedDate = default(DateTime);
CalendarLastDate.Visible = false;
}
/**//// <summary>
/// 根据条件查询所有数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButSearch_Click(object sender, EventArgs e)
{
string dateStart = DateTime.Now.ToShortDateString();
string dateEnd = DateTime.Now.ToShortDateString(); ;
string strUserName = DDLUserName.Items[DDLUserName.SelectedIndex].Text;
if (TextBoxFirstDate.Text.Trim().Length == 0)
{
dateStart = dateStart + " " + DDLFirstHours.Items[DDLFirstHours.SelectedIndex].Text + ":" + DDLFirstMinutes.Items[DDLFirstMinutes.SelectedIndex].Text;
}
else
{
dateStart = TextBoxFirstDate.Text + " " + DDLFirstHours.Items[DDLFirstHours.SelectedIndex].Text + ":" + DDLFirstMinutes.Items[DDLFirstMinutes.SelectedIndex].Text;
}
if (TextBoxLastDate.Text.Trim().Length == 0)
{
dateEnd = dateEnd + " " + DDLLastHours.Items[DDLLastHours.SelectedIndex].Text + ":" + DDLLastMinutes.Items[DDLLastMinutes.SelectedIndex].Text;
}
else
{
dateEnd = TextBoxLastDate.Text + " " + DDLLastHours.Items[DDLLastHours.SelectedIndex].Text + ":" + DDLLastMinutes.Items[DDLLastMinutes.SelectedIndex].Text;
}
initDataGrid(dateStart, dateEnd, strUserName);
}
private void initDataGrid(string dateStart,string dateEnd,string UserName)
{
List<SPFile> docItemList = GetSiteDocItems(dateStart,dateEnd,UserName);
//设置datagrid
dgSite.AutoGenerateColumns = false;
dgSite.AllowPaging = true;
dgSite.PageSize = 10;
BoundColumn bc = new BoundColumn();
bc.HeaderText = "文件名";
bc.DataField = "Name";
dgSite.Columns.Add(bc);
bc = new BoundColumn();
bc.HeaderText = "创建人";
bc.DataField = "Author";
dgSite.Columns.Add(bc);
bc = new BoundColumn();
bc.HeaderText = "创建时间";
bc.DataField = "TimeCreated";
dgSite.Columns.Add(bc);
bc = new BoundColumn();
bc.HeaderText = "文件路径";
bc.DataField = "ServerRelativeUrl";
dgSite.Columns.Add(bc);
dgSite.DataSource = docItemList;
dgSite.DataBind();
}
private static List<SPFile> GetSiteDocItems(string dateStart, string dateEnd, string UserName)
{
List<SPFile> docItemList = new List<SPFile>();
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = SPContext.Current.Site;
SPWeb spweb = SPContext.Current.Web;
spweb.AllowUnsafeUpdates = true;
using (SPSite ElevatedsiteColl = new SPSite(site.ID))
{
using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(spweb.ID))
{
SPListCollection listcollection = spweb.Lists;
foreach (SPList list in listcollection)
{
if (list.BaseType == SPBaseType.DocumentLibrary && list.BaseTemplate != SPListTemplateType.ListTemplateCatalog
&& list.BaseTemplate == SPListTemplateType.DocumentLibrary)
{
SPListItemCollection itemcollection = list.Items;
foreach (SPListItem item in itemcollection)
{
//if (item.ParentList.Title == "文档")
if (item.ParentList.BaseType == SPBaseType.DocumentLibrary)
{
if ((Convert.ToDateTime(dateStart) <= item.File.TimeCreated) && (item.File.TimeCreated <= Convert.ToDateTime(dateEnd)))
{
if (item.File.Author != null)
{
if (UserName == "所有用户")
{
docItemList.Add(item.File);
}
else
{
SPUser su = item.File.Author;
if ((su.LoginName.ToLower() == UserName.ToLower()))
{
docItemList.Add(item.File);
}
}
}
}
}
}
}
}
}
}
});
return docItemList;
}
/**//// <summary>
/// 页面数据绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void dgSite_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink linkbut = new HyperLink();
linkbut.NavigateUrl = e.Item.Cells[3].Text;
linkbut.Text = e.Item.Cells[3].Text;
e.Item.Cells[3].Controls.Add(linkbut);
//添加自定义属性,当鼠标移过来时设置该行的背景色为"#ffffff",并保存原背景色
e.Item.Attributes.Add("onmouseover", "StyleColor=this.style.backgroundColor;this.style.backgroundColor='#ffffff'");
//添加自定义属性,当鼠标移走时还原该行的背景色
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=StyleColor");
}
}
/**//// <summary>
/// 页面功能
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void dgSite_ItemCommand(object source, DataGridCommandEventArgs e)
{
}
/**//// <summary>
/// 分页
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void dgSite_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
dgSite.CurrentPageIndex = e.NewPageIndex;
dgSite.DataBind();
}
}
/**//// <summary>
/// 文件类
/// </summary>
public class FileClass
{
public string FileName;
private string _FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
}
}
public string FileCreatTime;
private string _FileCreatTime
{
get
{
return _FileCreatTime;
}
set
{
_FileCreatTime = value;
}
}
public string FilePath;
private string _FilePath
{
set
{
_FilePath = value;
}
get
{
return _FilePath;
}
}
}
}