How to set a domain for a field

3759
3
07-19-2011 02:46 PM
DejanMitrovic
New Contributor II
I am working on a code that programmatically creates a file geodb schema. I can create domains and tables using appropriate methods, but I cannot find any sample how to set a domain for a field. According to the geodb XSD, the Domain element should be of type Domain, but if I insert an XML string with a domain definition (which is ok as I can use it to create a domain) I get an XML exception and CreateTable method fails.

Could you please let me know how to set a field domain?

Thanks,
Dejan
0 Kudos
3 Replies
PaulAustin
Occasional Contributor
I would also like to see an example.

I have tried to include a copy of the full Domain definition in the Field that I want to attach the domain to.

Here is the XML table definition that I create the table using (no errors returned).

<?xml version="1.0" encoding="UTF-8"?>
<esri:DataElement xsi:type="esri:DETable" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:esri="http://www.esri.com/schemas/ArcGIS/10.1">
  <CatalogPath>\ADDRESS_RANGE</CatalogPath>
  <Name>ADDRESS_RANGE</Name>
  <DatasetType>esriDTTable</DatasetType>
  <DSID>1</DSID>
  <Versioned>false</Versioned>
  <CanVersion>false</CanVersion>
  <ConfigurationKeyword></ConfigurationKeyword>
  <HasOID>true</HasOID>
  <OIDFieldName>OBJECTID</OIDFieldName>
  <Fields xsi:type="esri:Fields">
    <FieldArray xsi:type="esri:ArrayOfField">
      <Field xsi:type="esri:Field">
        <Name>OBJECTID</Name>
        <Type>esriFieldTypeOID</Type>
        <IsNullable>false</IsNullable>
        <Length>4</Length>
        <Precision>0</Precision>
        <Scale>0</Scale>
        <Required>true</Required>
        <Editable>false</Editable>
        <AliasName>OBJECTID</AliasName>
        <ModelName>OBJECTID</ModelName>
      </Field>
      <Field xsi:type="esri:Field">
        <Name>HOUSE_NUMBER_STRUCTURE_CODE_ID</Name>
        <Type>esriFieldTypeSmallInteger</Type>
        <IsNullable>true</IsNullable>
        <Length>2</Length>
        <Precision>0</Precision>
        <Scale>0</Scale>
        <DomainFixed>true</DomainFixed>
        <AliasName>HOUSE_NUMBER_STRUCTURE_CODE_ID</AliasName>
        <ModelName>HOUSE_NUMBER_STRUCTURE_CODE_ID</ModelName>
        <Domain xsi:type="esri:CodedValueDomain">
          <DomainName>HOUSE_NUMBER_STRUCTURE_CODE</DomainName>
          <FieldType>esriFieldTypeSmallInteger</FieldType>
          <MergePolicy>esriMPTDefaultValue</MergePolicy>
          <SplitPolicy>esriSPTDuplicate</SplitPolicy>
          <Description></Description>
          <Owner></Owner>
          <CodedValues xsi:type="esri:ArrayOfCodedValue">
            <CodedValue xsi:type="esri:CodedValue">
              <Name>Center</Name>
              <Code xsi:type="xs:short">1</Code>
            </CodedValue>
            <CodedValue xsi:type="esri:CodedValue">
              <Name>Even</Name>
              <Code xsi:type="xs:short">2</Code>
            </CodedValue>
            <CodedValue xsi:type="esri:CodedValue">
              <Name>Hypothetical</Name>
              <Code xsi:type="xs:short">3</Code>
            </CodedValue>
            <CodedValue xsi:type="esri:CodedValue">
              <Name>None</Name>
              <Code xsi:type="xs:short">4</Code>
            </CodedValue>
            <CodedValue xsi:type="esri:CodedValue">
              <Name>Odd</Name>
              <Code xsi:type="xs:short">5</Code>
            </CodedValue>
            <CodedValue xsi:type="esri:CodedValue">
              <Name>Special</Name>
              <Code xsi:type="xs:short">6</Code>
            </CodedValue>
          </CodedValues>
        </Domain>
      </Field>
    </FieldArray>
  </Fields>
  <Indexes xsi:type="esri:Indexes">
    <IndexArray xsi:type="esri:ArrayOfIndex">
      <Index xsi:type="esri:Index">
        <Name>FDO_OBJECTID</Name>
        <IsUnique>true</IsUnique>
        <IsAscending>true</IsAscending>
        <Fields xsi:type="esri:Fields">
          <FieldArray xsi:type="esri:ArrayOfField">
            <Field xsi:type="esri:Field">
              <Name>OBJECTID</Name>
              <Type>esriFieldTypeOID</Type>
              <IsNullable>false</IsNullable>
              <Length>4</Length>
              <Precision>0</Precision>
              <Scale>0</Scale>
              <Required>true</Required>
              <Editable>false</Editable>
              <AliasName>OBJECTID</AliasName>
              <ModelName>OBJECTID</ModelName>
            </Field>
          </FieldArray>
        </Fields>
      </Index>
    </IndexArray>
  </Indexes>
  <CLSID>{7A566981-C114-11D2-8A28-006097AFF44E}</CLSID>
  <EXTCLSID></EXTCLSID>
  <RelationshipClassNames xsi:type="esri:Names"></RelationshipClassNames>
  <AliasName>ADDRESS_RANGE</AliasName>
  <ModelName></ModelName>
  <HasGlobalID>false</HasGlobalID>
  <GlobalIDFieldName></GlobalIDFieldName>
  <RasterFieldName></RasterFieldName>
  <ExtensionProperties xsi:type="esri:PropertySet">
    <PropertyArray xsi:type="esri:ArrayOfPropertySetProperty"></PropertyArray>
  </ExtensionProperties>
  <ControllerMemberships xsi:type="esri:ArrayOfControllerMembership"></ControllerMemberships>
