我們先看一下配置界面的樣子:
wss(moss)的所有配置頁面都放到C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/LAYOUTS目錄
或子目錄中,我們將字段權限配置頁面(FieldEditControl.aspx)放到Layouts的子目錄CodeArt中。
配置界面的核心邏輯開發成一個webcontrol(FieldRightSettingPart),將這個webpart直接嵌入到管理頁面,管理頁面的代碼如下:
<%@ Page Language="C#" Inherits="Microsoft.SharePoint.ApplicationPages.NewListPage" MasterPageFile="~/_layouts/application.master" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Assembly="CodeArt.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2c606279787b575f" Namespace="CodeArt.SharePoint.WebPart" TagPrefix="codeArt" %>
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
列表字段權限設置
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderId="PlaceHolderMain" runat="server">
<codeArt:FieldRightSettingPart runat="server" id="fSetting" />
</asp:Content>
wss開發中經常會碰到配置信息存儲的問題,如果是webpart,我們可以用webpart的屬性來存儲,其他情況下,我們可以考慮用數據庫或List來存儲。
這裏我選擇用文檔庫來存儲,將配置類序列化成xml存儲到一個文檔庫中。考慮到以後還會碰到這類配置信息存儲的情況,把這個功能開發成一個通用的類CongfigManager:(爲了以後“可能的”替換數據存儲方式,用了工程模式)
public abstract class ConfigManager
{
public static ConfigManager GetConfigManager(string key)
{
return new DocLibConfigManager( key );
}
public virtual T GetConfigData<T>(Guid id) where T : class , new()
{
object obj = this.GetConfigData(typeof(T), id);
if( obj == null )
return null;
return
(T)obj;
}
public virtual object GetConfigData(Type t, Guid id)
{
return null ;
}
public virtual void SetConfigData(Guid id , object obj)
{
}
public virtual void ClearConfigData(Guid id)
{
}
}
class DocLibConfigManager : ConfigManager
{
private string _key;
public DocLibConfigManager(string key)
{
_key = key;
}
SPList EnsureList(SPWeb web)
{
SPList list = null;
try
{
list = web.Lists[_key];
}
catch { }
if (list == null)
{
web.AllowUnsafeUpdates = true;
Guid listId = web.Lists.Add(_key, "List for config , never delete this list.", SPListTemplateType.DocumentLibrary);
list = web.Lists[listId];
}
return list;
}
public override void SetConfigData( Guid id ,object obj)
{
SPList list = null;
string xml = SerializeUtil.Seralize(obj);
byte[] content = Encoding.UTF8.GetBytes(xml);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedsiteColl = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb elevatedWeb = elevatedsiteColl.OpenWeb(SPContext.Current.Web.ID))
{
list = this.EnsureList(elevatedWeb);
elevatedWeb.AllowUnsafeUpdates = true;
SPFile file = list.RootFolder.Files.Add( id.ToString() + ".xml", content, true);
}
}
});
}
private SPListItem GetItem(SPList list, Guid id)
{
SPQuery q = new SPQuery();
q.Query = "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + id.ToString() + ".xml</Value></Eq></Where>";
q.RowLimit = 1;
SPListItemCollection items = list.GetItems(q);
if (items.Count == 0)
return null;
else
return items[0];
}
public override void ClearConfigData(Guid id)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedsiteColl = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb elevatedWeb = elevatedsiteColl.OpenWeb(SPContext.Current.Web.ID))
{
try
{
SPList list = this.EnsureList(elevatedWeb);
elevatedWeb.AllowUnsafeUpdates = true;
SPListItem item = this.GetItem(list, id);
if( item != null )
item.Delete();
}
catch { throw; }
}
}
});
}
public override object GetConfigData(Type t, Guid id)
{
object obj = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedsiteColl = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb elevatedWeb = elevatedsiteColl.OpenWeb(SPContext.Current.Web.ID))
{
SPList list = this.EnsureList(elevatedWeb);
try
{
SPListItem item = this.GetItem(list, id);
if (item != null)
{
SPFile file = item.File;
XmlDocument doc = new XmlDocument();
doc.Load(item.File.OpenBinaryStream());
obj = SerializeUtil.Deserialize(t, doc.OuterXml);
}
}
catch { throw; }
}
}
});
return obj;
}
}
這個CongfigManager實現了對一個配置類的保存,獲取和刪除。
下面考慮配置類如何抽象化
用以下類來表示每個字段的配置信息:
public class FieldEditSetting
{
public string FieldName;
public bool CreatorCanEdit;
public bool AllUserCanEdit ;
public string SpecialAccounts;
public bool IsInSpecialAccounts(string account)
{
if (String.IsNullOrEmpty(SpecialAccounts))
return false;
string checkList = "," + this.SpecialAccounts.ToLower() + ",";
return checkList.IndexOf("," + account.ToLower() + ",") != -1;
}
public bool CanEdit( SPUser currentUser , SPUser creatUser )
{
//if (currentUser.IsSiteAdmin) return true;
if (this.AllUserCanEdit) return true;
if (this.CreatorCanEdit && String.Compare(currentUser.LoginName, creatUser.LoginName, true) == 0)
return true;
return this.IsInSpecialAccounts(currentUser.LoginName);
}
}
public class ListFieldEditSetting : List<FieldEditSetting>
{
public const string Config_List = "__CodeArt_ListFieldEditSetting";
public FieldEditSetting GetByFieldName(string fieldName)
{
foreach (FieldEditSetting fSetting in this)
{
if (String.Compare(fSetting.FieldName, fieldName, true) == 0)
return fSetting;
}
return null;
}
public void Save(Guid listId)
{
ConfigManager cmg = ConfigManager.GetConfigManager(ListFieldEditSetting.Config_List);
cmg.SetConfigData(listId, this);
}
public static ListFieldEditSetting GetListSetting(Guid listId)
{
ConfigManager cmg = ConfigManager.GetConfigManager(ListFieldEditSetting.Config_List);
ListFieldEditSetting setting = cmg.GetConfigData<ListFieldEditSetting>(listId);
return setting;
}
}
以上的基礎類建好了,可以開始FieldRightSettingPart的開發了:
public class FieldRightSettingPart : BaseSPListWebPart
{
private Button _btnSubmit;
private Table _layoutTable;
private Dictionary<string, CheckBox> _AllUserCanEditControls = new Dictionary<string, CheckBox>();
private Dictionary<string, CheckBox> _CreatorCanEditControls = new Dictionary<string, CheckBox>();
private Dictionary<string, Microsoft.SharePoint.WebControls.PeopleEditor> _SpecialAccountsControls =
new Dictionary<string, Microsoft.SharePoint.WebControls.PeopleEditor>();
protected override void CreateChildControls()
{
if (List == null)
return;
_layoutTable = new Table ();
_layoutTable.BorderWidth = new Unit("0px");
_layoutTable.CssClass = "ms-formtable";
_layoutTable.CellSpacing = 0;
this.Controls.Add(_layoutTable);
this.AddRow(_layoutTable, "<b>字段</b>", "<b>編輯權限</b>");
//
ConfigManager cmg = ConfigManager.GetConfigManager(ListFieldEditSetting.Config_List);
ListFieldEditSetting setting = cmg.GetConfigData<ListFieldEditSetting>(List.ID);
//
foreach (SPField f in List.Fields)
{
if (f.Hidden || f.ReadOnlyField ) continue ;
TableRow row = new TableRow();
_layoutTable.Rows.Add(row);
TableCell fieldCell = new TableCell();
fieldCell.VerticalAlign = VerticalAlign.Top;
fieldCell.CssClass = "ms-formlabel";
row.Cells.Add(fieldCell);
fieldCell.Text = f.Title + f.AuthoringInfo;
TableCell ctlCell = new TableCell();
ctlCell.VerticalAlign = VerticalAlign.Top;
ctlCell.CssClass = "ms-formbody";
row.Cells.Add(ctlCell);
CheckBox allUser = new CheckBox();
allUser.Text = "所有人員";
allUser.Checked = true;
ctlCell.Controls.Add(allUser);
this.AddHtml("<br/>", ctlCell);
_AllUserCanEditControls.Add(f.InternalName, allUser);
CheckBox creator = new CheckBox();
creator.Text = "創建者";
creator.Checked = true;
ctlCell.Controls.Add(creator);
this.AddHtml("<br/>", ctlCell);
_CreatorCanEditControls.Add(f.InternalName, creator);
this.AddHtml("指定人員:<br/>", ctlCell );
Microsoft.SharePoint.WebControls.PeopleEditor peopleEditor = new Microsoft.SharePoint.WebControls.PeopleEditor();
peopleEditor.MultiSelect = true;
peopleEditor.Rows = 1;
peopleEditor.Width = new Unit("200px");
ctlCell.Controls.Add(peopleEditor);
_SpecialAccountsControls.Add(f.InternalName, peopleEditor);
this.SetControlValue(allUser, creator, peopleEditor, setting, f.InternalName);
}
_btnSubmit = new Button();
_btnSubmit.ID = "btn1";
_btnSubmit.Text = "確定";
_btnSubmit.CssClass = "ms-ButtonHeightWidth";
this.Controls.Add(_btnSubmit);
_btnSubmit.Click += new EventHandler(_btnSubmit_Click);
}
void SetControlValue(CheckBox allUser, CheckBox creator, Microsoft.SharePoint.WebControls.PeopleEditor peopleEditor ,ListFieldEditSetting setting , string fieldName )
{
if (setting == null || setting.Count == 0 || Page.IsPostBack )
return ;
FieldEditSetting set = setting.GetByFieldName(fieldName);
if (set == null) return;
allUser.Checked = set.AllUserCanEdit;
creator.Checked = set.CreatorCanEdit;
peopleEditor.CommaSeparatedAccounts = set.SpecialAccounts;
}
void _btnSubmit_Click(object sender, EventArgs e)
{
ListFieldEditSetting setting = new ListFieldEditSetting();
foreach (SPField f in List.Fields)
{
if (f.Hidden || f.ReadOnlyField) continue;
FieldEditSetting set = new FieldEditSetting();
setting.Add(set);
set.FieldName = f.InternalName.ToLower();
CheckBox allUser = _AllUserCanEditControls[f.InternalName];
set.AllUserCanEdit = allUser.Checked;
CheckBox creator = _CreatorCanEditControls[f.InternalName];
set.CreatorCanEdit = creator.Checked;
Microsoft.SharePoint.WebControls.PeopleEditor peopleEditor = _SpecialAccountsControls[f.InternalName];
set.SpecialAccounts = peopleEditor.CommaSeparatedAccounts;
}
setting.Save(base.List.ID);
if (Page.Request.QueryString["ListId"] != null)
{
string sourceUrl = base.Web.ServerRelativeUrl + "_layouts/listedit.aspx?List=" + base.List.ID.ToString("B").ToUpper();
Page.Response.Redirect(sourceUrl);
}
//ConfigManager cmg = ConfigManager.GetConfigManager(ListFieldEditSetting.Config_List);
//cmg.SetConfigData(List.ID, setting);
}
//void AddRow( Table table , params Control[] ctls)
//{
// TableRow row = new TableRow();
// table.Rows.Add(row);
// foreach (Control c in ctls)
// {
// TableCell cell = new TableCell();
// row.Cells.Add(cell);
// cell.Controls.Add(c);
// }
//}
void AddRow(Table table, params string[] texts)
{
TableRow row = new TableRow();
table.Rows.Add(row);
foreach (string c in texts)
{
TableCell cell = new TableCell();
row.Cells.Add(cell);
cell.Text = c;
}
}
}
FieldRightSettingPart會在內部生成一個佈局表格和很多的子控件,爲了便於操作這些子 控件,
聲明瞭三個字典類型的變量,來放置生成的控件,字典key爲字段名,值爲對應的編輯控件:
private Dictionary<string, CheckBox> _CreatorCanEditControls = new Dictionary<string, CheckBox>();
private Dictionary<string, Microsoft.SharePoint.WebControls.PeopleEditor> _SpecialAccountsControls =
new Dictionary<string, Microsoft.SharePoint.WebControls.PeopleEditor>();
protected override void CreateChildControls()
{
if (List == null)
return;
_layoutTable = new Table ();
_layoutTable.BorderWidth = new Unit("0px");
_layoutTable.CssClass = "ms-formtable";
_layoutTable.CellSpacing = 0;
this.Controls.Add(_layoutTable);
this.AddRow(_layoutTable, "<b>字段</b>", "<b>編輯權限</b>");
//
ConfigManager cmg = ConfigManager.GetConfigManager(ListFieldEditSetting.Config_List);
ListFieldEditSetting setting = cmg.GetConfigData<ListFieldEditSetting>(List.ID);
//
foreach (SPField f in List.Fields)
{
if (f.Hidden || f.ReadOnlyField ) continue ;
TableRow row = new TableRow();
_layoutTable.Rows.Add(row);
TableCell fieldCell = new TableCell();
fieldCell.VerticalAlign = VerticalAlign.Top;
fieldCell.CssClass = "ms-formlabel";
row.Cells.Add(fieldCell);
fieldCell.Text = f.Title + f.AuthoringInfo;
TableCell ctlCell = new TableCell();
ctlCell.VerticalAlign = VerticalAlign.Top;
ctlCell.CssClass = "ms-formbody";
row.Cells.Add(ctlCell);
CheckBox allUser = new CheckBox();
allUser.Text = "所有人員";
allUser.Checked = true;
ctlCell.Controls.Add(allUser);
this.AddHtml("<br/>", ctlCell);
_AllUserCanEditControls.Add(f.InternalName, allUser);
CheckBox creator = new CheckBox();
creator.Text = "創建者";
creator.Checked = true;
ctlCell.Controls.Add(creator);
this.AddHtml("<br/>", ctlCell);
_CreatorCanEditControls.Add(f.InternalName, creator);
this.AddHtml("指定人員:<br/>", ctlCell );
Microsoft.SharePoint.WebControls.PeopleEditor peopleEditor = new Microsoft.SharePoint.WebControls.PeopleEditor();
peopleEditor.MultiSelect = true;
peopleEditor.Rows = 1;
peopleEditor.Width = new Unit("200px");
ctlCell.Controls.Add(peopleEditor);
_SpecialAccountsControls.Add(f.InternalName, peopleEditor);
this.SetControlValue(allUser, creator, peopleEditor, setting, f.InternalName);
}
_btnSubmit = new Button();
_btnSubmit.ID = "btn1";
_btnSubmit.Text = "確定";
_btnSubmit.CssClass = "ms-ButtonHeightWidth";
this.Controls.Add(_btnSubmit);
_btnSubmit.Click += new EventHandler(_btnSubmit_Click);
}
按鈕提交時,遍歷編輯子控件,組裝配置類,調用ConfigManage類保存數據。:
void _btnSubmit_Click(object sender, EventArgs e)
{
ListFieldEditSetting setting = new ListFieldEditSetting();
foreach (SPField f in List.Fields)
{
if (f.Hidden || f.ReadOnlyField) continue;
FieldEditSetting set = new FieldEditSetting();
setting.Add(set);
set.FieldName = f.InternalName.ToLower();
CheckBox allUser = _AllUserCanEditControls[f.InternalName];
set.AllUserCanEdit = allUser.Checked;
CheckBox creator = _CreatorCanEditControls[f.InternalName];
set.CreatorCanEdit = creator.Checked;
Microsoft.SharePoint.WebControls.PeopleEditor peopleEditor = _SpecialAccountsControls[f.InternalName];
set.SpecialAccounts = peopleEditor.CommaSeparatedAccounts;
}
setting.Save(base.List.ID);
if (Page.Request.QueryString["ListId"] != null)
{
string sourceUrl = base.Web.ServerRelativeUrl + "_layouts/listedit.aspx?List=" + base.List.ID.ToString("B").ToUpper();
Page.Response.Redirect(sourceUrl);
}
}
game over!
附1:存放配置信息的文檔庫:
附2:配置信息序列化後的xml格式:
<ArrayOfFieldEditSetting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FieldEditSetting>
<FieldName>fileleafref</FieldName>
<CreatorCanEdit>false</CreatorCanEdit>
<AllUserCanEdit>false</AllUserCanEdit>
<SpecialAccounts /></FieldEditSetting>
<FieldEditSetting>
<FieldName>title</FieldName>
<CreatorCanEdit>false</CreatorCanEdit>
<AllUserCanEdit>false</AllUserCanEdit>
<SpecialAccounts /></FieldEditSetting>
</ArrayOfFieldEditSetting>
本系列的所有文章:
CodeArt WSS3.0(MOSS)字段編輯權限控制解決方案(v1.0)
CodeArt WSS3.0(MOSS)字段編輯權限控制解決方案的實現 -- 概要
MOSS字段編輯權限控制方案的實現(1)-管理頁面的開發和配置信息的持久化
WSS頁面定製系列(2)---定製單個列表的表單頁面
WSS頁面定製系列(3)---重寫表單的保存邏輯
MOSS字段編輯權限控制方案(4)-打包解決方案