Ashok Raja's Blog
Extending SharePoint Dynamic Forms
by Ashok Raja T 20. August 2012 10:13

Links to other posts in Dynamic Forms Series
1. Introduction to SharePoint Dynamic Forms
2. List Based Rendering of Share Point Dynamic Forms
3. Template Based Rendering of Share Point Dynamic Forms
4. Configuring and Extending Share Point Dynamic Forms (This Post)
5. XML Specification for Template based rendering
6. Project @ CodePlex

 

This post explains the additional configuration options available in SharePoint Dynamic Form, its XML template structure and options of extending through code.

Display Settings

The display settings are not mandatory. If no information (blank) is provided, the default values will be used while generating the form. The below is a screen shot with some sample data.

image

Display Settings Configuration

Option

Particulars

Default Value ( If Blank )

Save Text

Caption for Save Button

Save

Cancel Text

Caption for Cancel Button

Cancel

Save Url

The URL to which the page is to be re-directed after the data is successfully submitted. Also accepts the below URL tokens


~/ redirects to home page of site


~~/ redirects to site collection root


~~~/ redirects to web application root


Redirects to same page

Cancel Url

The URL to which the page is to be re-directed if cancel button is clicked.

Redirects to same page

Save Size

The size of Save Button in pixels (No need to specify px)

125

Cancel Size

The size of Cancel Button in pixels (No need to specify px)

125

Additional Options

Option

Particulars

Hide Cancel Button

Hides the Cancel button in the generated form.

Close Popup

If the generated form is displayed in a Popup, select this option to close the popup after the process is completed.

Show Form Header

Provides additional title for the generated form. This can be used if you don’t want to use the default web part title.

Extending via Code

This option is exclusively for developers to control the data submission. SharePoint Dynamic Forms exposes two virtual methods which can be overridden to provide additional functionality.

1. OnItemAdding

This method is triggered after data is populated to SPListItem class but before data is submitted to the list.

2. OnItemAdded

This method is triggered after the data is successfully submitted and saved in the SharePoint List.

Steps

1. Download SFS.SharePoint.DynamicForms.wsp from CodePlex

2. Deploy the WSP

3. Add reference to SFS.SharePoint.DynamicForms.dll (Will be inside the bin folder of Web Application to which the WSP is deployed)

4. Open the Visual Studio and create a new WebPart Project

5. Inherit that webpart from class “SFS.SharePoint.DynamicForms.BaseDynamicWebpart”

6. To inject custom value to the user submitted data or to provide data for a field which is not exposed in the data entry screen, override OnItemAdding method.

7. User submitted data will be available in the item parameter. If any data has to be manipulated, reassign value of appropriate columns with new value.

8. Build the project and deploy the WSP and attach this WebPart (Not the Generic New Form webpart that comes along with Share Point Dynamic Forms) to a page and provide necessary configuration settings.

9. If for some reason, data submission has to be blocked, let’s say, you have validated the user submitted data with your business layer and if it does not adhere to the expectation, then this operation can be cancelled by setting e.Cancel=true to the property exposed by DynamicFormEventArgs. This also contains an option to provide custom error message to user. This can be achieved by assigning you custom message to e.CancelMessage=”some error message”

Code Samples

Sample 1

In this sample a custom text is appended to title column

 namespace SFS.Intranet.WebApp.WebParts.GenericForm
 {
     [ToolboxItemAttribute(false)]
     public class GenericForm : SFS.SharePoint.DynamicForms.BaseDynamicWebpart
     {
         public override void OnItemAdding(SPListItem item, SharePoint.DynamicForms.DynamicFormEventArgs e)
         {
             //Your Business Logic to manipulate field values goes here….
 
             item["Title"] = item["Title"].ToString() + " Appended through code";
         }
     }
 }

Sample 2

Cancel form submission with custom error message

 namespace SFS.Intranet.WebApp.WebParts.GenericForm
 {
     [ToolboxItemAttribute(false)]
     public class GenericForm : SFS.SharePoint.DynamicForms.BaseDynamicWebpart
     {
         public override void OnItemAdding(SPListItem item, SharePoint.DynamicForms.DynamicFormEventArgs e)
         {
             if (item["Title"].ToString().Contains("test")) // Your business logic goes here to validate the data
             {
                 e.Cancel = true;
 
                 e.CancelMessage = "You are not supposed to use the word test in title.";
             }
 
             else
             {
                 item["Title"] = item["Title"].ToString() + " Appended through code";
             }
         }
     }
 }

Sample 3

Options available after data is submitted

 namespace SFS.Intranet.WebApp.WebParts.GenericForm
 {
     [ToolboxItemAttribute(false)]
     public class GenericForm : SFS.SharePoint.DynamicForms.BaseDynamicWebpart
     {
         public override void OnItemAdded(SPListItem item)
         {
             // Your business logic goes here to use submitted values. Page redirection happens only after this method is processed. To redirect to a specific page, rather than specifying here, change the Save URL in display settings.
         }
     }
 }

Screen Shot for Cancel Operation

image

blog comments powered by Disqus