</esri:DataElement>


When I get the table definition back it is missing the  xsi:type="esri:CodedValueDomain" attribute on the domain for the field. This then causes it not to be shown when you look at the field's domain in ArcCatalog. If you try and select the domain it tells you there is already an association with that domain.

<?xml version="1.0" encoding="UTF-8"?>
<esri:DataElement xmlns:esri="http://www.esri.com/schemas/ArcGIS/10.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="esri:DETable">
  <CatalogPath>\ADDRESS_RANGE</CatalogPath>
  <Name>ADDRESS_RANGE</Name>
  <DatasetType>esriDTTable</DatasetType>
  <DSID>22</DSID>
  <Versioned>false</Versioned>
  <CanVersion>false</CanVersion>
  <ConfigurationKeyword/>
  <HasOID>true</HasOID>
  <OIDFieldName>OBJECTID</OIDFieldName>
  <Fields xsi:type="esri:Fields">
    <FieldArray xsi:type="esri:ArrayOfField">
      <Field xsi:type="esri:Field">
        <Name>OBJECTID</Name>
        <Type>esriFieldTypeOID</Type>
        <IsNullable>false</IsNullable>
        <Length>4</Length>
        <Precision>0</Precision>
        <Scale>0</Scale>
        <Required>true</Required>
        <Editable>false</Editable>
        <AliasName>OBJECTID</AliasName>
        <ModelName>OBJECTID</ModelName>
      </Field>
      <Field xsi:type="esri:Field">
        <Name>HOUSE_NUMBER_STRUCTURE_CODE_ID</Name>
        <Type>esriFieldTypeSmallInteger</Type>
        <IsNullable>true</IsNullable>
        <Length>2</Length>
        <Precision>0</Precision>
        <Scale>0</Scale>
        <AliasName>HOUSE_NUMBER_STRUCTURE_CODE_ID</AliasName>
        <ModelName>HOUSE_NUMBER_STRUCTURE_CODE_ID</ModelName>
        <Domain>
          <DomainName>HOUSE_NUMBER_STRUCTURE_CODE</DomainName>
          <FieldType>esriFieldTypeSmallInteger</FieldType>
          <MergePolicy>esriMPTDefaultValue</MergePolicy>
          <SplitPolicy>esriSPTDuplicate</SplitPolicy>
          <Description/>
          <Owner/>
          <CodedValues xsi:type="esri:ArrayOfCodedValue">
    <CodedValue xsi:type="esri:CodedValue">
      <Name>Center</Name>
      <Code xsi:type="xs:short">1</Code>
    </CodedValue>
    <CodedValue xsi:type="esri:CodedValue">
      <Name>Even</Name>
      <Code xsi:type="xs:short">2</Code>
    </CodedValue>
    <CodedValue xsi:type="esri:CodedValue">
      <Name>Hypothetical</Name>
      <Code xsi:type="xs:short">3</Code>
    </CodedValue>
    <CodedValue xsi:type="esri:CodedValue">
      <Name>None</Name>
      <Code xsi:type="xs:short">4</Code>
    </CodedValue>
    <CodedValue xsi:type="esri:CodedValue">
      <Name>Odd</Name>
      <Code xsi:type="xs:short">5</Code>
    </CodedValue>
    <CodedValue xsi:type="esri:CodedValue">
      <Name>Special</Name>
      <Code xsi:type="xs:short">6</Code>
    </CodedValue>
  </CodedValues>
        </Domain>
      </Field>
    </FieldArray>
  </Fields>
  <Indexes xsi:type="esri:Indexes">
    <IndexArray xsi:type="esri:ArrayOfIndex">
      <Index xsi:type="esri:Index">
        <Name>FDO_OBJECTID</Name>
        <IsUnique>true</IsUnique>
        <IsAscending>true</IsAscending>
        <Fields xsi:type="esri:Fields">
          <FieldArray xsi:type="esri:ArrayOfField">
            <Field xsi:type="esri:Field">
              <Name>OBJECTID</Name>
              <Type>esriFieldTypeOID</Type>
              <IsNullable>false</IsNullable>
              <Length>4</Length>
              <Precision>0</Precision>
              <Scale>0</Scale>
              <Required>true</Required>
              <Editable>false</Editable>
              <AliasName>OBJECTID</AliasName>
              <ModelName>OBJECTID</ModelName>
            </Field>
          </FieldArray>
        </Fields>
      </Index>
    </IndexArray>
  </Indexes>
  <CLSID>{7A566981-C114-11D2-8A28-006097AFF44E}</CLSID>
  <EXTCLSID/>
  <RelationshipClassNames xsi:type="esri:Names"/>
  <AliasName>ADDRESS_RANGE</AliasName>
  <ModelName/>
  <HasGlobalID>false</HasGlobalID>
  <GlobalIDFieldName/>
  <RasterFieldName/>
  <ExtensionProperties xsi:type="esri:PropertySet">
    <PropertyArray xsi:type="esri:ArrayOfPropertySetProperty"/>
  </ExtensionProperties>
  <ControllerMemberships xsi:type="esri:ArrayOfControllerMembership"/>
</esri:DataElement>
0 Kudos
LanceShipman
Esri Regular Contributor
An example was included in the Domains sample in the file geodatabase 1.1 release. You assign a domain to a field via table.AlterField. It is not currently possible to add a domain on table creation.

I've added an enhancement (NIM072315) to allow the association of a domain on AddField and on CreateTable.
0 Kudos
LanceShipman
Esri Regular Contributor
You were not able to add a domain to fields on a table created with the File Geodatabase API (NIM074135). This has been fixed in the 1.2 release, which will be available this week. With alter field working correctly you can add a domain to a field in one step and the API is operating as designed. NIM072315 is an enhancement. With the current design you can add domains to fields. NIM072315 just changes the way you have to code this.

NIM074135 slipped through because our test used an existing feature class. We have corrected this test to create the test table.
0 Kudos