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);
}
}