MOSS站点下所有用户文档上传日志

 一个简单的WebPart,功能大致为获取当前站点下所有用户,解析当前登录用户的权限。如权限足够,查询当前用户所管理人员的所有文档上传记录,如果权限不够,临时提升权限,获取自己上传文档的记录。

ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//***************************************
 * 功能:查询并统计用户一定时间内上传个数
 * 作者:梁亮
 * 时间: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
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 统计用户时间内上传WebPart
    
/// </summary>

    public partial class UserUploadLog : System.Web.UI.UserControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

        
public bool truePermission=false;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 页面加载
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
using (SPSite site = SPContext.Current.Site)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    SPWeb web 
= SPContext.Current.Web;
                    
//是否具有查看网站列表项的权限(必须为管理员)
                    bool ishavepermission = web.DoesUserHavePermissions(SPBasePermissions.FullMask);
                    truePermission 
= ishavepermission;
                }

                initPage();
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 页面初始化
        
/// </summary>

        private void initPage()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//小时列表
            for (int HourIndex = 0; HourIndex < 24; HourIndex++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (HourIndex < 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    DDLFirstHours.Items.Add(
new ListItem("0"+HourIndex.ToString()));
                    DDLLastHours.Items.Add(
new ListItem("0"+HourIndex.ToString()));
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    DDLFirstHours.Items.Add(
new ListItem(HourIndex.ToString()));
                    DDLLastHours.Items.Add(
new ListItem(HourIndex.ToString()));
                }

            }

            
//分列表
            for (int MinIndex = 0; MinIndex < 60; MinIndex++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (MinIndex < 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    DDLFirstMinutes.Items.Add(
new ListItem("0"+MinIndex.ToString()));
                    DDLLastMinutes.Items.Add(
new ListItem("0"+MinIndex.ToString()));
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    DDLFirstMinutes.Items.Add(
new ListItem(MinIndex.ToString()));
                    DDLLastMinutes.Items.Add(
new ListItem(MinIndex.ToString()));
                }

            }

            
//用户列表
            getSiteUser();
            
            
            
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 以管理员身份查询
        
/// </summary>

        private void getSiteUser()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
// 以管理员身份运行以下代码
            Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                SPSite siteColl 
= SPContext.Current.Site;
                SPWeb site 
= SPContext.Current.Web;
                
using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
//当前网站集合
                        SPWebCollection allWebs = ElevatedsiteColl.AllWebs;
                        DDLUserName.Items.Add(
new ListItem("所有用户"));
                        
//当前网站
                        foreach (SPWeb subWeb in allWebs)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
//当前网站用户集合
                            SPUserCollection allUsers = subWeb.AllUsers;

                            
//当前用户
                            foreach (SPUser user in allUsers)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                
//不是管理员
                                if (!truePermission)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    
if (!DDLUserName.Items.Contains(new ListItem(this.Page.User.Identity.Name)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
{
                                        DDLUserName.Items.Add(
new ListItem(this.Page.User.Identity.Name));
                                    }

                                    DDLUserName.SelectedIndex 
= 1;
                                    DDLUserName.Enabled 
= false;
                                }

                                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    
if (!DDLUserName.Items.Contains(new ListItem(user.LoginName)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
{
                                        DDLUserName.Items.Add(
new ListItem(user.LoginName));
                                    }

                                }

                            }

                        }

                    }

                }

            }
);
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 起始时间选择事件
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        protected void ButFirstDate_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            CalendarFirstDate.Visible 
= true;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 截止时间选择事件
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        protected void ButLastDate_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            CalendarLastDate.Visible 
= true;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 起始时间事件
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        protected void CalendarFirstDate_SelectionChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            TextBoxFirstDate.Text 
= CalendarFirstDate.SelectedDate.ToShortDateString();
            CalendarFirstDate.SelectedDate 
= default(DateTime);
            CalendarFirstDate.Visible 
= false;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 截止时间选择事件
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        protected void CalendarLastDate_SelectionChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            TextBoxLastDate.Text 
= CalendarLastDate.SelectedDate.ToShortDateString();
            CalendarLastDate.SelectedDate 
= default(DateTime);
            CalendarLastDate.Visible 
= false;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 根据条件查询所有数据
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        protected void ButSearch_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string dateStart = DateTime.Now.ToShortDateString();
            
