Exemplo n.º 1
0
        /// <summary>
        /// Uploads a local item to online with its parameters
        /// </summary>
        /// <param name="baseURI"></param>
        /// <param name="itemPathStr"></param>
        /// <param name="thumbnail"></param>
        /// <param name="tags"></param>
        /// <returns></returns>
        Tuple <bool, string> uploadItem(string baseURI, string itemPathStr, string thumbnail, String tags)
        {
            String[] tags_arr = tags.Split(new Char[] { ',', ':', '\t' });

            EsriHttpClient       myClient = new EsriHttpClient();
            Tuple <bool, string> response = null;
            Item itemToUpload             = ItemFactory.Create(itemPathStr);

            if (itemToUpload != null)
            {
                response = myClient.Upload(baseURI, itemToUpload, thumbnail, tags_arr);
                if (response.Item1)
                {
                    Tuple <bool, SDCItem> result = SearchPortalItemREST(itemToUpload.Name, itemToUpload.Type, baseURI);
                    if (result.Item1)
                    {
                        return(new Tuple <bool, string>(true, result.Item2.id));
                    }
                    else
                    {
                        return(new Tuple <bool, string>(true, "Cannot find item online"));
                    }
                }
                else
                {
                    return(new Tuple <bool, String>(false, "Upload fails - " + response.Item2));
                }
            }
            else
            {
                return(new Tuple <bool, String>(false, "Null item cannot be uploaded - " + response.Item2));
            }
        }
        private async Task <string> UploadImpl()
        {
            // Create EsriHttpClient object
            var httpClient = new EsriHttpClient();

            // Upload vtpk file to the currently active portal
            var itemToUpload = ItemFactory.Instance.Create(FilePath);
            var tags         = new string[] { "ArcGIS Pro", "SDK", "UploadVtpkToAgol Demo" };
            var portalUrl    = ArcGISPortalManager.Current.GetActivePortal().PortalUri.ToString();

            var result = httpClient.Upload(
                portalUrl, itemToUpload, string.Empty, tags);

            if (result.Item1 == false)
            {
                return($@"Unable to upload this item: {FilePath} to ArcGIS Online");
            }

            string userName = ArcGISPortalManager.Current.GetActivePortal().GetSignOnUsername();
            string query    = $@"q=owner:{userName} tags:""UploadVtpkToAgol Demo"" ";

            // Once uploaded make another REST call to search for the uploaded data
            var searchUrl = new UriBuilder(portalUrl)
            {
                Path  = "sharing/rest/search",
                Query = $@"{query}&f=json"
            };

            var searchResults = httpClient.Get(searchUrl.Uri.ToString());

            dynamic resultItems = JObject.Parse(await searchResults.Content.ReadAsStringAsync());

            long numberOfTotalItems = resultItems.total.Value;

            if (numberOfTotalItems == 0)
            {
                return($@"Unable to find uploaded item with query: {query}");
            }

            var resultItemList = new List <dynamic>();

            resultItemList.AddRange(resultItems.results);
            //get the first result
            dynamic item = resultItemList[0];

            // Create an item from the search results

            string itemId      = item.id;
            var    currentItem = ItemFactory.Instance.Create(itemId, ItemFactory.ItemType.PortalItem);

            // Finally add the feature service to the map
            // if we have an item that can be turned into a layer
            // add it to the map
            if (LayerFactory.Instance.CanCreateLayerFrom(currentItem))
            {
                LayerFactory.Instance.CreateLayer(currentItem, MapView.Active.Map);
            }
            return($@"Uploaded this item: {FilePath} to ArcGIS Online and added the item to the Map");
        }
