Monday, April 9, 2012

How to show related item on InfoPath task form

Task form is displayed using WrkTaskIP.aspx page from TEMPLATE\LAYOUTS folder.

  1. Create copy of WrkTaskIP.aspx into your project named CustomWrkTaskIP.aspx.
  2. Register forms.css style:
    <asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <SharePoint:CssRegistration runat="server" Name="forms.css" EnableCssTheming="true" />
    </asp:Content>

  3. Add the following code into page:
    <div>
    <h3>Related item</h3>
    <table width="100%" class="ms-formtable" border="0" cellspacing="0" cellpadding="0">
    <SharePoint:ListFieldIterator ID="ItemFieldIterator" runat="server" ControlMode="Display" />
    </table>
    </div>

  4. Add codebehind to your page:
    public class CustomWrkTaskPage : WrkTaskIPPage
    {
    protected ListFieldIterator ItemFieldIterator;

    protected override void OnLoad(EventArgs ea)
    {
    base.OnLoad(ea);

    Guid workflowInstanceId = new Guid((string)m_task[SPBuiltInFieldId.WorkflowInstanceID]);
    SPWorkflow workflow = new SPWorkflow(Web, workflowInstanceId);


    ItemFieldIterator.ListId = workflow.ParentList.ID;
    ItemFieldIterator.ItemId = workflow.ParentItem.ID;
    }
    }

  5. Set DisplayFormUrl and EditFormUrl properties of all task content types to your CustomWrkTaskIP.aspx:
    foreach (SPContentType taskContentType in taskList.ContentTypes)
    {
    if (!taskContentType.Id.IsChildOf(SPBuiltInContentTypeId.WorkflowTask))
    continue;

    taskContentType.DisplayFormUrl = "_layouts/My/CustomWrkTaskIP.aspx";
    taskContentType.EditFormUrl = "_layouts/My/CustomWrkTaskIP.aspx";
    taskContentType.Update();
    }

No comments:

Post a Comment