在 WSS 3.0 中自定义 default.master

发布于 2024-07-14 20:17:03 字数 338 浏览 12 评论 0原文

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

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

请注意,我不是指 MOSS 2007。我已经知道如何设置 MOSS 2007 的母版页。不过,我似乎不知道如何为 WSS 3.0 执行此操作。

谢谢。

Does anyone know how to customize the master page (i.e. default.master) for a WSS 3.0 site? Actually, I already know how to customize the master page (i.e. via SharePoint Designer, or a text editor), I'm more interested in learning how to tell WSS 3.0 to use my customized master page.

I've renamed the default.master with my customized version and that works, but that sets it for all WSS sites. I want to be able to use version A of a master page for site collection A, and a separate master page for other site collections.

Note, I'm NOT referring to MOSS 2007. I already know how to set the master page for MOSS 2007. I can't seem to figure out how to do it for WSS 3.0 though.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸 2024-07-21 20:17:03

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

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

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

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

然后创建一个像这样的类:

namespace MyClassLibrary.Utility
{
公共类 MasterPageHttpModule :IHttpModule
{
公共无效初始化(HttpApplication上下文)
{
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()
    {
    }
}

您正在动态选择一个母版页面

WSS pages use Master Pages just like regular web apps. However, the value if the MasterPageFile attribute is a token, and is set to "~default.master". This token gets replaced with the actual reference to the master page in the page's PreInit method.

You can change the value of ~default.master to anything you like. But a better solution is to do the same type of thing that SharePoint does.

I added an HttpHandler to my SharePoint site. The handler attaches to the PreRequestHandlerExecute method, and changes the value of the master page file attribute before SharePoint starts rendering the page.

Add a line to the httpModules section of the web.config that looks like this:

Then create a class like this:

namespace 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()
    {
    }
}

}

Now you are dynamically choosing a mater page.

慈悲佛祖 2024-07-21 20:17:03

您需要更改表示站点的 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>

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

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)
  {
  }
}

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

You need to change the MasterUrl property on the SPWeb object representing the site. A good way to do this is to create a SharePoint feature that when activated sets the property, and when deactivated restores the original value. The Feature.xml could look like this:

<?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>

The file Masterpage.xml uploads your master page to the gallery:

 <?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>

Include this feature in a WSP solution along with the MyAssembly.dll containing the MyFeatureReceiver class, which goes like this:

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)
  {
  }
}

Finally, deploy the solution and activate the feature on your site(s).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文