在运行时渲染自定义表单/更改现有渲染模板

发布于 2024-08-26 07:40:00 字数 4410 浏览 9 评论 0原文

如何创建可重用的自定义新项目表单+最好,我不想将此表单与内容类型绑定?我想强制渲染一个隐藏字段(它可以在页面上渲染,但使其不可见或在页面上渲染并显示)并以编程方式设置字段值(这就是必须渲染它的原因 - 设置它的值)。

谷歌有大量关于如何使用共享点设计器创建自定义列表表单的信息,但就我而言,我不希望共享点设计器具有您在下面看到的优势。

我想要实现的目标

是能够有一个自定义新表单来创建项目(我不希望它成为默认值)。要打开这个新表单,我将在项目的 ECB 菜单中使用 CustomAction。在这种形式中,我想强制渲染一个隐藏字段并以编程方式设置它的值。

我想从 CustomAction ECB(项目的上下文菜单)打开此表单,因此我不想将其设置为内容类型的默认新表单模板。

<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
      <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
        <New>ListForm</New>
      </FormTemplates>
    </XmlDocument>

想法#1

我可以创建自定义 RenderingTemplate 并将内容类型的新表单模板设置为我新创建的模板。

例如,OOTB ListForm 渲染模板:

<SharePoint:RenderingTemplate ID="ListForm" runat="server">
    <Template>
        <SPAN id='part1'>
            <SharePoint:InformationBar runat="server"/>
            <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator="&nbsp;" runat="server">
                    <Template_RightButtons>
                        <SharePoint:NextPageButton runat="server"/>
                        <SharePoint:SaveButton runat="server"/>
                        <SharePoint:GoBackButton runat="server"/>
                    </Template_RightButtons>
            </wssuc:ToolBar>
            <SharePoint:FormToolBar runat="server"/>
            <TABLE class="ms-formtable" style="margin-top: 8px;" border=0 cellpadding=0 cellspacing=0 width=100%>
            <SharePoint:ChangeContentType runat="server"/>
            <SharePoint:FolderFormFields runat="server"/>
            <SharePoint:ListFieldIterator runat="server" />
            <SharePoint:ApprovalStatus runat="server"/>
            <SharePoint:FormComponent TemplateName="AttachmentRows" runat="server"/>
            </TABLE>
            <table cellpadding=0 cellspacing=0 width=100%><tr><td class="ms-formline"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></td></tr></table>
            <TABLE cellpadding=0 cellspacing=0 width=100% style="padding-top: 7px"><tr><td width=100%>
            <SharePoint:ItemHiddenVersion runat="server"/>
            <SharePoint:ParentInformationField runat="server"/>
            <SharePoint:InitContentType runat="server"/>
            <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbl" RightButtonSeparator="&nbsp;" runat="server">
                    <Template_Buttons>
                        <SharePoint:CreatedModifiedInfo runat="server"/>
                    </Template_Buttons>
                    <Template_RightButtons>
                        <SharePoint:SaveButton runat="server"/>
                        <SharePoint:GoBackButton runat="server"/>
                    </Template_RightButtons>
            </wssuc:ToolBar>
            </td></tr></TABLE>
        </SPAN>
        <SharePoint:AttachmentUpload runat="server"/>
    </Template>
</SharePoint:RenderingTemplate>

我只需要这样一个微小的更改(将内置 ListFieldIterator 更改为自定义 ListFieldIterator):

<SharePoint:RenderingTemplate ID="NewRelatedListItemTemplate" runat="server">
...
  <Balticovo:ListFieldIteratorExtended IncludeFields="RelatedItems" runat="server"/>
...
</SharePoint:RenderingTemplate>

相对于手动(SPD)自定义表单的优点

  • 这样,表单就不是“恒定/静态”的。如果之后将新列表字段添加到列表或内容类型,我的自定义表单将呈现它们(ListFieldIterator 将执行此操作)。

想法 #2

我是否可以在运行时修改现有的 RenderingTemplate?

  1. 我会采用“新表格”模板 (例如,命名为 ListForm 或 it 可能不是默认值 列表表格)与 SPControlTemplateManager.GetTemplateByName("ListForm")
  2. 找到ListIterator控件并添加 财产 TemplateName="ListItemFormFieldsWithRelatedItems"
  3. 渲染此模板并返回它?

简而言之,我想以编程方式动态创建 RenderingTemplate,然后使用此模板来渲染列表的新表单。

