Render custom form / alter existing rendering template at runtime.
Posted
by Janis Veinbergs
on Stack Overflow
See other posts from Stack Overflow
or by Janis Veinbergs
Published on 2010-03-19T12:09:31Z
Indexed on
2010/03/19
12:11 UTC
Read the original article
Hit count: 932
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:
<SharePoint:RenderingTemplate ID="NewRelatedListItemTemplate" runat="server">
...
<SharePoint:ListFieldIterator TemplateName="ListItemFormFieldsWithRelatedItems" runat="server" />
..
</SharePoint:RenderingTemplate>
<SharePoint:RenderingTemplate ID="ListItemFormFieldsWithRelatedItems" runat="server">
<Template>
<Balticovo:ListFieldIteratorExtended IncludeFields="RelatedItems" runat="server"/>
</Template>
</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?
- I would take "new forms" template
(Named, for example, ListForm or it
could be other than default
ListForm) with
SPControlTemplateManager.GetTemplateByName("ListForm")
- Find ListIterator control and add
property
TemplateName="ListItemFormFieldsWithRelatedItems"
- 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...?
© Stack Overflow or respective owner