How to load network locations based on network fields (not geometry) (.NET/C#)

Discussion created by ceicher on Oct 10, 2013
Network Analyst, C# ArcObjects

I would like to load network features (e.g. facilities, point barriers).

I cannot get 'load using network fields' to work (I can load based on geometry, but this isn't what I want).

I am using the exact code from (below) from this .NET help topic. I am using 10.2.

How to load data into a network analysis problem

rowsLocated is always coming back as 0. rowsIn shows that there are indeed rows in my input cursor. I have tried on a couple feature classes, but always the same result.

Any ideas what could cause this to not work? I can load from the same feature classes using ArcMap. Works fine.

I'm not doing anything with GP



public void LoadAnalysisObjectsByField(ESRI.ArcGIS.Geodatabase.ITable inputClass,
    string naClassName, ESRI.ArcGIS.NetworkAnalyst.INAContext naContext)
    // Both Initialize and Load take a cursor from the input class
    ESRI.ArcGIS.Geodatabase.ICursor cursor = inputClass.Search(null, false)as
    ESRI.ArcGIS.NetworkAnalyst.INAClassLoader2 naClassLoader = new
    naClassLoader.Initialize(naContext, naClassName, cursor);

    // Store the current set of locator agents, so they can be added back later
    int agentCount = naContext.Locator.LocatorAgentCount;
    var listOfAgents = new System.Collections.Generic.List <
        ESRI.ArcGIS.NetworkAnalyst.INALocatorAgent > ();
    for (int locIndex = 0; locIndex < agentCount; locIndex++)

    // Remove the existing locator agents from the locator
    // This for loop is done in reverse order, because agents are being removed as the loop executes
    for (int locIndex = agentCount - 1; locIndex >= 0; locIndex--)

    // Create and add a fields agent
    var fieldsAgent = new

    // Set the field names appropriately based on input data and NAClass
    var naClass = naContext.NAClasses.get_ItemByName(naClassName)as
    var naFeatureClass = naClass as ESRI.ArcGIS.Geodatabase.IFeatureClass;

    // Check to see if the NAClass is of type NALocation or NALocationRanges
    ESRI.ArcGIS.esriSystem.UID naLocationFeatureUID = new
    naLocationFeatureUID.Value = "esriNetworkAnalyst.NALocationFeature";
    ESRI.ArcGIS.esriSystem.UID naLocationFeatureRangesUID = new
    naLocationFeatureRangesUID.Value = "esriNetworkAnalyst.NALocationRangesFeature";
    if (naFeatureClass.CLSID.Compare(naLocationFeatureUID))
        // The field names listed below are the names used in ArcGIS Network Analyst extension classes to represent NALocations.
        //  These are also the names of fields added by the CalculateLocations geoprocessing tool
        fieldsAgent.OIDFieldName = "SourceOID";
        fieldsAgent.SourceIDFieldName = "SourceID";
        fieldsAgent.PositionFieldName = "PosAlong";
        fieldsAgent.SideFieldName = "SideOfEdge";
    else if (naFeatureClass.CLSID.Compare(naLocationFeatureRangesUID))
        // The location ranges input field must be of type BLOB
        fieldsAgent.LocationRangesFieldName = "Locations";
        var blobField = inputClass.Fields.get_Field(inputClass.FindField
        if (blobField.Type !=
                "Loading location ranges by field requires a blob field");
            return ;
    naContext.Locator.AddLocatorAgent(fieldsAgent as

    // After Loading is complete, the rowsIn and rowsLocated variable can be used to verify
    //  that every row from the input feature class has been loaded into the network analysis class
    int rowsIn = 0;
    int rowsLocated = 0;
    naClassLoader.Load(cursor, null, ref rowsIn, ref rowsLocated);

    // Now remove the custom fields agent and add back the stored agents
    foreach (var agent in listOfAgents)