Create and save map

View inMAUIUWPWPFWinUIView on GitHub

Create and save a map as an ArcGIS PortalItem (i.e. web map).

Image of create and save map

Use case

Maps can be created programmatically in code and then serialized and saved as an ArcGIS web map. A web map can be shared with others and opened in various applications and APIs throughout the platform, such as ArcGIS Pro, ArcGIS Online, the JavaScript API, Collector, and Explorer.

How to use the sample

  1. Select the basemap and layers you'd like to add to your map.
  2. Press the Save button.
  3. Sign into an ArcGIS Online account.
  4. Provide a title, tags, and description.
  5. Save the map.

How it works

  1. A Map is created with a Basemap and a few operational layers.
  2. A Portal object is created and loaded. This will issue an authentication challenge, prompting the user to provide credentials.
  3. Once the user is authenticated, map.SaveAsAsync is called and a new Map is saved with the specified title, tags, and folder.

Relevant API

  • AuthenticationManager
  • ChallengeHandler
  • GenerateCredentialAsync
  • IOAuthAuthorizeHandler
  • Map
  • Map.SaveAsAsync
  • Portal

Tags

ArcGIS Online, OAuth, portal, publish, share, web map

Sample Code

SaveMapPage.xaml.csSaveMapPage.xaml.csSaveMapPage.xamlAuthorMap.xaml.csAuthorMap.xamlArcGISLoginPrompt.cs
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
namespace ArcGIS.Samples.AuthorMap
{
    public partial class SaveMapPage : ContentPage
    {
        // Raise an event so the listener can access input values when the form has been completed
        public event EventHandler<SaveMapEventArgs> OnSaveClicked;

        public SaveMapPage()
        {
            InitializeComponent();
        }

        // If updating an existing map item, show the existing item info and disable changing info
        public void ShowForUpdate(string title, string description, string[] tags)
        {
            // Item title
            MapTitleEntry.Text = title;
            MapTitleEntry.IsEnabled = false;

            // Item description
            MapDescriptionEntry.Text = description;
            MapDescriptionEntry.IsEnabled = false;

            // Item tags
            MapTagsEntry.Text = string.Join(",", tags);
            MapTagsEntry.IsEnabled = false;

            // Show 'Update' rather than 'Save' for button text
            SaveMapButton.Text = "Update";
        }

        // A click handler for the save map button
        private void SaveButtonClicked(object sender, EventArgs e)
        {
            try
            {
                // Get information for the new portal item
                string title = MapTitleEntry.Text;
                string description = MapDescriptionEntry.Text;
                string[] tags = MapTagsEntry.Text.Split(',');

                // Make sure all required info was entered
                if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(description) || tags.Length == 0)
                {
                    throw new Exception("Please enter a title, description, and some tags to describe the map.");
                }

                // Create a new OnSaveMapEventArgs object to store the information entered by the user
                SaveMapEventArgs mapSavedArgs = new SaveMapEventArgs(title, description, tags);

                // Raise the OnSaveClicked event so the main activity can handle the event and save the map
                OnSaveClicked?.Invoke(this, mapSavedArgs);

                // Close the dialog
                Application.Current.MainPage.Navigation.PopAsync();
            }
            catch (Exception ex)
            {
                // Show the exception message (dialog will stay open so user can try again)
                Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
            }
        }

        private void CancelButtonClicked(object sender, EventArgs e)
        {
            // If the user cancels, just navigate back to the previous page
            Application.Current.MainPage.Navigation.PopAsync();
        }
    }

    // Custom EventArgs class for containing portal item properties when saving a map
    public class SaveMapEventArgs : EventArgs
    {
        // Portal item title
        public string MapTitle { get; set; }

        // Portal item description
        public string MapDescription { get; set; }

        // Portal item tags
        public string[] Tags { get; set; }

        public SaveMapEventArgs(string title, string description, string[] tags) : base()
        {
            MapTitle = title;
            MapDescription = description;
            Tags = tags;
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.