string dateEnd = DateTime.Now.ToShortDateString(); ;
            
string strUserName = DDLUserName.Items[DDLUserName.SelectedIndex].Text;
            
if (TextBoxFirstDate.Text.Trim().Length == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                dateStart 
= dateStart + " " + DDLFirstHours.Items[DDLFirstHours.SelectedIndex].Text + ":" + DDLFirstMinutes.Items[DDLFirstMinutes.SelectedIndex].Text;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                dateStart 
= TextBoxFirstDate.Text + " " + DDLFirstHours.Items[DDLFirstHours.SelectedIndex].Text + ":" + DDLFirstMinutes.Items[DDLFirstMinutes.SelectedIndex].Text;
            }

            
if (TextBoxLastDate.Text.Trim().Length == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                dateEnd 
= dateEnd + " " + DDLLastHours.Items[DDLLastHours.SelectedIndex].Text + ":" + DDLLastMinutes.Items[DDLLastMinutes.SelectedIndex].Text;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            List
<SPFile> docItemList = new List<SPFile>();
            Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(
delegate()
ExpandedSubBlockStart.gifContractedSubBlock.gif           
{
               SPSite site 
= SPContext.Current.Site;
               SPWeb spweb 
= SPContext.Current.Web;
               spweb.AllowUnsafeUpdates 
= true;
               
using (SPSite ElevatedsiteColl = new SPSite(site.ID))
ExpandedSubBlockStart.gifContractedSubBlock.gif               
{
                   
using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(spweb.ID))
ExpandedSubBlockStart.gifContractedSubBlock.gif                   
{
                       SPListCollection listcollection 
= spweb.Lists;
                       
foreach (SPList list in listcollection)
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
{
                           
if (list.BaseType == SPBaseType.DocumentLibrary && list.BaseTemplate != SPListTemplateType.ListTemplateCatalog
                               
&& list.BaseTemplate == SPListTemplateType.DocumentLibrary)
ExpandedSubBlockStart.gifContractedSubBlock.gif                           
{
                               SPListItemCollection itemcollection 
= list.Items;
                               
foreach (SPListItem item in itemcollection)
ExpandedSubBlockStart.gifContractedSubBlock.gif                               
{
                                   
//if (item.ParentList.Title == "文档")

                                   
if (item.ParentList.BaseType == SPBaseType.DocumentLibrary)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                   
{
                                       
if ((Convert.ToDateTime(dateStart) <= item.File.TimeCreated) && (item.File.TimeCreated <= Convert.ToDateTime(dateEnd)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                       
{
                                           
if (item.File.Author != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                           
{
                                               
if (UserName == "所有用户")
ExpandedSubBlockStart.gifContractedSubBlock.gif                                               
{
                                                   docItemList.Add(item.File);
                                               }

                                               
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                               
{
                                                   SPUser su 
= item.File.Author;
                                                   
if ((su.LoginName.ToLower() == UserName.ToLower()))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                   
{
                                                       docItemList.Add(item.File);
                                                   }

                                               }

                                           }

                                       }

                                   }

                               }

                           }

                       }

                   }

               }

               
           }
);
            
return docItemList;
        }




ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 页面数据绑定
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        protected void dgSite_ItemDataBound(object sender, DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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");
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 页面功能
        
/// </summary>
        
/// <param name="source"></param>
        
/// <param name="e"></param>

        protected void dgSite_ItemCommand(object source, DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 分页
        
/// </summary>
        
/// <param name="source"></param>
        
/// <param name="e"></param>

        protected void dgSite_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            dgSite.CurrentPageIndex 
= e.NewPageIndex;
            dgSite.DataBind();
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 文件类
    
/// </summary>

    public class FileClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public string FileName;
        
private string _FileName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return _FileName;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                _FileName 
= value;
            }

        }

        
public string FileCreatTime;
        
private string _FileCreatTime
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return _FileCreatTime;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                _FileCreatTime 
= value;
            }

        }

        
public string FilePath;
        
private string _FilePath
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                _FilePath 
= value;
            }

            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return _FilePath;
            }

        }

    }

}

转载于:https://www.cnblogs.com/Bright-Liang/archive/2009/05/25/1489044.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值