private void SitesCatalog_SiteOpened(object sender, SitesCatalog.SiteOpenedEventArgs e)
        {
            if (e.Site == null || string.IsNullOrWhiteSpace(e.Site.Url))
            {
                return;
            }

            BuilderApplication.Instance.CurrentSite             = e.Site;
            BuilderApplication.Instance.BuilderScreenVisibility = System.Windows.Visibility.Visible;
            BuilderApplication.Instance.CatalogScreenVisibility = System.Windows.Visibility.Collapsed;
            BuilderApplication.Instance.NewappScreenVisibility  = System.Windows.Visibility.Collapsed;

            SidePanelContainer.Visibility = System.Windows.Visibility.Collapsed;
            MapContentsHost.GoToMapContent(false);

            BuilderConfigurationProvider configProvider = ViewerApplicationControl.ConfigurationProvider as BuilderConfigurationProvider;

            if (configProvider != null)
            {
                configProvider.MapXaml = null; // clear the map xaml
            }
            string baseUrl = e.Site.Url;

            if (baseUrl.IndexOf("?", StringComparison.Ordinal) < 0 && !baseUrl.EndsWith("/", StringComparison.Ordinal)) // has no query parameter
            {
                baseUrl += '/';
            }
            ViewerApplicationControl.Instance.DefaultApplicationSettings = null;
            ViewerApplicationControl.BaseUri = new Uri(baseUrl, UriKind.Absolute);
        }
        public override void Execute(object parameter)
        {
            string layoutFilePath = parameter as string;

            if (string.IsNullOrEmpty(layoutFilePath))
            {
                return;
            }

            // Applying the layout will, among other things, retrieve the configuration of tools.  Important to do this prior to getting map
            // configuration so that tools have a chance to do any cleanup that affects the map before the map is saved
            ViewerApplicationControl.Instance.ApplyNewLayout(layoutFilePath);
            BuilderConfigurationProvider configProvider = ViewerApplicationControl.Instance.ConfigurationProvider as BuilderConfigurationProvider;

            if (configProvider != null)
            {
                configProvider.MapXaml = ViewerApplicationControl.Instance.View.GetMapConfiguration(null);
            }
        }
        public static void CreateNewApplication(string MapXaml)
        {
            // Reset web map properties
            if (string.IsNullOrEmpty(MapXaml))
            {
                ViewerApplication.WebMapSettings.Linked   = null;
                ViewerApplication.WebMapSettings.Document = null;
                ViewerApplication.WebMapSettings.ID       = null;
                ViewerApplication.WebMapSettings.ItemInfo = null;
            }

            BuilderConfigurationProvider configProvider = ViewerApplicationControl.Instance.ConfigurationProvider as BuilderConfigurationProvider;

            if (configProvider != null)
            {
                configProvider.MapXaml = MapXaml;
            }

            if (BuilderApplication.Instance.Templates == null)
            {
                return;
            }

            Template defaultTemplate = BuilderApplication.Instance.Templates.FirstOrDefault <Template>(t => t.IsDefault);

            if (defaultTemplate == null && BuilderApplication.Instance.Templates.Count > 0)
            {
                defaultTemplate = BuilderApplication.Instance.Templates[0];
            }

            if (defaultTemplate != null)
            {
                BuilderApplication.Instance.CurrentTemplate = defaultTemplate;
                BuilderApplication.Instance.BaseUrl         = Application.Current.Host.Source.AbsoluteUri.Replace("ESRI.ArcGIS.Mapping.Builder.xap", "").TrimEnd('/');
                ViewerApplicationControl.Instance.DefaultApplicationSettings = BuilderApplication.Instance;
                ViewerApplicationControl.Instance.BaseUri = new Uri(string.Format("{0}/", defaultTemplate.BaseUrl), UriKind.Absolute);
            }

            BuilderApplication.Instance.CurrentSite             = null; // Since this is a new site - it hasn't been published yet
            BuilderApplication.Instance.CatalogScreenVisibility = Visibility.Collapsed;
            BuilderApplication.Instance.NewappScreenVisibility  = Visibility.Collapsed;
            BuilderApplication.Instance.BuilderScreenVisibility = Visibility.Visible;
        }