Mina Wissa
Mina Wissa

Reputation: 10971

Sharepoint Custom Field Render pattern: loop through Sub Columns of Multiple Column field

I have a Custom Sharepoint field that inherits from SPFieldMultiColumn. the values of the field appear like this:

";#column one value;#column two value;#column three value;#"

I want to display each item in a separate line and remove the

;#

I checked this link http://msdn.microsoft.com/en-us/library/ms411957(office.12).aspx and found that this can be achieved by overriding the render pattern of the field like this

<RenderPattern Name="DisplayPattern" DisplayName="DisplayPattern">
  <Switch>
    <Expr>
      <GetVar Name="FreeForm"/>
    </Expr>
    <Case Value="TRUE">
      <Column/>
    </Case>
    <Default>
      <HTML>
        <![CDATA[ <DIV ALIGN=RIGHT> ]]>
      </HTML>
      <Column/>
      <HTML>
        <![CDATA[ </DIV> ]]>
      </HTML>
    </Default>
  </Switch>
</RenderPattern>

bu this requires that the number of SubColumns to be predefined, in my case it can be any number.

so I need a for loop to loop through each subcolumn in my multiple column field?

how can this be done ? thanks

Upvotes: 1

Views: 2922

Answers (4)

Jeff
Jeff

Reputation: 11

Mina,

I am running out of gas on helping you, here is the full content of my Xml and related class headers for your reference, good luck.

<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">CountryStateCityAddress1Field</Field>
    <Field Name="TypeDisplayName">CountryStateCityAddress1Field</Field>
    <Field Name="TypeShortDescription">CountryStateCityAddress1Field</Field>
    <Field Name="ParentType">MultiColumn</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="FieldTypeClass">c0d4e972-cae4-4320-b13b-83ed0bf8cedc</Field>
    <RenderPattern Name="DisplayPattern">
      <Switch>
        <Expr>
          <Column />
        </Expr>
        <Case Value="" />
        <Default>
          <Column SubColumnNumber="0" HTMLEncode="TRUE" />
          <HTML><![CDATA[<br/>]]></HTML>
          <Column SubColumnNumber="1" HTMLEncode="TRUE" />
          <HTML><![CDATA[<br/>]]></HTML>
          <Column SubColumnNumber="2" HTMLEncode="TRUE" />
          <HTML><![CDATA[<br/>]]></HTML>
        </Default>
      </Switch>
    </RenderPattern>
  </FieldType>
  <FieldType>
    <Field Name="TypeName">EMailFieldTypeField</Field>
    <Field Name="TypeDisplayName">EMailFieldTypeField</Field>
    <Field Name="TypeShortDescription">EMailFieldTypeField</Field>
    <Field Name="ParentType">MultiColumn</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="FieldTypeClass">0e8a370a-0388-4b78-9e5c-ffbb8a481391</Field>
    <RenderPattern Name="DisplayPattern">
      <Switch>
        <Expr>
          <Column />
        </Expr>
        <Case Value="" />
        <Default>
          <Column SubColumnNumber="0" HTMLEncode="TRUE" />
          <HTML><![CDATA[<br/>]]></HTML>
          <Column SubColumnNumber="1" HTMLEncode="TRUE" />
          <HTML><![CDATA[<br/>]]></HTML>
          <Column SubColumnNumber="2" HTMLEncode="TRUE" />
          <HTML><![CDATA[<br/>]]></HTML>
        </Default>
      </Switch>
    </RenderPattern>
  </FieldType>
</FieldTypes>

Headers:

namespace CustomFieldTypeAddress1
{
    [CLSCompliant(false)]
    [Guid("0e8a370a-0388-4b78-9e5c-ffbb8a481391")]
    public class EMailFieldTypeField : SPFieldMultiColumn 
    ...

namespace CustomFieldTypeAddress1
{
    [CLSCompliant(false)]
    [Guid("dc20d765-2d36-4396-83aa-f063166d8fcc")]
    public class EMailFieldTypeFieldControl : BaseFieldControl  
    ...

Upvotes: 1

GavrielSl
GavrielSl

Reputation: 56

whan SPFieldMultiColumn object use the method override BaseFieldControl FieldRenderingControl it load the object which inherit from the BaseFieldControl,

So, the object which inherit the BaseFieldControl has a method that call

protected override void RenderFieldForDisplay(HtmlTextWriter output){}

in this method you can render you controls that fits you.

You can use the example below

namespace SharePointTestApplication
{
    public class UserChoiceColumn:SPFieldMultiColumn
    {
        public UserChoiceColumn(SPFieldCollection fields, string fname)
            : base(fields, fname) { }

        public UserChoiceColumn(SPFieldCollection fields, string tname, string dname)
            : base(fields, tname, dname) { }

        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl ctr = new UserChoiceColumnControlType();
                ctr.FieldName = this.InternalName;
                return ctr;
            }
        }

        public override string GetFieldValueAsHtml(object value)
        {

            SPFieldMultiColumnValue mcv = new SPFieldMultiColumnValue(value.ToString());

            return string.Format("{0} , {1}",mcv[0],mcv[1]);
        }


    }

    public class UserChoiceColumnControlType : BaseFieldControl
    {
        #region Protected Members 

        protected TextBox TextBox1;
        protected TextBox TextBox2;

        #endregion

        protected override string DefaultTemplateName
        {
            get
            {
                return "UserChoiceColumnTemplate";
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            TextBox1 = (TextBox)this.TemplateContainer.FindControl("TextBox1");
            TextBox2 = (TextBox)this.TemplateContainer.FindControl("TextBox2");
        }

        public override object Value
        {
            get
            {
                this.EnsureChildControls();
                SPFieldMultiColumnValue mcv = new SPFieldMultiColumnValue(2);
                mcv[0] = TextBox1.Text;
                mcv[1] = TextBox2.Text;
                return mcv;
            }
            set
            {
                this.EnsureChildControls();
                SPFieldMultiColumnValue mcv = (SPFieldMultiColumnValue)this.ItemFieldValue;
                TextBox1.Text = mcv[0];
                TextBox2.Text = mcv[1];
            }
        }

        **protected override void RenderFieldForDisplay(HtmlTextWriter output)
        {
            output.Write("hello world");
           // base.RenderFieldForDisplay(output);
        }**

    }
}

Upvotes: 0

Jeff
Jeff

Reputation: 1

I got the same problem which was caused by ParentType in the Xml file

Before fixing:

<Field Name="ParentType">Text</Field>"

After fixing it:

<Field Name="ParentType">MultiColumn</Field>"

Please refer to http://msdn.microsoft.com/en-us/magazine/dd727508.aspx.

Upvotes: 0

Nat
Nat

Reputation: 14305

You could create a custom field control. See here and here for starting info on how to do this.

This gives you great flexibility in displaying the field, however it does involve more code.

Upvotes: 0

Related Questions