ArcGIS Pro 3.2 API Reference Guide
ArcGIS.Desktop.Core Namespace / FavoritesManager Class / AddFavorite Method / AddFavorite(Item) Method
The item to add to the favorite collection. For example a folder connection or a toolbox.
Example

In This Topic
    AddFavorite(Item) Method
    In This Topic
    Adds an item to the favorites collection. The item is added to the collection with the Favorite.IsAddedToAllNewProjects value set to false.
    Syntax
    public Favorite AddFavorite( 
       Item item
    )
    Public Overloads Function AddFavorite( _
       ByVal item As Item _
    ) As Favorite

    Parameters

    item
    The item to add to the favorite collection. For example a folder connection or a toolbox.

    Return Value

    The favorite item. Null if the item cannot be added as a favorite.
    Remarks
    An item can only exist once in the favorite collection. If the item already exists in the favorites collection, then no additional item is added. However the Favorite.IsAddedToAllNewProjects property will be updated to false.

    You should always check CanAddAsFavorite before passing an item to this method.

    Example
    Add a Favorite - Folder
    var itemFolder = ItemFactory.Instance.Create(@"d:\data");
    
    // is the folder item already a favorite?
    var fav = FavoritesManager.Current.GetFavorite(itemFolder);
    if (fav == null)
    {
      if (FavoritesManager.Current.CanAddAsFavorite(itemFolder))
      {
        fav = FavoritesManager.Current.AddFavorite(itemFolder);
      }
    }
    
    Add a Favorite - Style project item
    StyleProjectItem styleItem = Project.Current.GetItems<StyleProjectItem>().
                            FirstOrDefault(style => (style.Name == "ArcGIS 3D"));
    
    if (FavoritesManager.Current.CanAddAsFavorite(styleItem))
    {
      // add to favorites with IsAddedToAllNewProjects set to false
      FavoritesManager.Current.AddFavorite(styleItem);
    }
    
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also