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);
}
}
Solved! Go to Solution.
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
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
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?
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
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.