Example #1
0
        public async Task <BA_ReturnCode> GetPortalFile(string portalOrganization, string itemId, string downLoadPath)
        {
            try
            {
                var          enumPortal = ArcGISPortalManager.Current.GetPortals();
                ArcGISPortal myPortal   = null;
                foreach (var oPortal in enumPortal)
                {
                    var info = await oPortal.GetPortalInfoAsync();

                    if (info.OrganizationName.Equals(portalOrganization))
                    {
                        myPortal = oPortal;
                    }
                }
                if (myPortal == null)
                {
                    Module1.Current.ModuleLogManager.LogError(nameof(GetPortalFile),
                                                              "The NRCS Portal is missing from the ArcGIS Pro 'Portals' tab. The requested file cannot be downloaded! ArcGIS Pro will " +
                                                              "use a previous version of the file if it exists");
                    return(BA_ReturnCode.UnknownError);
                }
                if (!myPortal.IsSignedOn())
                {
                    var result = await myPortal.SignInAsync();

                    if (result.success == false)
                    {
                        Module1.Current.ModuleLogManager.LogError(nameof(GetPortalFile),
                                                                  "Unable to signIn to the NRCS Portal. Can you connect to the portal in the ArcGIS Pro 'Portals' tab? The requested file cannot be downloaded! " +
                                                                  "ArcGIS Pro will use a previous version of the file if it exists");
                        return(BA_ReturnCode.UnknownError);
                    }
                }

                //assume we query for some content
                var query   = PortalQueryParameters.CreateForItemsWithId(itemId);
                var results = await myPortal.SearchForContentAsync(query);

                var portalItem = results.Results.First();   //first item

                bool success = false;
                if (portalItem != null)
                {
                    //rename the original, if it exists so that we get the most current copy
                    if (File.Exists(downLoadPath))
                    {
                        string strDirectory = Path.GetDirectoryName(downLoadPath);
                        string strFile      = Path.GetFileNameWithoutExtension(downLoadPath) + "_1" + Path.GetExtension(downLoadPath);
                        File.Copy(downLoadPath, strDirectory + "\\" + strFile, true);
                        File.Delete(downLoadPath);
                        Module1.Current.ModuleLogManager.LogDebug(nameof(GetPortalFile),
                                                                  "Renamed " + downLoadPath + " so a new copy could be downloaded");
                    }
                    //download the item
                    success = await portalItem.GetItemDataAsync(downLoadPath);
                }
                if (success == true)
                {
                    Module1.Current.ModuleLogManager.LogDebug(nameof(GetPortalFile),
                                                              "The requested file cannot was successfully downloaded from the Portal");
                    return(BA_ReturnCode.Success);
                }
                else
                {
                    Module1.Current.ModuleLogManager.LogError(nameof(GetPortalFile),
                                                              "The requested file cannot be downloaded from the Portal! ArcGIS Pro will " +
                                                              "use a previous version of the file if it exists");
                    return(BA_ReturnCode.UnknownError);
                }
            }
            catch (Exception e)
            {
                Module1.Current.ModuleLogManager.LogError(nameof(GetPortalFile),
                                                          "Exception: " + e.Message);
                return(BA_ReturnCode.UnknownError);
            }
        }