Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

add xmlformview control on sharepoint page programmatically

add xmlformview control on sharepoint page programmatically

In the below code snippet I am adding an xmlformview control on an SharePoint 2010 application page and using the code behind for that page to load the xml form and to set the properties for the xmlformview control.

ApplicationPage Aspx page -

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”MyFormPage.aspx.cs” Inherits=”MyFormPage” EnableSessionState=”True” %>
<%@ Register tagprefix=”fv” namespace=”Microsoft.Office.InfoPath.Server.Controls” assembly=”Microsoft.Office.InfoPath.Server, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c” %>
<%@ Register tagprefix=”Server” namespace=”Microsoft.Office.InfoPath” assembly=”Microsoft.Office.InfoPath, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c” %>
http://www.w3.org/1999/xhtml”>

FormViews







Please Note that to correctly display the forms, XmlFormView needs to know the location of the form to display, the location of the form template used to render the form, and finally, the location for saved forms to be stored. The properties XmlLocation, XsnLocation, and SaveLocation are used to pass this information to
the XmlFormView object.

Now, Lets look at the Code behind for the application Page. The code behind below uses the On_Load method to display the actual Infopath form. Before loading the form the Onload method reads the request parameter i.e. “action” and decides weather an existing form should be loaded using the XmlLocation parameter or the template should be assigned by the XsnLocation ( if action== new).

Code Behind -

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.InfoPath.Server.Controls;
namespace formviewdemo
{
public partial class MyFormPage: LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
string action = Request.Params["action"];
if (action == “new”)
{
// Set the template location for new forms
String templateLib = “FormServerTemplates”;
String xsnName = “template.xsn”; ->> form template Name
formView.XsnLocation = String.Format(“{0}/{1}/{2}”, SPContext.Current.Web.Url, templateLib, xsnName);
}
else
{
// Set the XML location for an existing form ( in the sharepoint library)
String lib = “TestForms”;
String name = “example.xml”; ->> name of the infopathform
formView.XmlLocation = String.Format(“{0}/{1}/{2}”, SPContext.Current.Web.Url, lib, name);
}}
}}

No comments:

Post a Comment