MOSS 2007入门(5) : 使用Feature定义站点中的菜单项

   前面的文章提到了:Everything is feature.今天就来讲讲feature的使用.在MOSS 2007中feature功能之强大超乎想象,可以说对一个已有的网站功能性的增强最后多数都要通过feature来实现.例如添加webpart,实现Event Handler等等...今天就做一个使用feature定义网站中的菜单项的Demo.

 

  Step(1):在网站中创建一个页面,存放于一个文档库中,这里我创建的页面在文档库SiteCollectionDocuments下testpage.aspx,那么这个页面的引用路径就是<%siteurl%>/SiteCollectionDocuments/testpage.aspx .


  Step(2):在12\TEMPLATE\FEATURES文件架下新建一个文件夹叫LinkToPage,在文件夹下创建Feature.xml.内容如下.

<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="C86A92EA-A5D3-4d65-855C-0557AE5D065A" 
    Title
="Link To Page"
    Description
="This example show how to create a menu linke to a customize page!"
    Version
="1.0.0.0"
    Scope
="Site" 
    AlwaysForceInstall
="TRUE" 
    xmlns
="http://schemas.microsoft.com/sharepoint/">
  
<ElementManifests>
    
<ElementManifest Location="LinkToPage.xml" />
  
</ElementManifests>
</Feature>

  Feature元素的几个属性说明一下:

  Id:这里要使用vs的guid工具生成一个guid,copy过来就可以

  TitleDescription分别是Feature的标题和描述.

  Scope这个属性比较重要:是一个枚举值,是用来描述这个feature的作用范围.这里选择:site

  AlwaysForceInstall:这个属性设置为true 可以在重复安装这个feature中自动覆盖原来安装的那个相同的feature.

  ElementManifestlocation属性制定一个描述feature内容的xml文件,这里叫做LinkToPage.xml

  Step(3):建立上一步中的LinkToPage.xml,内容如下.

 

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  
<CustomAction Id="LinkToPageInactionsMenu"
    RegistrationType
="List"
    RegistrationId
="101"
    GroupId
="ActionsMenu"
    Location
="Microsoft.SharePoint.StandardMenu"
    Sequence
="1000"
    Title
="Link To Page"
    Description
="This is action menu"
    
>
    
<UrlAction Url="~sitecollection/SiteCollectionDocuments/testpage.aspx"/>
  
</CustomAction>
    
<CustomAction Id="LinkToPage"
      RegistrationType
="List"
      RegistrationId
="101"
      GroupId
="NewMenu"
      Location
="Microsoft.SharePoint.StandardMenu"
      Sequence
="1000"
      Title
="Link To Page"
      Description
="This is new menu!"
     
>
  
<UrlAction Url="~sitecollection/SiteCollectionDocuments/testpage.aspx"/>
    
</CustomAction>
</Elements>


  这个xml文件中可以多个CustomAction来描述feature的实际内容,这里采用了两个。

  Locationgroupid两个属性分别指定该feature是定义了哪两个菜单.

  Sequence属性指定新增加的菜单项在这个菜单中的排序,设置的大一点该菜单项排列就靠后.

  TitleDescription分别是该菜单项的标题和描述.

  UrlAction属性指定了该菜单项的连接到的页面,当然了,这个demo连接到的是我们在第一步中建立那个页面.这里有一个相对路径的问题,~site 表示站点的主目录, ~sitecollection  表示站点集的主目录.因为我的demo是做在一个站点集下面的,所以采用~sitecollection后面是连接文件的相对路径..

  Step(4):下面就是将这个feature部署到站点,因为可能会经常使用到部署feature这样的功能,那么我们写一个通用的bat文件来实现feature的部署,内容如下.

 

@rem======================================================================
@rem
@rem    setupfeature.bat
@rem
@rem
======================================================================

@echo off
setlocal
pushd .

goto InstallFeature


@rem----------------------------------------------------------------------
@rem   InstallFeature
@rem----------------------------------------------------------------------
:InstallFeature
    set SPAdminTool
=%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN\stsadm.exe
    set TargetUrl
=http://mymoss:8001
    set FeaturePath
=LinkToPage\Feature.xml
   
    echo InstallFeature %FeaturePath% 
    
"%SPAdminTool%" -o installfeature -filename %FeaturePath%

    echo Activating feature %FeaturePath% 
    
"%SPAdminTool%" -o activatefeature -filename %FeaturePath% -url %TargetUrl%

    echo iisreset
    iisreset


  其中每次只要更改TargetUrlFeaturePath就可以了,是不是方便了很多.

  实际上看这个文件中的内容就知道,通过stsadm命令的-o instanllfeature安装这个feature,然后通过stsadm命令的-o activatefeature激活这个feature,然后iisreset一下.将这个bat文件放在12\TEMPLATE\FEATURES,双击执行就可以.


  Step(5):进入站点新建一个文档库,看看文档库的菜单中是不是多出了两项?

       

  到网站设置的网站集功能中看看,是不是有一个Link to Page的feature已经被激活.

  Step(6): 通过列表的菜单我们可以链接到刚刚所建立的新页面,但是怎么能通过这个链接传递数据呢?也就是说可以在testpage.aspx中获取列表的Id.  ok,将UrlAction更改为<UrlAction Url="~sitecollection/SiteCollectionDocuments/testpage.aspx?listid={ListId}"/> ,  保存文件之后重新运行installfeature.bat,新建一个列表,重新使用该功能定位到testpage.aspx,看看是不是能在querystring中获得列表的guid了.

  ok,以上就是使用feature来Feature定义站点中的菜单项的DEMO.
  Enjoy it....

posted on 2006-12-09 02:38  King Lau  阅读(4515)  评论(2编辑  收藏  举报