在MOSS中后臺管理的頁面都是Application Page,比如網站設置的頁面(settings.aspx)就是典型的Application Page,它不能被Sharepoint Desiger定制。如果我們要修改只能手動的使用其他工具來修改,我們也可以添加Application Page,必須放在C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS目錄下,它對應的虛擬路徑為_layouts。所有的Application Page都使用application.master這個母版頁,我們自定義的Application Page也是一樣,可以使用內嵌代碼也可以使用后置代碼。自定義的application page繼承自LayoutsPageBase類,下面我們就來做兩個自定義的Application Page,下面是項目的結構:
Feature.xml中代碼如下:
<?xml version="1.0" encoding="utf-8"??>
<Feature?Id="86689158-7048-4421-AD21-E0DEF0D67C81"?
???Title="自定義ApplicationPage"
???Description="使用SPTreeViw演示自定義ApplicationPage"
???Version="1.0.0.0"
???Scope="Web"
???Hidden="FALSE"?????????
???ImageUrl="TPG\PithHelmet.gif"?????????
???xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest?Location="elements.xml"?/>
</ElementManifests>
</Feature>
ApplicationPage1.aspx和ApplicationPage2.aspx就是我們自定義的Application Page,ApplicationPage1.aspx演示的是在一個列表庫的列表項的編輯菜單里出現一個鏈接,統計該列表的信息,如下圖:
要添加此菜單須在Elements.xml中填加如下代碼:
<CustomAction?Id="CustomApplicationPage1"
RegistrationType="List"
RegistrationId="101"
ImageUrl="/_layouts/images/GORTL.GIF"
Location="EditControlBlock"
Sequence="240"
Title="此文檔庫信息"?>
<UrlAction?Url="~site/_layouts/CustomApplicationPages/ApplicationPage1.aspx?ItemId={ItemId}&ListId={ListId}"/>
</CustomAction>
RegistrationType="List":代表注冊的類型是列表.
Location="EditControlBlock":代表菜單將出現在控件編輯項當中.
UrlAction 是它的鏈接,URL中的ItemId 和ListId是通過 QueryString得到的。
ApplicationPage1.cs中代碼如下:
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?Microsoft.SharePoint;
using?Microsoft.SharePoint.WebControls;
namespace?CustomApplicationPages?
{
??public?class?ApplicationPage1?:?LayoutsPageBase?
?{
????protected?Label?lblSiteTitle;
????protected?Label?lblSiteID;
????protected?Label?lblSiteUrl;
????protected?Label?lblListID;
????protected?Label?lblListTile;
????protected?Label?lblRootFolderUrl;
????protected?Label?lblDocumentID;
????protected?Label?lblDocumentName;
????protected?Label?lblDocumentUrl;
????protected?Label?lblDocumentTemplateUrl;
????protected?Label?lblFileAuthor;
????protected?Label?lblFileSize;
????protected?Label?lblFileLastModified;
????protected?Label?lblFileCheckOutStatus;
????protected?override?void?OnLoad(EventArgs?e)?
?? {
????? SPSite?siteCollection?=?this.Site;
??????SPWeb?site?=?this.Web;
??????lblSiteTitle.Text?=?site.Title;
??????lblSiteUrl.Text?=?site.Url.ToLower();??????
??????string?ListId?=?Request.QueryString["ListId"];
??????lblListID.Text?=?ListId;
??????SPList?list?=?site.Lists[new?Guid(ListId)];
??????lblListTile.Text?=?list.Title;
??????lblRootFolderUrl.Text?=?list.RootFolder.Url;??????
??????string?ItemId?=?Request.QueryString["ItemId"];
??????lblDocumentID.Text?=?ItemId;
??????SPListItem?item?=?list.Items.GetItemById(Convert.ToInt32(ItemId));
??????lblDocumentName.Text?=?item.Name;
??????lblDocumentUrl.Text?=?item.Url;
??????if?(list?is?SPDocumentLibrary)
?? ?{
????????SPDocumentLibrary?documentLibrary?=?(SPDocumentLibrary)list;
????????lblDocumentTemplateUrl.Text?=?documentLibrary.DocumentTemplateUrl;
????????SPFile?file?=?site.GetFile(item.Url);
????????lblFileAuthor.Text?=?file.Author.Name;
????????lblFileSize.Text?=?file.TotalLength.ToString("0,###")?+?"?bits";
????????lblFileLastModified.Text?=?"By?"?+?file.ModifiedBy.Name?+
???????????????????????????????????"?on?"?+?file.TimeLastModified.ToLocalTime().ToString();
????????lblFileCheckOutStatus.Text?=?file.CheckOutStatus.ToString();
??????}
????}
????
??}
}
結果如下圖:
ApplicationPage2.aspx中我們使用控件SPTreeView來顯示該站點的文件夾結構,我們將菜單添加到“網站操作“中,并且設置只有管理員權限才可以看到,如下圖:
Elements.xml中填加如下代碼:
<CustomAction?Id="CustomApplicationPage2"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="2006"
Title="獲取站點信息"
Description="使用SPTreeView獲取站點信息"
RequireSiteAdministrator="True">
<UrlAction?Url="~site/_layouts/CustomApplicationPages/ApplicationPage2.aspx"/>
</CustomAction>
RequireSiteAdministrator="True":改屬性說明該項操作只有擁有管理員權限的用戶才可以操作
ApplicationPage2.cs中代碼如下:
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?Microsoft.SharePoint;
using?Microsoft.SharePoint.WebControls;
namespace?CustomApplicationPages
{
??public?class?ApplicationPage2?:?LayoutsPageBase?
??{????
????protected?SPTreeView?treeSitesFiles;
????const?string?siteImg?=?@"\_layouts\images\FPWEB16.GIF";
????const?string?foloerImg?=?@"\_layouts\images\FOLDER16.GIF";
????const?string?ghostedFileImg?=?@"\_layouts\images\NEWDOC.GIF";
????const?string?unGhostedFileImg?=?@"\_layouts\images\RAT16.GIF";
????protected?override?void?OnLoad(EventArgs?e)
????{
??????SPWeb?site?=?SPContext.Current.Web;
??????SPFolder?rootFolder?=?site.RootFolder;
??????TreeNode?rootNode?=?new?TreeNode(site.Url,?site.Url,?siteImg);
??????LoadFolderNodes(rootFolder,?rootNode);
??????treeSitesFiles.Nodes.Add(rootNode);
??????treeSitesFiles.ExpandDepth?=?1;
????}
????protected?void?LoadFolderNodes(SPFolder?folder,?TreeNode?folderNode)
????{
??????foreach?(SPFolder?childFolder?in?folder.SubFolders)?
??????{
????????TreeNode?childFolderNode?=?new?TreeNode(childFolder.Name,?childFolder.Name,?foloerImg);
????????childFolderNode.NavigateUrl?=?Site.MakeFullUrl(childFolder.Url);
????????LoadFolderNodes(childFolder,?childFolderNode);????????
????????folderNode.ChildNodes.Add(childFolderNode);
??????}
??????foreach?(SPFile?file?in?folder.Files)?
??????{
????????TreeNode?fileNode;
????????if?(file.CustomizedPageStatus?==?SPCustomizedPageStatus.Uncustomized)?
????????{
????????????fileNode?=?new?TreeNode(file.Name,?file.Name,?ghostedFileImg);??????????
????????}
????????else?
????????{
????????????fileNode?=?new?TreeNode(file.Name,?file.Name,?unGhostedFileImg);
????????}
????????fileNode.NavigateUrl?=?Site.MakeFullUrl(file.Url);
????????folderNode.ChildNodes.Add(fileNode);
??????}
????}?
??}
}
效果如下圖:
如何調試:
1.修改當前web應用程序的配置文件如下:
??<SharePoint>
????<SafeMode?CallStack="true"?/>
???</SharePoint>
??<system.web>
????<customErrors?mode="Off"?/>
????<compilation?debug="true"?/>
??</system.web>
</configuration>
2.然后附加w3wp進程,設置斷點即可調試了。
本文轉自生魚片博客園博客,原文鏈接:http://www.cnblogs.com/carysun/archive/2008/04/19/applicationpage.html,如需轉載請自行聯系原作者