Accessing Coded Domain List

1441
6
Jump to solution
04-13-2022 04:08 PM
JimmyBowden
Occasional Contributor II

I'm looking for a way to get a coded domain list based on it's name so that I can use on as combo box items in a custom dockpane or control.  Am I missing the easy way to do this? There doesn't seem to be a way to do this from a workspace. There is a sample that shows how to query them directly from gdb_items in the database.  I've also been able to access them through a feature but that isn't a very direct method.  I'm currently hard coding the values and descriptions as shown in the image.  I would rather not do that for obvious reasons.

JimmyBowden_0-1649891213013.png

 

0 Kudos
1 Solution

Accepted Solutions
RichRuh
Esri Regular Contributor

Hi Jimmy,

 

You can get a list of the domains in a geodatabase with the Geodatabase.GetDomains method. This returns an IReadOnlyList which you can search using a Linq query.

IReadOnlyList<Domain> domains = myGeodatabase.GetDomains();
Domain myDomain = domains.First(x => x.GetName() == myDomainName);

I hope this helps,

--Rich

View solution in original post

0 Kudos
6 Replies
RichRuh
Esri Regular Contributor

Hi Jimmy,

 

You can get a list of the domains in a geodatabase with the Geodatabase.GetDomains method. This returns an IReadOnlyList which you can search using a Linq query.

IReadOnlyList<Domain> domains = myGeodatabase.GetDomains();
Domain myDomain = domains.First(x => x.GetName() == myDomainName);

I hope this helps,

--Rich

0 Kudos
JimmyBowden
Occasional Contributor II

Wow I'm feeling very dense at the moment.  I thought I looked on the geodatabase object, I guess I just missed it.  Thanks for the prompt response @RichRuh 

KarthikAditya
New Contributor III

Hi Rich,

How can the domains be accessed if the layers are loaded from a feature server and do not exist in a geodatabase? I searched the SDK guide here but could not find any method that can do this.

0 Kudos
RichRuh
Esri Regular Contributor

Hi Karthik,

You can create a Geodatabase object from a feature service by creating a ServiceConnectionProperties object.  You can then instantiate a Geodatabase using the Geodatabase(ServiceConnectionProperties) constructor.

Once you have a Geodatabase you can proceed as described above.

I hope this helps,

--Rich

0 Kudos
KarthikAditya
New Contributor III

Thank you, Rich. That worked!

0 Kudos
JimmyBowden
Occasional Contributor II

In case someone stumbles on this post and needs a bit more here's an extension method to return the values of an Integer based coded value domain from a geodatabase as a dictionary. 

  public static Dictionary<int, string> GetDomainValuesInt(this Geodatabase geodatabase, string name)
        {
            Dictionary<int, string> retval = new Dictionary<int, string>();
            IReadOnlyList<Domain> domains = geodatabase.GetDomains();
            Domain namedomain = domains.FirstOrDefault(a => a.GetName().Equals(name, StringComparison.CurrentCultureIgnoreCase));
            if (namedomain != null && namedomain is CodedValueDomain)
            {
                //need to cast the domain to CodedValueDomain to get the Value Pairs
                CodedValueDomain cvd = namedomain as CodedValueDomain;
                SortedList<object, string> valuePairs = cvd.GetCodedValuePairs();
                foreach (var item in valuePairs)
                {
                    if (int.TryParse(item.Key.ToString(), out int i))
                    {
                        retval.Add(i, item.Value);
                    }
                }
            }
            return retval;
        }