在WSS 3.0中自定义default.master

时间:2009-03-18 18:47:28

标签: sharepoint wss

有谁知道如何为WSS 3.0网站自定义母版页(即default.master)?实际上,我已经知道如何自定义母版页(即通过SharePoint Designer或文本编辑器),我更感兴趣的是学习如何告诉WSS 3.0使用我的自定义母版页。

我已经使用我的自定义版本重命名了default.master,但是它可以为所有WSS站点设置它。我希望能够使用网页集A的母版A版和其他网站集的单独母版页。

注意,我不是指MOSS 2007.我已经知道如何设置MOSS 2007的母版页。我似乎无法弄清楚如何为WSS 3.0做这件事。

感谢。

2 个答案:

答案 0 :(得分:2)

WSS页面使用母版页,就像常规的网络应用程序一样。但是,如果MasterPageFile属性是一个标记,则该值设置为“~default.master”。此标记将替换为页面的PreInit方法中对母版页的实际引用。

您可以将~default.master的值更改为您喜欢的任何内容。但更好的解决方案是执行与SharePoint相同类型的操作。

我在我的SharePoint网站上添加了一个HttpHandler。处理程序附加到PreRequestHandlerExecute方法,并在SharePoint开始呈现页面之前更改母版页文件属性的值。

在web.config的httpModules部分添加一行,如下所示:       

然后创建一个这样的类:

命名空间MyClassLibrary.Utility {     public class MasterPageHttpModule:IHttpModule     {         public void Init(HttpApplication context)         {             context.PreRequestHandlerExecute + = new EventHandler(context_PreRequestHandlerExecute);         }

    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        bool useCustomMasterPages = Convert.ToBoolean(ConfigurationManager.AppSettings["ShowCustomMasterPages"].ToString());
        if (useCustomMasterPages)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page != null)
            {
                page.PreInit += new EventHandler(page_PreInit);
            }
        }
    }

    void page_PreInit(object sender, EventArgs e)
    {
        bool useThreeColumnMaster = Convert.ToBoolean(ConfigurationManager.AppSettings["UseThreeColumnMasterOnDefaultPage"].ToString());

        try
        {
            Page page = sender as Page;
            if (page != null && SPContext.Current != null)
            {
                string url = page.Request.Url.AbsolutePath.ToLower();
                if (url.IndexOf("/public/") > -1)
                {
                    if (url.IndexOf("sitemap.aspx") == -1)
                    {
                        page.MasterPageFile = "~/_catalogs/masterpage/edge_con.master";
                    }
                    else
                    {
                        page.MasterPageFile = "";
                    }
                }
                else if (url.IndexOf("default.aspx") > -1)
                {
                    if (useThreeColumnMaster)
                    {
                        page.MasterPageFile = "~/_catalogs/masterpage/edge_con.master";
                    }
                    else
                    {
                        page.MasterPageFile = "~/_catalogs/masterpage/edge.master";
                    }
                }
                else if (url.IndexOf("sitemap.aspx") > -1)
                {
                    //
                    //  Sitemap pages should not have a master page
                    //
                    page.MasterPageFile = "";
                    page.Controls.Clear();
                }
                else if (url.IndexOf("/admin/") > -1)
                {
                    page.MasterPageFile = "~/_catalogs/masterpage/edge.master";
                }
                else if (url.IndexOf("/member/") > -1)
                {
                    page.MasterPageFile = "~/_catalogs/masterpage/edge.master";
                }
                else if (url.IndexOf("/supplier/") > -1)
                {
                    page.MasterPageFile = "~/_catalogs/masterpage/edge.master";
                }
                else if (page.MasterPageFile == "~masterurl/default.master")
                {
                    page.MasterPageFile = "~/_catalogs/masterpage/edge.master";
                }
            }
        }
        catch (Exception exception)
        {
            LogWriter logWriter = new LogWriter();
            logWriter.WriteToLog("Could  not set master page: " + exception.Message, LogType.MasterPage, DateTime.Now);
        }
    }

    public void Dispose()
    {
    }
}

}

现在您正在动态选择母版页。

答案 1 :(得分:2)

您需要更改代表站点的SPWeb对象上的MasterUrl属性。执行此操作的一种好方法是创建一个SharePoint功能,该功能在激活时设置属性,并在停用时恢复原始值。 Feature.xml可能如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="8651CC66-FEB1-4255-B7E9-0DFE24367DAB"
          Title="My Master Page"
          Scope="Web"              
          SolutionId="06D3B01F-0C26-457a-BFA5-A1B0BC8D4225"
          ReceiverAssembly="MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9e95445247029289"
          ReceiverClass="MyFeatureReceiver"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="Masterpage.xml"/>
    <ElementFile Location="my.master"/>
  </ElementManifests>
  <Properties>
    <Property Key="MyMaster" Value="my.master" />
  </Properties>
</Feature>

文件Masterpage.xml将您的母版页上传到图库:

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

在WSP解决方案中包含此功能以及包含MyFeatureReceiver类的MyAssembly.dll,如下所示:

public class MyFeatureReceiver : SPFeatureReceiver
{
  public override void FeatureActivated(SPFeatureReceiverProperties properties)
  {
    SPWeb web = (SPWeb)properties.Feature.Parent;
    web.MasterUrl = properties.Definition.Properties["MyMaster"].Value;
    web.Update();      
  }

  public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  {
    SPWeb web = (SPWeb)properties.Feature.Parent;
    web.MasterUrl = "default.master";
    web.Update();      
  }

  public override void FeatureInstalled(SPFeatureReceiverProperties properties)
  {
  }

  public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
  {
  }
}

最后,部署解决方案并激活您网站上的功能。