【问题标题】:SharePoint - How can I customize NewForm.aspx in custom feature?SharePoint - 如何在自定义功能中自定义 NewForm.aspx?
【发布时间】:2010-10-10 14:10:59
【问题描述】:

我在 SharePoint 网站中创建了一个自定义列表,并使用 SharePoint 解决方案生成器生成了一个 Visual Studio 2008 项目。我可以将其打包为一个功能并安装它。它在我的服务器上运行良好。

在对此进行测试后,我已经能够将自定义母版页添加到部署到 _catalogs/masterpage 文件夹的功能中。这里是:

<Elements Id="2196306F-3F37-40b5-99CF-42E89B93888A" xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Name="DefaultMasterPage" Url="_catalogs/masterpage" RootWebOnly="FALSE">
      <File Url="gcbranding.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" />
    </Module>
</Elements>

现在我的网站中有一个自定义母版页,我希望将其用于创建新项目。但我不想从 SharePoint Designer 设置母版。

回到生成的解决方案,它具有带有列表架构的 NewForm.aspx 等。我该怎么...

  1. 自定义为新项目显示的表单,并将其重定向到thankyou.aspx 页面,而不是显示所有列表项目?
  2. 正确设置母版页的url?

我不知道第 1 点。我需要创建一个自定义 Web 部件并将其嵌入到 NewForm.aspx 中吗?

关于第 2 点,我取得了一些进展,但遇到了问题。如果我在我的 NewForm.aspx 中设置这样的主...

 MasterPageFile="~/masterurl/gcmaster.master"

它会安装好,但是当我点击该站点时,我收到一个错误,因为 URL 中不允许使用 ~。如果我在指令中使用 _catalogs/masterpage,它将找不到主页面,因为 URL 是相对的。只有这段代码似乎有效:

MasterPageFile="../../_catalogs/masterpage/gcmaster.master"

在部署自定义功能/解决方案时,在 SharePoint 中设置母版页文件的最佳做法是什么?

【问题讨论】:

    标签: sharepoint sharepoint-2007 moss wss wss-3.0


    【解决方案1】:

    Re Masterpage:我想你想要'~masterurl/gcmaster.master'。 “~”和“master”之间没有“/”。

    Re NewForm:您可以为 NewForm.aspx 创建自己的代码隐藏页面,将Inherits 属性更改为您自己的类。我想我会先让我的自定义代码继承自SharePoint.SharePoint.WebPartPages.WebPartPage,然后从那里开始。

    【讨论】:

      【解决方案2】:

      自定义为新项目显示的表单,并将其重定向到thankyou.aspx 页面,而不是显示所有列表项目?

      最简单的答案是,更改“添加新项目”链接,为您的感谢页面添加源 URL。例如,而不是(我不得不省略 http):

      www.yoursite.com/Lists/Links/NewForm.aspx

      你把它改成:

      www.yoursite.com/Lists/Links/NewForm.aspx?Source=www.yoursite.com/ThankYou.aspx

      当用户从 NewForm 页面单击提交时,他们将被重定向到ThankYou.aspx。

      不过,您可能必须使用 SharePoint Designer 来更改链接。

      【讨论】:

        【解决方案3】:

        最好的方法是在 SharePoint Designer 中打开 NewForm.aspx,更改:

        MasterPageFile="~masterurl/default.master"

        MasterPageFile="~masterurl/custom.master"

        这显然会编辑当前实例,但如果您想使用站点定义或功能部署它,那么您需要在与列表实例文件夹中的 schema.xml 相同的文件夹中创建 NewForm.aspx 页面。

        希望对您有所帮助。

        【讨论】:

          【解决方案4】:

          覆盖网页上的 OnPreInit 事件,并在其中放入以下行:

          this.MasterPageFile = SPContext.Current.Web.CustomMasterUrl;
          

          (如果您使用的是自定义母版页)

          如果您使用的是默认母版页,请使用以下行:

          this.MasterPageFile =  SPContext.Current.Web.MasterUrl;
          

          HTH,

          詹姆斯

          【讨论】:

          • 我忘了说:部署的母版页必须设置为自定义或默认母版页。这发生在功能激活阶段,您必须实现继承的 SPFeatureReceiver 类来执行此操作。 SPFeatureReceiver 子类当然必须在 feature.xml 文件中设置。
          【解决方案5】:

          如果您想在各种 .aspx 页面中重复使用母版页,那么最好将其设置为自定义母版页。 WSS 中没有可让您轻松执行此操作的页面,因此您最好在 SharePoint Designer 中右键单击您的 _catalogs/masterpage/gcmaster.master 文件并选择设置为自定义母版页,或者通过 PowerShell 在服务器上设置它(SharePoint 脚本头在此处可用:http://sharepoint.microsoft.com/blogs/zach/Lists/Posts/Post.aspx?ID=7

          $web = Get-SPWeb("http://mysiteurl")
          $web.CustomMasterUrl = "_catalogs/masterpage/gcmaster.master"
          $web.Update()
          

          然后在你的.aspx页面的@Page指令中,你可以设置:

          MasterPageFile="~/masterurl/custom.master"
          

          请记住,这会使您网站中所有引用“~/masterurl/custom.master”的 .aspx 页面都使用您的 gcmaster.master 页面。

          或者,您可以跳过所有这些,并为您的 .aspx 页面简单地包含一个如下所示的 @Page 指令:

          MasterPageFile="~/site/_catalogs/masterpage/gcmaster.master"
          

          【讨论】:

            猜你喜欢
            • 2017-04-23
            • 1970-01-01
            • 2018-10-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-27
            • 2019-08-02
            • 1970-01-01
            相关资源
            最近更新 更多