ArcGISPortalViewer.ViewModel.MapViewModel.UpdateWebMap C# (CSharp) Method

UpdateWebMap() private method

private UpdateWebMap ( ) : Task
return Task
        private async Task<WebMap> UpdateWebMap()
        {
            try
            {
                WebMap = await WebMap.FromPortalItemAsync(PortalItem);
                if (WebMap == null)
                    return null;
                
                WebMapVM = await WebMapViewModel.LoadAsync(WebMap, this.PortalItem.ArcGISPortal);
                if (WebMapVM == null)
                    return null;
               
                var errors = WebMapVM.LoadErrors.ToArray();
                if (errors != null && errors.Any())
                {
                    string title = null;
                    string message = null;
                    if (errors.Count() == 1)
                    {
                        WebMapLayer webMapLayer = errors.First().Key;
                        var layerName = webMapLayer.Title ?? webMapLayer.Id ?? webMapLayer.LayerType.ToString();
                        title = string.Format("Unable to add the layer '{0}' in the map.", layerName);
                        message = errors.First().Value.Message;
                    }
                    else
                    {
                        title = string.Format("Unable to add {0} layers in the map.", errors.Count());
                        foreach (KeyValuePair<WebMapLayer, Exception> error in errors)
                        {
                            WebMapLayer webMapLayer = error.Key;
                            var layerName = webMapLayer.Title ?? webMapLayer.Id ?? webMapLayer.LayerType.ToString();
                            message += layerName + ":  " + error.Value.Message + Environment.NewLine;
                        }
                    }
                    var ex = new Exception(message);
                    var _ = App.ShowExceptionDialog(ex, title);
                }
                
                // <start workaround>
                // This is work around for WebMapViewModel because OutFields is not being set 
                // on the GeodatabaseFeatureServiceTable in the API this will be fixed after Beta.
                foreach(var featureLayer in OperationalLayers.OfType<FeatureLayer>())
                {
                    var webMapLayer = WebMap.OperationalLayers.FirstOrDefault(wml => wml.Id == featureLayer.ID);
                    if(webMapLayer != null && webMapLayer.PopupInfo != null && webMapLayer.PopupInfo.FieldInfos != null && webMapLayer.PopupInfo.FieldInfos.Any())
                    {
                        var serviceFeatureTable = featureLayer.FeatureTable as ServiceFeatureTable;
                        if (serviceFeatureTable != null && serviceFeatureTable.OutFields == null)
                        {
                            serviceFeatureTable.OutFields = new OutFields(webMapLayer.PopupInfo.FieldInfos.Where(f => f != null).Select(f => f.FieldName));
                            serviceFeatureTable.RefreshFeatures(false);
                        }
                    }
                }
                // <end workaround>
            }
            catch (Exception ex)
            {
                var _ = App.ShowExceptionDialog(ex, "An exception was caught while trying to open the map.");
            }

            return WebMap;
        }