Can you access geoprocessing history programmatically using the ArcGIS Pro SDK?

164
2
12-08-2020 12:08 PM
MikeDavlantes
New Contributor II

I'm writing an ArcGIS Pro add-in and would like to view items in the geoprocessing history. I see in the SDK docs there is a HistoryProjectItem class that offers pretty much exactly what I need. I can access it while debugging in Visual Studio, but as you can see in the docs it's a public sealed class and has a note "public acess Deprecated at 2.1." I'm new to C# but it seems this means I cannot access the HistoryProjectItem class in my add-in code? If that is the case, was this deprecated in favor of another method of accessing the geoprocessing history using the SDK?

I am aware of the option to write the history to a log file, but requiring my users to have that option enabled and then parsing the log file every time that my add-in gets opened is not optimal.

2 Replies
KirkKuykendall1
Occasional Contributor II

History gets saved in the aprx file (which is a zip file).  The xml can be deserialized into a CIMGISProject.  I see no mention of deprecation for accessing the history items in the project.

 

 

public static void ListHistory()
{
    // this can be run in a console app (or within a Pro add-in)
    CIMGISProject project = GetProject(@"D:\tests\topologies\topotest1.aprx");
    foreach(CIMProjectItem hist in project.ProjectItems
                .Where(itm => itm.ItemType == "GPHistory"))
    {
        Debug.Print($"+++++++++++++++++++++++++++");
        Debug.Print($"{hist.Name}");
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(hist.PropertiesXML);
        //it sure would be nice if Pro SDK had things like MdProcess class in ArcObjects
        //https://desktop.arcgis.com/en/arcobjects/latest/net/webframe.htm#MdProcess.htm
        var json = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented);
        Debug.Print(json);
    }
}
static CIMGISProject GetProject(string aprxPath)
{
    //aprx files are actually zip files
    //https://www.nuget.org/packages/SharpZipLib
    using (var zipFile = new ZipFile(aprxPath))
    {
        var entry = zipFile.GetEntry("GISProject.xml");
        using (var stream = zipFile.GetInputStream(entry))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                var xml = reader.ReadToEnd();
                //deserialize the xml from the aprx file to hydrate a CIMGISProject
                return ArcGIS.Core.CIM.CIMGISProject.FromXml(xml);
            }
        };
    };
}
MikeDavlantes
New Contributor II

Thanks for taking the time to write this out, Kirk! I was aware that aprx files are zips and had found the GP history as XML there, but it seems that the history is only written out when the document is saved? Ultimately I'm trying to make something like "re-run one of these GP tools you just ran:" and have a list of operations you have done recently, regardless of whether you've saved or not. Kind of like the existing history tab within Arc, but I'd need to be able to modify the items. It seems ArcPro has an internal stack of these operations (obviously it must because they get written out on save), but accessing HistoryProjectItem directly might be out of the question given what's written in the docs. Do you know of any way to get at GP history items before a save is made and they're written out to the file?

Reply
0 Kudos