0

How can I customize rendering of a custom field type in SharePoint using server side code?

I searched on the net but I am getting examples with csr technology i.e. JSLink. I want to know how I can do it in another way.

I want my field to have a default value or placeholder when displayed in new form for a list or library.

0

Setting default values for custom field types is only possible in changing the implementation on server side.

Check out this nice MSDN walkthrough regarding the implementation of custom field types.

The sample explains also how to set a default value.

protected override void CreateChildControls()
{
    if (this.Field != null)
    {
        // Make sure inherited child controls are completely rendered.
        base.CreateChildControls();

        // Associate child controls in the .ascx file with the 
        // fields allocated by this control.
        this.ISBNPrefix = (Label)TemplateContainer.FindControl("ISBNPrefix");
        this.textBox = (TextBox)TemplateContainer.FindControl("TextField");
        this.ISBNValueForDisplay = (Label)TemplateContainer.FindControl("ISBNValueForDisplay");

        if (this.ControlMode != SPControlMode.Display)
        {
            if (!this.Page.IsPostBack)
            {
                if (this.ControlMode == SPControlMode.New)
                {
                    // setting default value here!
                    textBox.Text = "0-000-00000-0";
                }
             }
        }
        else
        {                 
            // Assign current value from database to the label control
            ISBNValueForDisplay.Text = (String)this.ItemFieldValue;
        }
    }
}
  • Can you tell me that how can i add this as a watermark ? means the text should disappear as soon as i click on the textbox – Vishwas Goswami Apr 28 '15 at 10:06
  • Please mark as answer when this was helpful ;) What do you mean with watermark? When the user clicks on the field the text should disappear? I don't understand your requirement. – Robin Güldenpfennig Apr 28 '15 at 10:07
  • for example if you this comment box of stack exchange, when you start typing the default text in it gets disappeared. i want this same thing for my custom field type. – Vishwas Goswami Apr 28 '15 at 10:12
  • Ahh OK I understand. This is only possible in conjunction with CSR (Client Side Rendering) in JSLink. When the field is empty (has no value) then just inject some text with JavaScript. When the field is in edit mode, then keep it empty. I didn't implement such a thing but I think it should work in this way. – Robin Güldenpfennig Apr 28 '15 at 11:36
  • how can i add that using CSR ? – Vishwas Goswami Apr 28 '15 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.