Select to view content in your preferred language

Populate - Folder Connection, Custom Style, Custom Tools in new project

580
4
Jump to solution
10-10-2022 10:24 AM
JeromeHaaland
Regular Contributor

Goal - Create an add-in button that populates a new project with a Folder Connection, a Custom Style and a Custom Tools

Lines 3 through 9 below run correctly and were copied from:

ProSnippets Content · Esri/arcgis-pro-sdk Wiki (github.com)

After modifying the example code to work with a "Style" instead of a "Folder Connection".  An exception is thrown on line 16... 

System.NullReferenceException
Message=Object reference not set to an instance of an object.

Thus, if I read the exception correctly, an object of StyleProjectItem is not instantiated or the return is left as null.

Reading through the API documentation...  IProjectItem Interface—ArcGIS Pro The  Overview -> Remarks state, "These classes also follow a convention that includes "ProjectItem" as part of their name"

Based upon the stated convention... it appears like StyleProjectItem should be instantiated correctly.

protected override async void OnClick()
        {
            string folderPath = "C:\\Data";
            var folder = await QueuedTask.Run(() =>
            {
                var itemFolder = ItemFactory.Instance.Create(folderPath) as IProjectItem;
                return Project.Current.AddItem(itemFolder) ? itemFolder as FolderConnectionProjectItem : null;
            });
            FavoritesManager.Current.AddFavorite(folder);

            string stylePath = "C:\\Data\\CustomStyles.stylx";

            var style = await QueuedTask.Run(() =>
            {
                var itemStyle = ItemFactory.Instance.Create(stylePath) as IProjectItem;
                return Project.Current.AddItem(itemStyle) ? itemStyle as StyleProjectItem : null;
            });
            FavoritesManager.Current.AddFavorite(style);

            string paToolBoxPath = "C:\\Data\\CustomTools.tbx";
            var toolBox = await QueuedTask.Run(() =>
            {
                var itemTool = ItemFactory.Instance.Create(paToolBoxPath) as IProjectItem;
                return Project.Current.AddItem(itemTool) ? itemTool as GeoprocessingProjectItem : null;
            });
            FavoritesManager.Current.AddFavorite(toolBox);
        }
    }

 

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

For toolbox try this code:

                string paToolBoxPath = "C:\\Data\\CustomTools.tbx";
                var itemTool = ItemFactory.Instance.Create(paToolBoxPath) as IProjectItem;
                await QueuedTask.Run(() =>
                {
                    Project.Current.AddItem(itemTool);
                });

                if (FavoritesManager.Current.CanAddAsFavorite(itemTool as Item))
                {
                    FavoritesManager.Current.AddFavorite(itemTool as Item);
                }

Last code sample works for folder connections and styles too. Maybe this way is right to add items to project and favorites

View solution in original post

0 Kudos
4 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Style needs a little bit different workflow:

                string stylePath = "C:\\Data\\CustomStyles.stylx";

                await QueuedTask.Run(() =>
                {
                    Project.Current.AddStyle(stylePath);
                });

                var itemStyle = ItemFactory.Instance.Create(stylePath);
                FavoritesManager.Current.AddFavorite(itemStyle);

Another one thing. If you return null from QueuedTask.Run then you need to check result for it is not null before adding to favorites

0 Kudos
JeromeHaaland
Regular Contributor

Gintautas,

Thank You... the above code works well to add a style.

However... there does not appear to be a similar method to add a custom toolbox.  As I am trying to do in lines 20 through 26 of my code.

Do you have a suggestion to add a custom toolbox with C# code?

 

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi,

For toolbox try this code:

                string paToolBoxPath = "C:\\Data\\CustomTools.tbx";
                var itemTool = ItemFactory.Instance.Create(paToolBoxPath) as IProjectItem;
                await QueuedTask.Run(() =>
                {
                    Project.Current.AddItem(itemTool);
                });

                if (FavoritesManager.Current.CanAddAsFavorite(itemTool as Item))
                {
                    FavoritesManager.Current.AddFavorite(itemTool as Item);
                }

Last code sample works for folder connections and styles too. Maybe this way is right to add items to project and favorites

0 Kudos
JeromeHaaland
Regular Contributor

Gintautas,

Excellent help.  The last solution was the best.  It is better than the example provided by ESRI because similar code works for a Folder Connect, Style and Toolbox.

I did change my code to use the last solution for all 3 items.

Nice and elegant solution.

0 Kudos