Exemplo n.º 3
0
        private async Task <string> UploadImpl()
        {
            // Create EsriHttpClient object
            var httpClient = new EsriHttpClient();

            // Upload vtpk file to the currently active portal
            var itemToUpload = ItemFactory.Instance.Create(FilePath);
            var tags         = new string[] { "ArcGIS Pro", "SDK", "UploadVtpkToAgol Demo" };
            var portalUrl    = ArcGISPortalManager.Current.GetActivePortal().PortalUri.ToString();

            var uploadDefn = new UploadDefinition(portalUrl, itemToUpload, tags);

            var result = httpClient.Upload(uploadDefn);

            if (result.Item1 == false)
            {
                return($@"Unable to upload this item: {FilePath} to ArcGIS Online");
            }

            // Once uploaded make another REST call to search for the uploaded data
            var    portal   = ArcGISPortalManager.Current.GetActivePortal();
            string userName = ArcGISPortalManager.Current.GetActivePortal().GetSignOnUsername();

            //Searching for Vector tile packages in the current user's content
            var pqp = PortalQueryParameters.CreateForItemsOfTypeWithOwner(PortalItemType.VectorTilePackage, userName, "tags: UploadVtpkToAgol Demo");

            //Execute to return a result set
            PortalQueryResultSet <PortalItem> results = await ArcGISPortalExtensions.SearchForContentAsync(portal, pqp);

            if (results.Results.Count == 0)
            {
                return($@"Unable to find uploaded item with query: {pqp.Query}");
            }

            // Create an item from the search results
            string itemId      = results.Results[0].ID;
            var    currentItem = ItemFactory.Instance.Create(itemId, ItemFactory.ItemType.PortalItem);
            var    lyrParams   = new LayerCreationParams(currentItem);

            // Finally add the feature service to the map
            // if we have an item that can be turned into a layer
            // add it to the map
            if (LayerFactory.Instance.CanCreateLayerFrom(currentItem))
            {
                LayerFactory.Instance.CreateLayer <FeatureLayer>(lyrParams, MapView.Active.Map);
            }
            return($@"Uploaded this item: {results.Results[0].Name} [Type: {results.Results[0].Type}] to ArcGIS Online and added the item to the Map");
        }
        /// <summary>
        /// Uploads a local item to online with its parameters
        /// </summary>
        /// <param name="baseURI"></param>
        /// <param name="itemPathStr"></param>
        /// <param name="thumbnail"></param>
        /// <param name="tags"></param>
        /// <returns></returns>
        async Task <Tuple <bool, string> > UploadItemAsync(string baseURI, string itemPathStr, string thumbnail, string tags)
        {
            String[] tags_arr = tags.Split(new Char[] { ',', ':', '\t' });

            EsriHttpClient       myClient = new EsriHttpClient();
            Tuple <bool, string> response = null;

            var itemToUpload = ItemFactory.Instance.Create(itemPathStr);

            if (itemToUpload != null)
            {
                //Create the upload defintion
                var uploadDfn = new UploadDefinition(baseURI, itemToUpload, tags_arr);
                uploadDfn.Thumbnail = thumbnail;
                //upload item
                response = myClient.Upload(uploadDfn);
                //response = myClient.Upload(baseURI, itemToUpload, thumbnail, tags_arr);  //obsolete
                if (response.Item1) //Upload was successfull
                {
                    //Search for the uploaded item to get its ID.
                    Tuple <bool, PortalItem> result = await SearchPortalForItemsAsync(baseURI, itemToUpload);

                    if (result.Item1) //search successful
                    {
                        ItemID = result.Item2.ToString();
                        return(new Tuple <bool, string>(true, result.Item2.ID));
                    }

                    else //item not found
                    {
                        ItemID = "Cannot find item online";
                        return(new Tuple <bool, string>(true, "Cannot find item online"));
                    }
                }
                else //Upload failed
                {
                    ItemID = "Upload failed";
                    return(new Tuple <bool, String>(false, "Upload failed"));
                }
            }
            else   //Item was not created
            {
                ItemID = "Item cannot be created";
                return(new Tuple <bool, String>(false, "Item cannot be created"));
            }
        }
        private async Task<string> UploadImpl()
        {
            // Create EsriHttpClient object
            var httpClient = new EsriHttpClient();

            // Upload vtpk file to the currently active portal
            var itemToUpload = ItemFactory.Create(FilePath);
            var tags = new string[] { "ArcGIS Pro", "SDK", "UploadVtpkToAgol Demo" };

            var result = httpClient.Upload(PortalManager.GetActivePortal().ToString(), itemToUpload, string.Empty, tags);
            if (result.Item1 == false)
                return $@"Unable to upload this item: {FilePath} to ArcGIS Online";

            // Make a self call to extract the signed on username for now. 
            // Coming soon - An API which will give you the the signed on username for a portal
            var selfUrl = new UriBuilder(PortalManager.GetActivePortal())
            {
                Path = "sharing/rest/portals/self",
                Query = "f=json"
            };

            EsriHttpResponseMessage response = httpClient.Get(selfUrl.Uri.ToString());

            dynamic portalSelf = JObject.Parse(await response.Content.ReadAsStringAsync());
            // if the response doesn't contain the user information then it is essentially
            // an anonymous request against the portal
            if (portalSelf.user == null)
                return "Unable to get current user from portal";

            string userName = portalSelf.user.username;
            string query = $@"q=owner:{userName} tags:""UploadVtpkToAgol Demo"" ";

            // Once uploaded make another REST call to search for the uploaded data
            var searchUrl = new UriBuilder(PortalManager.GetActivePortal())
            {
                Path = "sharing/rest/search",
                Query = $@"{query}&f=json"
            };

            var searchResults = httpClient.Get(searchUrl.Uri.ToString());

            dynamic resultItems = JObject.Parse(await searchResults.Content.ReadAsStringAsync());

            long numberOfTotalItems = resultItems.total.Value;

            if (numberOfTotalItems == 0)
                return $@"Unable to find uploaded item with query: {query}";

            var resultItemList = new List<dynamic>();
            resultItemList.AddRange(resultItems.results);
            //get the first result
            dynamic item = resultItemList[0];

            // Create an item from the search results

            string itemId = item.id;
            var currentItem = ItemFactory.Create(itemId, ItemFactory.ItemType.PortalItem);

            // Finally add the feature service to the map
            // if we have an item that can be turned into a layer
            // add it to the map
            if (LayerFactory.CanCreateLayerFrom(currentItem))
                LayerFactory.CreateLayer(currentItem, MapView.Active.Map);
            return $@"Uploaded this item: {FilePath} to ArcGIS Online and added the item to the Map";
        }
        private async Task <string> UploadImpl()
        {
            // Create EsriHttpClient object
            var httpClient = new EsriHttpClient();

            // Upload vtpk file to the currently active portal
            var itemToUpload = ItemFactory.Create(FilePath);
            var tags         = new string[] { "ArcGIS Pro", "SDK", "UploadVtpkToAgol Demo" };

            var result = httpClient.Upload(PortalManager.GetActivePortal().ToString(), itemToUpload, string.Empty, tags);

            if (result.Item1 == false)
            {
                return($@"Unable to upload this item: {FilePath} to ArcGIS Online");
            }

            // Make a self call to extract the signed on username for now.
            // Coming soon - An API which will give you the the signed on username for a portal
            var selfUrl = new UriBuilder(PortalManager.GetActivePortal())
            {
                Path  = "sharing/rest/portals/self",
                Query = "f=json"
            };

            EsriHttpResponseMessage response = httpClient.Get(selfUrl.Uri.ToString());

            dynamic portalSelf = JObject.Parse(await response.Content.ReadAsStringAsync());

            // if the response doesn't contain the user information then it is essentially
            // an anonymous request against the portal
            if (portalSelf.user == null)
            {
                return("Unable to get current user from portal");
            }

            string userName = portalSelf.user.username;
            string query    = $@"q=owner:{userName} tags:""UploadVtpkToAgol Demo"" ";

            // Once uploaded make another REST call to search for the uploaded data
            var searchUrl = new UriBuilder(PortalManager.GetActivePortal())
            {
                Path  = "sharing/rest/search",
                Query = $@"{query}&f=json"
            };

            var searchResults = httpClient.Get(searchUrl.Uri.ToString());

            dynamic resultItems = JObject.Parse(await searchResults.Content.ReadAsStringAsync());

            long numberOfTotalItems = resultItems.total.Value;

            if (numberOfTotalItems == 0)
            {
                return($@"Unable to find uploaded item with query: {query}");
            }

            var resultItemList = new List <dynamic>();

            resultItemList.AddRange(resultItems.results);
            //get the first result
            dynamic item = resultItemList[0];

            // Create an item from the search results

            string itemId      = item.id;
            var    currentItem = ItemFactory.Create(itemId, ItemFactory.ItemType.PortalItem);

            // Finally add the feature service to the map
            // if we have an item that can be turned into a layer
            // add it to the map
            if (LayerFactory.CanCreateLayerFrom(currentItem))
            {
                LayerFactory.CreateLayer(currentItem, MapView.Active.Map);
            }
            return($@"Uploaded this item: {FilePath} to ArcGIS Online and added the item to the Map");
        }
   /// <summary>
   /// Uploads a local item to online with its parameters
   /// </summary>
   /// <param name="baseURI"></param>
   /// <param name="itemPathStr"></param>
   /// <param name="thumbnail"></param>
   /// <param name="tags"></param>
   /// <returns></returns>
   Tuple<bool, string> uploadItem(string baseURI, string itemPathStr, string thumbnail, String tags)
   {
       String[] tags_arr = tags.Split(new Char[] { ',', ':', '\t' });
 
       EsriHttpClient myClient = new EsriHttpClient();
       Tuple<bool, string> response = null;
       Item itemToUpload = ItemFactory.Create(itemPathStr);
       if (itemToUpload != null)
       {
           response = myClient.Upload(baseURI, itemToUpload, thumbnail, tags_arr);
           if (response.Item1)
           {
               Tuple<bool, SDCItem> result = SearchPortalItemREST(itemToUpload.Name, itemToUpload.Type, baseURI);
               if(result.Item1)
                   return new Tuple<bool, string>(true, result.Item2.id);
               else
                   return new Tuple<bool, string>(true,"Cannot find item online");
           }
           else
               return new Tuple<bool, String>(false, "Upload fails - " + response.Item2);
       }else
           return new Tuple<bool, String>(false, "Null item cannot be uploaded - " + response.Item2);
   }