Datapolis Process System SDK

Search:

Contents
:
IndexBookmarkPrint
Home > Custom Controls > Creating Control Tutorial

Creating Control Tutorial

 

 

This walkthrough will guide you through creating a custom control. The control consists of textbox and drop down list. Drop down list contains list of data types. Text box allows us to input value. Validation is based on type selected in drop down list.

 

Prerequisites


Before creating a custom control, be sure you have installed all of the necessary components.

 

  1. Open a new Visual Studio project (File | New | Project)
  2. Create a new SharePoint 2013 - empty project.
  3. Name the project appropriately. In this case it might be QuickLaunchForm.CustomFieldControl. Set the other project properties (Location, Solution Name etc.)
  4. Select project to be deployed as farm solution and choose right site for debugging.
  5. Add reference to Datapolis.WorkBox.Common  - it should be available in GAC.
  6. Add a new User Control by right clicking on the project node in Solution Explorer and select Add | New Item | Office/SharePoint | User Control. Name it MyCustomFieldControl.ascx

Develop control


Add following lines to MyCustomFieldControl.ascx to define control's look.

 

<asp:TextBox runat="server" ID="tbMyValue" Text="" />
<asp:DropDownList runat="server" ID="ddlMyType" >
</asp:DropDownList>

 

Edit code behind


Open MyCustomFieldControl.ascx.cs file. Add using statement for Datapolis.WorkBox.Common namespace and change inheritance to WBCustomFieldControlBase.

 

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Datapolis.WorkBox.Common;
  
namespace QuickLauchForm.CustomFieldControl.ControlTemplates.QuickLaunchForm.CustomFieldControl
{
    public partial class MyCustomFieldControl : WBCustomFieldControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

 

Add Types region to the class. It has MyDataType enum for selectable types in drop down list and MyValue struct for storing data. Value of the control will be passed in following format: [type];#[value]

 

#region Types
    // used types
    private enum MyDataType { Integer, String, YesNo }
    // wrapper struct for value
    private struct MyValue
    {
        public MyDataType Type;
        public String Value;
        public MyValue(string rawValue)
        {
            try
            {
                string[] parts = rawValue.Split(new string[] { ";#" },StringSplitOptions.RemoveEmptyEntries);
                Type = (MyDataType)Enum.Parse(typeof(MyDataType), parts[0]);
                if (parts.Length > 1)
                    Value = parts[1];
                else
                    Value = String.Empty;
                }
                catch (Exception)
                {
                    throw new ArgumentException("Value is in wrong format.");
                }
            }
   
        public override string ToString()
        {
            return String.Format("{0};#{1}", Type, Value);
        }
    }
#endregion Types

 

Edit Page_Load event to populate controls with data.

 

#region Control Events
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // fill drop down list
            foreach (string typename in Enum.GetNames(typeof(MyDataType)))
            {
                ddlMyType.Items.Add(new ListItem(typename, typename));
            }
            // set controls values
            tbMyValue.Text = _myValue.Value;
            ddlMyType.SelectedValue = _myValue.Type.ToString();
        }
    }
#endregion Control Events

 

Lastly override base methods of WBCustomFieldConstrolBase.

 

#region WBCustomFieldControlBase
    public override void SetDefaultValue(string value)
    {
        // store default value passed from launch form
        _myValue = new MyValue(value);
        // set controls values
        tbMyValue.Text = _myValue.Value;
        ddlMyType.SelectedValue = _myValue.Type.ToString();
    }
  
    public override string GetValue()
    {
        // get values from form
        _myValue.Type = (MyDataType)Enum.Parse(typeof(MyDataType), ddlMyType.SelectedValue);
        _myValue.Value = tbMyValue.Text;
        // return formated
        return _myValue.ToString();
    }
   
    public override bool Validate(out string validationErrorMsg)
    {
        // validate value by selected type
        validationErrorMsg = String.Empty;
        bool isValid = false;
        MyDataType type = (MyDataType)Enum.Parse(typeof(MyDataType), ddlMyType.SelectedValue);
        switch (type)
        {
            case MyDataType.Integer:
                int intVal;
                if (int.TryParse(tbMyValue.Text, out intVal))
                    isValid = true;
                else
                    // set validation message
                    validationErrorMsg = "Expected integer value.";
                break;
            case MyDataType.String:
                isValid = true;
                break;
            case MyDataType.YesNo:
                bool boolVal;
                if (bool.TryParse(tbMyValue.Text, out boolVal))
                    isValid = true;
                else if (tbMyValue.Text.Equals("yes", StringComparison.InvariantCultureIgnoreCase) ||
                      tbMyValue.Text.Equals("no", StringComparison.InvariantCultureIgnoreCase))
                    isValid = true;
                else
                    // set validation message
                    validationErrorMsg = "Expected boolean or yes/no value.";
                break;
        }
        return isValid;
    }
   
    public override bool IsRequired()
    {
        return false;
    }
#endregion WBCustomFieldControlBase

 

Safe Control Entry


Edit Package.Template.xml by adding Safe Control Entry.

 

Paste the code below

 

<?xml version="1.0" encoding="utf-8"?>
       SolutionId="2ab3374a-7777-4b24-9646-c7616cb10139"
       SharePointProductVersion="15.0">
  <Assemblies>
    <Assembly Location="QuickLaunchForm.CustomFieldControl.dll"
           DeploymentTarget="GlobalAssemblyCache" >
      <SafeControls>
        <SafeControl Assembly="$SharePoint.Project.AssemblyFullName$"
                 Namespace="QuickLaunchForm.CustomFieldControl.ControlTemplates.QuickLaunchForm.CustomFieldControl"
                 TypeName="*"
                 Safe="True"/>
      </SafeControls>
    </Assembly>
  </Assemblies>
</Solution>

 

Deploy


Now you are ready to deploy solution.

 

Next Steps


Optionally, after reading this walkthrough you can see the same custom control code attached in SDK Samples in Download section.