Get Addin info Pro 3.0

1897
17
Jump to solution
07-12-2022 10:47 AM
AbelPerez
Occasional Contributor III

At Pro 2.9 there was this great helper class that got info for your own addin. https://github.com/Esri/arcgis-pro-sdk-community-samples/tree/master/Content/AddInInfoManager

It bombs at 3.0 and I have not been able to locate an alternative API or other form of getting the info programmatically.

I tweaked the function and it turns out the the code is returning zero guid attributes:

        public static string GetAddInGuid()
        {
            // get the GUID that identifies the current add-in
            Assembly exAssembly = Assembly.GetExecutingAssembly();
            //GuidAttribute gAttribute = (GuidAttribute)exAssembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
            object[] custAtts = exAssembly.GetCustomAttributes(typeof(GuidAttribute), true);
            if (custAtts.Length == 0)
            {
                throw new Exception("No custom attributes.");
            }
            GuidAttribute gAttribute = (GuidAttribute)custAtts[0];

            // guid={903288ab-2c9c-4e49-8a3d-0321f68425c1}
            return string.Format("{{{0}}}", gAttribute.Value);
        }

 

Anyone know an alternative to programmatically get info for your own addin or just the GUID?

0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I tried this method on both 2.x migrated projects and 3.0 created addin projects.  There might be a better way to do this but for the time being this should work:

    public static string GetAddInGuid()
    {
      // get the GUID that identifies the current add-in
      Assembly exAssembly = Assembly.GetExecutingAssembly();
      var dirs = exAssembly.Location.Split(System.IO.Path.DirectorySeparatorChar);
      if (dirs.Length > 2
          && Guid.TryParse(dirs[dirs.Length - 2], out Guid assemblyGuid))
      {
        return assemblyGuid.ToString();
      }
      throw new Exception($@"Assembly path doesn't contain a GUID: {exAssembly.Location}");
    }

View solution in original post

0 Kudos
17 Replies
StephenRhea
New Contributor III

Haven't tried this with 3.0, but for 2.9 you need to pass false to GetCustomAttributes instead of true.

// ...
var idAttribute = Assembly.GetExecutingAssembly().GetCustomAttributes(false).OfType<GuidAttribute>().FirstOrDefault();
if (idAttribute == null) return string.Empty;

var id = idAttribute.Value;
if (!Guid.TryParse(id, out var guid)) return string.Empty;
// ...

 

0 Kudos
AbelPerez
Occasional Contributor III

I looked up the documentation and it seems that parameter for isInherit is required in the call but ultimately ignored in GetCustomAttributes .

0 Kudos
AbelPerez
Occasional Contributor III

So tried your code and I'm getting a null on line 3. I also tried just the plain jane GetCustomAttributes() and that also returns zero elements.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

@AbelPerez , it appears that the AddinInfoManager sample was missing when we published the 3.0 community samples.  Sorry about that, but i added a 3.0 version of the sample and it appeared to work for me.  arcgis-pro-sdk-community-samples/Content/AddInInfoManager at master · Esri/arcgis-pro-sdk-community-...

Wolf_0-1657657070753.png

 

0 Kudos
AbelPerez
Occasional Contributor III

@Wolf that is still a no-go for me. The line where it crashes is same as before

var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];

 

When I run string guidStr = AddIn.GetAddInId(); it seems that the function GetCustomAttributes returns an empty collection.

Rather odd since I started this project from scratch and used the guide here https://developers.arcgis.com/documentation/arcgis-add-ins-and-automation/arcgis-pro/tutorials/build.... I did however go to the Project Properties and change some stuff there. Mainly default namespace and assembly version. Mainly in the Package section.

 

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi,

Have you tried GetConfigDamlAddInInfo method from @Wolf suggested sample? Returning AddIn class has Id property and it must contain Guid you are looking. It works on 2.9

0 Kudos
AbelPerez
Occasional Contributor III

Yes I did and it works IF you know the file path of the addin. To know the path of the Addin you need the guid. So to get the guid, you need the guid.

// 1. get the filename of this .esriAddinX file
var fileName = AddIn.GetAddInId();
// 2. get the config.daml content from the esriAddinX file
var versionTuple = AddIn.GetConfigDamlAddInInfo(fileName);
MessageBox.Show($@"Version: {versionTuple.Version} desktopVersion: {versionTuple.DesktopVersion} Id: {Module1.Id}");

 

But again this fails for me on line 2.

0 Kudos
AbelPerez
Occasional Contributor III

Ok this is very odd. I thought I had changed something that broke it. But I just created a brand new Addin with the VS template, added a button, added the Addin and Extensions classes. It still bombs at the same line.

My guess is that a project migration from Framework to Core keeps some properties while a brand new project based on Core doesn't have them. Idk.

 

0 Kudos
StephenRhea_NV5
Occasional Contributor

I was wondering that as well. It seems like the way the Config.daml attributes are written to the assembly is different between .NET Framework 4.8 and .NET 6, assuming your daml file is the same in both, of course.

0 Kudos