Sharepoint WebControl Part I: Reusing OWS Controls in Custom Webpart

English:
(This articles has already been posted in www.msd2d.com. Now I post it again in my own blog.)

Introduction

I am very sure that most of us have been familiar with NewForm.aspx from Sharepoint. That kind of submission form by Sharepoint is a template, which is then populated based on columns definition of a list. So we can have date field, number field etc., depends on columns definition.Surprisingly, that form is made by some JavaScript object. (Figure-1).

Typically creating text field is not a problem but not with date field. Usually we need to allocate special effort for date field – either by creating new custom control or create some JavaScript for the date chooser. However if we could re-use date field object from Sharepoint – then the job can be done faster. So let’s start to figure out how!

rewood ows 2

 

Sharepoint WebControl

One of Microsoft.Sharepoint namespaces that rarely been talked about is Microsoft.Sharepoint.WebControl. Most of the controls in this namespace are derived from System.Web.UI.Control class – which is the base class for the controls in a web page. I will not discuss all controls but our interest is the OWS* control. There are 5 OWS* control in this namespace :

– OWSForm
– OWSControl
o OWSDateField
o OWSNumberField
o OWSSubmitButton

Look at the order! The OWSForm is container for OWSControl – a base class for the other OWS*Field and OWSSubmitButton.
Basically the OWSForm is responsible to create form and OWS*Fields are the input definition. OWSSubmitButton is then create relevant submit action for specific OWSForm.

Reusing OWS Control

Let’s the creating webpart task leaves behind – because many examples around the web. We will focus our discussion on how to use the OWSControl.

First, remember the order !

If you need to use any of OWS*Field control then the container (OWSForm) must exist. You can create OWSForm object by calling

OWSForm owsForm = new OWSForm();
this.Controls.Add(owsForm);

and the OWSDateField objectby calling

OWSDateField dateFld = new OWSDateField();

Second, always submit the OWSForm

When the values need to be submitted – we must make sure that we submit the correct form. The easiest way is using OWSSubmitButton, because it will create relevant submit script for that control.

OWSSubmitButton submitBtn = new OWSSubmitButton();

Third, add everything to the containers

The next step is to add all child components to the container,

owsForm.Controls.Add(dateFld);
owsForm.Controls.Add(submitBtn);

Although you can directly add OWS*Field to the WebPart control – but it’s not suggested as you disobey the hierarchy. You can also layout the components before adding them to the containers.

Last, OWS*Field.Value is not the value you expect

OWS*Field has Value property – but believe me that it is not the value you’re expecting. Since OWS Control render javascript, then everything you need to capture is throughout Page.Request.Params. The Value property is the way you maintain viewing aspect of the users (same with EnableViewState = true in common controls).
So, you can get the correct value using

Page.Request.Params[dateFld.UniqueID];

Since you need to have control’s UniqueID then you MUST PUT THE LAST STATEMENT after all controls has been added to it parent!

dateFld.Value = Page.Request.Params[dateFld.UniqueID]

will create same effect as EnableViewState = true.

rewood ows 1

Summary
To use OWS Control we need OWSForm as containers and follow the hierarchy to build it up in webpart.

About

Riwut Libinuko, experienced Architect. Currently working with Credit Agricole CIB, responsible for SharePoint platform in Asia Pasific. . Certification: SharePoint 2013, SharePoint 2010, SharePoint 2007, ITiL v3

Posted in Microsoft Sharepoint
3 comments on “Sharepoint WebControl Part I: Reusing OWS Controls in Custom Webpart
  1. Sridhar Bommana says:

    redirects me to the webparts maintenance page

  2. Cakriwut says:

    Sridhar Bommana:
    When you start a new webpart project, its better to create simple “Hello World” text webpart. After successfull insertion in Sharepoint webpart pages, you can continue the project with real logic.
    You can also use try … catch block, so you won’t missed the error message.

  3. vikash says:

    hey Sridhar,
    cannu send me whole code of the above webpart i want to use datefiled.
    u can send me code shar.vikash@gmail.com
    thanks in advance

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: