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); } }
/// <summary> /// Execute the query command to get these info: /// 1. basic portal info; /// 2. advanced portal self calls; /// 3. fed server and registered ds. /// </summary> private async void CmdDoQuery() { #region setup ArcGISPortal arcGISportal = ArcGISPortalManager.Current.GetActivePortal(); if (arcGISportal == null) { return; } if (!arcGISportal.IsSignedOn()) { //Calling "SignIn" will trigger the OAuth popup if your credentials are //not cached (eg from a previous sign in in the session) await QueuedTask.Run(() => { SignInResult result = arcGISportal.SignIn(); if (result.success) { envFailure = false; } else { envFailure = true; } }); } //fill in username and password manually #endregion #region portal basic info var online = ArcGISPortalManager.Current.GetPortal(new Uri(_portalUrl)); //print out portal info StringBuilder out_str = new StringBuilder(); out_str.AppendLine("Portal URL: " + arcGISportal.PortalUri); out_str.AppendLine("User name: " + arcGISportal.GetSignOnUsername()); #endregion /* * the following calls are only present for portals; For online, just print the info generated above. */ if (!_portalUrl.Contains("arcgis.com")) { //get web adaptor string wa = arcGISportal.PortalUri.Segments[1]; _portalUrl = arcGISportal.PortalUri.ToString(); /* * Make Portal self call to understand the current role - user, publisher or admin? and other portal info */ #region portal self call //get portal self call's response UriBuilder selfURL = new UriBuilder(new Uri(_portalUrl)) { Path = wa + "sharing/rest/portals/self", Query = "f=json" }; EsriHttpResponseMessage response = new EsriHttpClient().Get(selfURL.Uri.ToString()); dynamic portalSelf = JObject.Parse(response.Content.ReadAsStringAsync().Result); // if the response doesn't contain the user information then it is essentially // an anonymous request against the portal if (portalSelf.user == null) { return; } string userRole = portalSelf.user.role; string[] userPriviledges = portalSelf.user.privileges.ToObject <string[]>(); if (portalSelf.isPortal == null) { return; } string IsPortal = (bool)(portalSelf.isPortal) ? "True" : "False"; if (portalSelf.supportsSceneServices == null) { return; } string SupportsSceneServices = (bool)(portalSelf.supportsSceneServices) ? "True" : "False"; if (portalSelf.helperServices == null) { return; } var elevationService = portalSelf.helperServices.elevation; var routeService = portalSelf.helperServices.route; out_str.AppendLine("User role: " + userRole); out_str.AppendLine("User Privileges:"); userPriviledges.ToList().ForEach(i => out_str.Append("\t" + i)); out_str.AppendLine(); out_str.AppendLine("Supports Scene services? " + SupportsSceneServices); out_str.AppendLine("Is it a portal? " + IsPortal); out_str.AppendLine("Helper Services: (1) elevation: " + ((elevationService == null) ? "null" : elevationService.url) + "\t" + "(2) Route: " + ((routeService == null) ? "null" : routeService.url)); #endregion /* * Make rest calls to get the list of fed servers, signaling which is the hosting fed server; * Also to print the registered data sources */ #region portal self server call selfURL = new UriBuilder(new Uri(_portalUrl)) { Path = wa + "sharing/rest/portals/self/servers", Query = "f=json" }; response = new EsriHttpClient().Get(selfURL.Uri.ToString()); dynamic portalSelf_servers = JObject.Parse(response.Content.ReadAsStringAsync().Result); // if the response doesn't contain the user information then it is essentially // an anonymous request against the portal if (portalSelf_servers == null) { return; } out_str.AppendLine(); out_str.AppendLine("Fed servers:"); int cnt = 0; foreach (var server in portalSelf_servers.servers) { out_str.Append("\t(" + (++cnt) + ") {" + server.url + ", " + server.adminUrl + ", " + server.serverRole + "}"); /* * generate a new token from portal * re-using old tokens would only yield empty item list for data sources */ #region generate server token ArcGISPortal arcGISportal_2 = ArcGISPortalManager.Current.GetActivePortal(); string server_token = arcGISportal_2.GetToken(); #endregion /* * get the registered data source from each server */ #region data sources string wa_2 = (new Uri(server.adminUrl.Value)).Segments[1]; //1. get registered shared folder selfURL = new UriBuilder(new Uri(server.adminUrl.Value)) { Path = wa_2 + "/admin/data/findItems/", Query = "f=json&parentPath=/fileShares&types=folder&token=" + server_token }; response = new EsriHttpClient().Get(selfURL.Uri.ToString()); dynamic server_registeredFolder = JObject.Parse(response.Content.ReadAsStringAsync().Result); // if the response doesn't contain the user information then it is essentially // an anonymous request against the portal if (server_registeredFolder == null) { return; } out_str.AppendLine(); out_str.AppendLine(); out_str.Append("Registered Folder:\t"); if (server_registeredFolder.items == null) { out_str.Append("\tRegistered Folder not accessible\t"); } foreach (var item in server_registeredFolder.items) { out_str.Append("\t{" + item.type + " - " + item.info.path + "}"); } //2. get registered shared egdb selfURL = new UriBuilder(new Uri(server.adminUrl.Value)) { Path = wa_2 + "/admin/data/findItems/", Query = "f=json&parentPath=enterpriseDatabases&types=egdb&token=" + server_token }; response = new EsriHttpClient().Get(selfURL.Uri.ToString()); dynamic server_registeredDS = JObject.Parse(response.Content.ReadAsStringAsync().Result); // if the response doesn't contain the user information then it is essentially // an anonymous request against the portal if (server_registeredDS == null) { return; } out_str.AppendLine(); out_str.AppendLine(); out_str.Append("Registered egdb:\t"); if (server_registeredDS.items == null) { out_str.Append("\tRegistered egdb not accessible\t"); } foreach (var ds in server_registeredDS.items) { out_str.Append("\t{" + ds.type + " - " + ds.path + "}"); } //3. get registered nosql //query="f=json&parentPath=nosqlDatabases&types=nosql" #endregion } #endregion } else { out_str.AppendLine(); out_str.AppendLine($@"To see further Portal Info details you have to connect to a portal other then ArcGIS Online"); } if (envFailure) { QueryResult = "Not able to sign into portal!\n\n" + out_str.ToString(); } else { QueryResult = "Successfully signed into portal\n\n" + out_str.ToString(); } System.Diagnostics.Debug.WriteLine(QueryResult); }