优点

  • 我得到了想法1的优势+
  • 这样,即使模板发生变化(从ListForm到CompanyCustomListForm),我也会得到奖金,并且如果我选择稍后更改内容类型的渲染模板,我的自定义表单也不会失去我实现的功能(我可以创建其他功能,而不是试图重新实现这个特定的东西,或者其他第三方功能如果使用自定义表单,则不会覆盖我的功能 - 是松散耦合吗?)。

现在,这(想法#2)可能吗......?

How do I create reusable custom new item form + preferrably, i don't want to tie this form to content type? I want to force render one hidden field (it could be render on the page, but make invisible or render on the page and display) and set field value programmatically (that's why it has to be rendered - to set it's value).

Google has tons of information on how to create custom list form with sharepoint designer, but in my case, i don't want sharepoint designer for the advantages you see below.

What i'm trying to achieve

I want to be able to have a custom newform to create item (i don't want it to be as default). To open this newForm i would use CustomAction in item's ECB menu. In this form, i want to force render one hidden field and set it's value programmatically.

I want to open this form from CustomAction ECB (item's context menu), so i don't want to set this as a default New form template for content type.

<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
      <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
        <New>ListForm</New>
      </FormTemplates>
    </XmlDocument>

Idea #1

I could create custom RenderingTemplate and set Content type's new form template to my newly created template.

For example, OOTB ListForm rendering template:

<SharePoint:RenderingTemplate ID="ListForm" runat="server">
    <Template>
        <SPAN id='part1'>
            <SharePoint:InformationBar runat="server"/>
            <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator=" " runat="server">
                    <Template_RightButtons>
                        <SharePoint:NextPageButton runat="server"/>
                        <SharePoint:SaveButton runat="server"/>
                        <SharePoint:GoBackButton runat="server"/>
                    </Template_RightButtons>
            </wssuc:ToolBar>
            <SharePoint:FormToolBar runat="server"/>
            <TABLE class="ms-formtable" style="margin-top: 8px;" border=0 cellpadding=0 cellspacing=0 width=100%>
            <SharePoint:ChangeContentType runat="server"/>
            <SharePoint:FolderFormFields runat="server"/>
            <SharePoint:ListFieldIterator runat="server" />
            <SharePoint:ApprovalStatus runat="server"/>
            <SharePoint:FormComponent TemplateName="AttachmentRows" runat="server"/>
            </TABLE>
            <table cellpadding=0 cellspacing=0 width=100%><tr><td class="ms-formline"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></td></tr></table>
            <TABLE cellpadding=0 cellspacing=0 width=100% style="padding-top: 7px"><tr><td width=100%>
            <SharePoint:ItemHiddenVersion runat="server"/>
            <SharePoint:ParentInformationField runat="server"/>
            <SharePoint:InitContentType runat="server"/>
            <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbl" RightButtonSeparator=" " runat="server">
                    <Template_Buttons>
                        <SharePoint:CreatedModifiedInfo runat="server"/>
                    </Template_Buttons>
                    <Template_RightButtons>
                        <SharePoint:SaveButton runat="server"/>
                        <SharePoint:GoBackButton runat="server"/>
                    </Template_RightButtons>
            </wssuc:ToolBar>
            </td></tr></TABLE>
        </SPAN>
        <SharePoint:AttachmentUpload runat="server"/>
    </Template>
</SharePoint:RenderingTemplate>

I only need such a minor change (change builtin ListFieldIterator to custom ListFieldIterator):

<SharePoint:RenderingTemplate ID="NewRelatedListItemTemplate" runat="server">
...
  <Balticovo:ListFieldIteratorExtended IncludeFields="RelatedItems" runat="server"/>
...
</SharePoint:RenderingTemplate>

Advantages over manual (SPD) custom forms

  • In this way form is not "constant/static". If new list fields are added to list or content type afterwards, my custom form will render them (the ListFieldIterator will do it).

Idea #2

Could it be that i modify existing RenderingTemplate at runtime?

  1. I would take "new forms" template
    (Named, for example, ListForm or it
    could be other than default
    ListForm) with
    SPControlTemplateManager.GetTemplateByName("ListForm")
  2. Find ListIterator control and add
    property
    TemplateName="ListItemFormFieldsWithRelatedItems"
  3. Render this template and return it?

In short, i would like to create RenderingTemplate programmatically, on-the-fly and then use this template to render list's new form.

Advantages

  • I get the advantage of Idea 1 +
  • This way i would get a bonus even if Template changes (from ListForm to CompanyCustomListForm) and my custom form won't loose my implemented functionality if i choose to change content type's rendering template later on (i can create other features not trying to rembeer to reimplement this particular stuff or other 3rd party features won't override my functionality if they use custom forms - loose coupling is it?).

Now, is this (Idea #2) possible...?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文