zoukankan      html  css  js  c++  java
  • 自定义MOSS网站的masterpage

    Feature是MOSS中一个重要的东西,Feature 使得基于sharepoint的用户功能定制变得简单和方便。我们使用Feature来实现网站的MasterPage的更换。写一个方法,继承SPFeatureReceiver这个抽象类,实现抽象方法即可。四个方法:FeatureActivated,FeatureDeactivating,FeatureInstalled,FeatureUninstalling,分别是激活反激活,安装反安装对应的事件。我们实现FeatureActivated和FeatureDeactivating的方法。代码如下:

     1public class MySiteFeature : SPFeatureReceiver
     2{
     3    public override void FeatureActivated(SPFeatureReceiverProperties properties)
     4    {
     5        this.ChangeMasterPage(properties, FeatureActiveAction.Change);
     6    }
     
     7
     8    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
     9    {
    10        this.ChangeMasterPage(properties, FeatureActiveAction.UnChange);
    11    }
     
    12
    13    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    14    {
    15        //this.ChangeMasterPage(properties, FeatureActiveAction.Change);
    16    }
     
    17
    18    public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
    19    {
    20        //this.ChangeMasterPage(properties, FeatureActiveAction.UnChange);
    21    }
     
    22
    23    private void ChangeMasterPage(SPFeatureReceiverProperties properties, FeatureActiveAction action)
    24    {
    25        if(properties == null)
    26        {
    27            return;
    28        }

    29        try
    30        {
    31            string newMaster = properties.Feature.Properties["NewMasterPage"].Value;
    32            if(!string.IsNullOrEmpty(newMaster))
    33            {
    34                using(SPSite site = properties.Feature.Parent as SPSite)
    35                {
    36                    if(site == null)
    37                    {
    38                        return;
    39                    }
     
    40
    41                    using(SPWeb web = properties.Feature.Parent as SPWeb)
    42                    {
    43                        if(web == null)
    44                        {
    45                            return;
    46                        }

    47                        string tpl = web.WebTemplate.ToUpper();
    48
    49                        //如果为MySite定制的话,使用如下代码判断是个人网站/宿主
    50                        //if(tpl != "SPSPERS" && tpl != "SPSMSITEHOST")
    51                        //{
    52                        //    return;
    53                        //}
    54                        string master = web.MasterUrl;
    55                        if(action == FeatureActiveAction.Change && master.Contains("default.master"))
    56                        {
    57                            web.MasterUrl = Regex.Replace(master, "default.master", newMaster, RegexOptions.IgnoreCase);
    58                        }

    59                        else if(action == FeatureActiveAction.UnChange && master.Contains(newMaster))
    60                        {
    61                            web.MasterUrl = Regex.Replace(master, newMaster, "default.master", RegexOptions.IgnoreCase);
    62                        }

    63                        else
    64                        {
    65                            return;
    66                        }

    67                        web.Update();
    68                    }

    69                }

    70            }

    71        }

    72        catch(Exception ex)
    73        {
    74            //记录日志等。EventLog.WriteEntry("ChangeMasterPage", ex.Message);
    75        }

    76    }

    77}

    78enum FeatureActiveAction
    79{
    80    Change,
    81    UnChange
    82}


    到这里代码部分就写好了。加强签名,然后扔到GAC里。然后进行下一步,建一个Feature.xml。我们可以将Feature定义在几个范围上:Farm(服务器场),WebApplication(Web应用),Site(网站集),Web(网站),但通常定义在网站集或网站范围上。我们的Feature.xml内容如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <Feature
      
    xmlns="http://schemas.microsoft.com/sharepoint/"
      Id
    ="3b387ef0-01c7-4980-bfa2-e6d37b986bac"
      Title
    ="change my masterpage"
      Description
    ="change my masterpage"
      Scope
    ="Site" 
      ReceiverAssembly
    ="MySiteFeatureManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7c96adf2aa593794"
      ReceiverClass
    ="MySiteFeatureManager.MySiteFeature"
     
    >
      
    <ElementManifests>
        
    <ElementFile Location="NewMasterPage.master"/>
        
    <ElementManifest Location="element.xml"/>
      
    </ElementManifests>
      
    <Properties>
         
    <Property Key="NewMasterPage" Value="NewMasterPage.master"/>
      
    </Properties>
    </Feature>

    属性介绍:
    Id:Guid格式,这个可以使用VS2005或者别的工具来生成一个。
    Title:Feature的标题。
    Description:Feature的内容描述。
    Scope:Feature的功能范围(Farm,WebApplication,Site,Web)
    ReceiverAssembly:引用的程序集
    ReceiverClass:引用程序集的类

    ElementManifests这里我们可以看到<ElementFile Location="NewMasterPage.master"/>,所以我们在同一文件夹放一个NewMasterPage.master,masterpage注意问题请查阅相关文章;还有<ElementManifest Location="element.xml"/>,所以还要在同一文件夹放一element.xml文件,这个文件的内容如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
          
    <Module Name="NewMasterPage" RootWebOnly="true" Url="_catalogs/masterpage">
            
    <File Url="NewMasterPage.master" Type="GhostableInLibrary" />
        
    </Module>
    </Elements>

    这里的意思是告诉它,我们的NewMasterPage.master上传到了哪个目录。
    这里<ElementManifest Location="element.xml"/>的element.xml文件名可以随便自己写,只要有这个对应xml文件即可,但是Feature.xml,名称是不可改变的。

    下面安装和激活这个Feature:
    在目录C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES中新建一个文件夹,如:MySiteFeature。将这两个xml放入这个文件夹。
    使用如下命令:
    安装:stsadm –o installfeature –filename MySiteFeature\feature.xml
    激活到网站集:stsadm –o activatefeature –filename MySiteFeature\feature.xml -url http://moss:81

    这样Feature就安装好了。现在我们已经做好了可以更换MasterPage的Feature,在网站集的管理中的网站集功能中可以看到我们的Feature,可以随时激活或者停止我们的Feature的功能。在上面代码的基础上,可以扩展,比如根据用户组来判断是用哪个MasterPage,MasterPage的定义在xml里定义好就行了。


  • 相关阅读:
    ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
    Identity(三)
    数字证书的使用
    自建mvc5项目里几个类图
    asp.net MVC 常见安全问题及解决方案
    security相关链接整理
    js面向对象编程:if中可以使用哪些作为判断条件呢?
    c#创建Windows service (Windows 服务)基础教程
    angular1框架前台笔记
    angular1.几框架新接口添加
  • 原文地址:https://www.cnblogs.com/ycc/p/801753.html
Copyright © 2011-2022 走看看