Thiago Vinicius
Thiago Vinicius

Reputation: 180

Custom Field SharePoint BCS Edit and NewForm

Scenario: I’ve got a source of data coming in through BCS and is represented like it’s always represented out of the box with BCS. I would like to do is to customize the New and EditForms to allow a DropDown in one of my fields.

I've tried: Creating a custom field based on SPFieldChoide (tested in a custom list and it works just fine) and through the BCS config XML file (.bdcm) added the property SPCustomFieldType to the field I want to get customized.

Error: I can open ReadItem/NewForm/EditForm pages and custom field renders just fine yet it throws an error while opening the ReadList page

Error while executing web part: System.NotSupportedException: Method 'GetFieldAttributeValue' is not supported on BiConvenioGrupoChoiceField for external lists.
at Microsoft.SharePoint.SPExternalList.ThrowNotSupportedExceptionForMethod(String sMethodName, Type typeThrowing)
at Microsoft.SharePoint.SPFieldChoice.get_Sortable()
at Microsoft.SharePoint.SPField.AnnotateField(XmlNode fieldRefNode)
at Microsoft.SharePoint.WebPartPages.XsltListViewWebPart.AddInFieldSchema(XmlNodeList fieldRefNodes, SPList list)
at Microsoft.SharePoint.WebPartPages.XsltListViewWebPart.AddInTypeInfoIntoViewXml(XmlNode viewXml)
at Microsoft.SharePoint.WebPartPages.XsltListViewWebPart.ModifyXsltArgumentList(ArgumentClassWrapper argList)
at Microsoft.SharePoint.WebPartPages.DataFormWebPart.PrepareAndPerformTransform(Boolean bDeferExecuteTransform)

Let's walk through the code.

Custom field .cs

class BiConvenioGrupoChoiceField : SPFieldChoice
{
    #region Constructors
    public BiConvenioGrupoChoiceField(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { }
    public BiConvenioGrupoChoiceField(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName) { }
    #endregion

    #region Properties
    public override string TypeDisplayName
    {
        get
        {
            return "BiConvenioGrupoChoiceField";
        }
    }        
    public override BaseFieldControl FieldRenderingControl
    {
        get
        {   
            BaseFieldControl fieldControl = new BiConvenioGrupoChoiceFieldControl();
            fieldControl.FieldName = InternalName;
            return fieldControl;
        }
    }
    #endregion
}

Custom field Control

class BiConvenioGrupoChoiceFieldControl : BaseFieldControl
{
    DropDownList customDropDown;
    protected override string DefaultTemplateName
    {
        get
        {
            return "DropDownRenderingTemplate";
        }
    }
    protected override void CreateChildControls()
    {
        try
        {
            base.CreateChildControls();
            customDropDown = (DropDownList)TemplateContainer.FindControl("customDropDown");
            if (customDropDown != null)
            {
                customDropDown.ID = this.FieldName;
                if (this.ControlMode == SPControlMode.New || this.ControlMode == SPControlMode.Edit)
                {                        
                        customDropDown.Items.Add(new ListItem("Option 0", "0"));
                        customDropDown.Items.Add(new ListItem("Option 1", "1"));
                        customDropDown.Items.Add(new ListItem("Option 2", "2"));
                        customDropDown.Items.Add(new ListItem("Option 9", "9"));
                }
            }
        }
        catch (Exception ex)
        {
            SystemLogger.Logger.Log(ex, LoggingLevel.Fatal);
        }
    }

    public override object Value
    {
        get
        {
            EnsureChildControls();
            return customDropDown.SelectedValue;
        }
        set
        {
            this.EnsureChildControls();
            customDropDown.SelectedValue = (string)ItemFieldValue;
        }
    }
}

BCS.bdcm

<Method Name="Create">
              <Parameters>
                <Parameter Name="returnCCCadastrados" Direction="Return">
                  <TypeDescriptor Name="ReturnCCCadastrados" TypeName="Models.ConvenioBI, CCCadastradosBDC">
                    <TypeDescriptors>
                      <TypeDescriptor Name="Dbico_sq" DefaultDisplayName="Id" IdentifierName="Dbico_sq" TypeName="System.Int32" />
                      <TypeDescriptor Name="Descricao" DefaultDisplayName="Descrição" TypeName="System.String" />
                      <TypeDescriptor Name="CodigoCorporativo" DefaultDisplayName="Código Corporativo" TypeName="System.String" />
                      <TypeDescriptor Name="Login" DefaultDisplayName="Criado Por" TypeName="System.String" />
                      <TypeDescriptor Name="Grupo" DefaultDisplayName="Grupo" TypeName="System.String">
                        <Properties>
                          <Property Name="SPCustomFieldType" Type="System.String">BiConvenioGrupoChoiceField</Property>
                        </Properties>
                      </TypeDescriptor>
                      <TypeDescriptor Name="DtCriacao" DefaultDisplayName="Data Criação" IsCollection="false" TypeName="System.DateTime">
                        <Interpretation>
                          <NormalizeDateTime LobDateTimeMode="UTC" />
                        </Interpretation>
                      </TypeDescriptor>
                      <TypeDescriptor Name="DtAtualizacao" DefaultDisplayName="Data Atualização" IsCollection="false" TypeName="System.DateTime" >
                        <Interpretation>
                          <NormalizeDateTime LobDateTimeMode="UTC" />
                        </Interpretation>
                      </TypeDescriptor>
                    </TypeDescriptors></TypeDescriptor></Parameter>
                <Parameter Name="newCCCadastrados" Direction="In">
                  <TypeDescriptor Name="NewCCCadastrados" TypeName="Models.ConvenioBI, CCCadastradosBDC">
                    <TypeDescriptors>                      
                      <TypeDescriptor Name="Descricao" DefaultDisplayName="Descrição" TypeName="System.String" CreatorField="true" />
                      <TypeDescriptor Name="CodigoCorporativo" DefaultDisplayName="Código Corporativo" TypeName="System.String" CreatorField="true" />
                      <TypeDescriptor TypeName="System.String" Name="Grupo" DefaultDisplayName="Grupo" CreatorField="true" >
                        <Properties>
                          <Property Name="SPCustomFieldType" Type="System.String">BiConvenioGrupoChoiceField</Property>
                        </Properties>
                      </TypeDescriptor>
                    </TypeDescriptors>
                  </TypeDescriptor>
              </Parameter>
              </Parameters>
              <MethodInstances>
                <MethodInstance Name="Create" Type="Creator" ReturnParameterName="returnCCCadastrados" ReturnTypeDescriptorPath="ReturnCCCadastrados" />
              </MethodInstances>
            </Method>

Upvotes: 0

Views: 832

Answers (1)

Thiago Vinicius
Thiago Vinicius

Reputation: 180

I just override the property Sortable in my custom field class.

public override bool Sortable
{
    get
    {
        return false;
    }
}

Works like charm.

Upvotes: 0

Related Questions