Sharepoint WebControl Part I: Reusing OWS Controls in Custom Webpart

(This articles has already been posted in Now I post it again in my own blog.)


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();

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,


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


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

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

Riwut Libinuko
Sr. Cloud Solution Architect

My research interests include distributed robotics, mobile computing and programmable matter.

comments powered by Disqus