天天看點

MOSS點滴(2):自定義Application Page

在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,下面是項目的結構:

<a href="http://www.cnblogs.com/images/cnblogs_com/carysun/WindowsLiveWriter/MOSS2ApplicationPage_11876/1.jpg"></a>

Feature.xml中代碼如下:

&lt;?xml version="1.0" encoding="utf-8" ?&gt;

&lt;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/"&gt;

&lt;ElementManifests&gt;

&lt;ElementManifest Location="elements.xml" /&gt;

&lt;/ElementManifests&gt;

&lt;/Feature&gt;

ApplicationPage1.aspx和ApplicationPage2.aspx就是我們自定義的Application Page,ApplicationPage1.aspx示範的是在一個清單庫的清單項的編輯菜單裡出現一個連結,統計該清單的資訊,如下圖:

<a href="http://www.cnblogs.com/images/cnblogs_com/carysun/WindowsLiveWriter/MOSS2ApplicationPage_11876/2.jpg"></a>

要添加此菜單須在Elements.xml中填加如下代碼:

&lt;!-- 出現在控件的編輯項中--&gt;

&lt;CustomAction Id="CustomApplicationPage1"

RegistrationType="List"

RegistrationId="101"

ImageUrl="/_layouts/images/GORTL.GIF"

Location="EditControlBlock"

Sequence="240"

Title="此文檔庫資訊" &gt;

&lt;UrlAction Url="~site/_layouts/CustomApplicationPages/ApplicationPage1.aspx?ItemId={ItemId}&amp;amp;ListId={ListId}"/&gt;

&lt;/CustomAction&gt;

RegistrationType="List":代表注冊的類型是清單.

Location="EditControlBlock":代表菜單将出現在控件編輯項當中.

UrlAction 是它的連結,URL中的ItemId 和ListId是通過 QueryString得到的。

ApplicationPage1.cs中代碼如下:

using System;

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();

      }

    }

  }

}

結果如下圖:

<a href="http://www.cnblogs.com/images/cnblogs_com/carysun/WindowsLiveWriter/MOSS2ApplicationPage_11876/3.jpg"></a>

ApplicationPage2.aspx中我們使用控件SPTreeView來顯示該站點的檔案夾結構,我們将菜單添加到“網站操作“中,并且設定隻有管理者權限才可以看到,如下圖:

<a href="http://www.cnblogs.com/images/cnblogs_com/carysun/WindowsLiveWriter/MOSS2ApplicationPage_11876/4.jpg"></a>

Elements.xml中填加如下代碼:

&lt;!-- 有管理者權限才可以察看 --&gt;

&lt;CustomAction Id="CustomApplicationPage2"

GroupId="SiteActions"

Location="Microsoft.SharePoint.StandardMenu"

Sequence="2006"

Title="擷取站點資訊"

Description="使用SPTreeView擷取站點資訊"

RequireSiteAdministrator="True"&gt;

&lt;UrlAction Url="~site/_layouts/CustomApplicationPages/ApplicationPage2.aspx"/&gt;

RequireSiteAdministrator="True":改屬性說明該項操作隻有擁有管理者權限的使用者才可以操作

ApplicationPage2.cs中代碼如下:

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);

    } 

效果如下圖:

<a href="http://www.cnblogs.com/images/cnblogs_com/carysun/WindowsLiveWriter/MOSS2ApplicationPage_11876/5.jpg"></a>

&lt;configuration&gt;

  &lt;SharePoint&gt;

    &lt;SafeMode CallStack="true" /&gt;

   &lt;/SharePoint&gt;

  &lt;system.web&gt;

    &lt;customErrors mode="Off" /&gt;

    &lt;compilation debug="true" /&gt;

  &lt;/system.web&gt;

&lt;/configuration&gt;

2.然後附加w3wp程序,設定斷點即可調試了。

本文轉自生魚片部落格園部落格,原文連結:http://www.cnblogs.com/carysun/archive/2008/04/19/applicationpage.html,如需轉載請自行聯系原作者