SharePoint安全...SPSecurity委托,一个值得我们去思考的问题
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
namespace SPGridViewDemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SPSecurity.CodeToRunElevated showLists = new SPSecurity.CodeToRunElevated(ShowLists);
SPSecurity.RunWithElevatedPrivileges(showLists);
}
protected void ShowLists()
{
DataTable entries = new DataTable("Entries");
entries.Columns.Add("ID", typeof(System.Int32));
entries.Columns.Add("SiteNav", typeof(string));
entries.Columns.Add("SiteName", typeof(string));
entries.Columns.Add("SiteURl", typeof(string));
entries.Columns.Add("ListName", typeof(string));
entries.Columns.Add("ListID", typeof(string));
entries.PrimaryKey = new DataColumn[] { entries.Columns["ID"] };
entries.Columns["ID"].AutoIncrement = true;
entries.Columns["ID"].AutoIncrementSeed = 1;
entries.Columns["ID"].AutoIncrementStep = 1;
entries.Columns["ID"].ReadOnly = true;
using (SPSite site = new SPSite(@"http://lh-vmpc:6060/"))
{
foreach (SPWeb web in site.AllWebs)
{
foreach (SPList list in web.GetListsOfType(SPBaseType.GenericList))
{
if(list.DoesUserHavePermissions(site.RootWeb.CurrentUser,SPBasePermissions.AddListItems) && list.BaseTemplate == SPListTemplateType.Tasks)
{
DataRow row = entries.NewRow();
row["SiteName"] = web.Title;
row["SiteNav"] = web.Url;
row["SiteUrl"] = web.Url;
row["ListName"] = list.Title;
row["ListID"] = list.ID;
entries.Rows.Add(row);
}
}
}
}
this.GridView1.DataSource = entries;
GridView1.DataBind();
}
}
}