/// <summary>
        /// Post "publish" request on the portal item
        /// </summary>
        /// <param name="baseURI"></param>
        /// <param name="username"></param>
        /// <param name="itemId"></param>
        /// <param name="fileType"></param>
        /// <param name="publishParameters"></param>
        /// <returns>tuple: bool ok/failed string: result msg/error msg</returns>
        Tuple <bool, string> PublishService(string baseURI, string username, string itemId,
                                            string fileType, string publishParameters)
        {
            EsriHttpClient myClient   = new EsriHttpClient();
            string         requestUrl = $@"{baseURI}/sharing/rest/content/users/{username}/publish";

            var postData = new List <KeyValuePair <string, string> >();

            postData.Add(new KeyValuePair <string, string>("f", "json"));
            postData.Add(new KeyValuePair <string, string>("itemId", itemId));
            postData.Add(new KeyValuePair <string, string>("fileType", fileType));
            postData.Add(new KeyValuePair <string, string>("publishParameters", publishParameters));
            HttpContent content = new FormUrlEncodedContent(postData);

            EsriHttpResponseMessage respMsg = myClient.Post(requestUrl, content);

            if (respMsg == null)
            {
                return(new Tuple <bool, String>(false, "HTTP response is null"));
            }

            string outStr = respMsg.Content.ReadAsStringAsync().Result;
            //De-serialize the response in JSON into a usable object.
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            //De-serialize the response in JSON into a usable object.
            SharingContracts.PublishedServices obj = (SharingContracts.PublishedServices)serializer.Deserialize(outStr, typeof(SharingContracts.PublishedServices));
            if (obj?.services == null)
            {
                return(new Tuple <bool, String>(false, "Service creation fails - " + outStr));
            }

            string respReturn = "";

            foreach (SharingContracts.PublishedService ps in obj.services)
            {
                respReturn += ps.serviceurl;
            }
            if (respReturn == "")
            {
                return(new Tuple <bool, String>(false, "Service creation fails - " + outStr));
            }
            else
            {
                return(new Tuple <bool, String>(true, respReturn));
            }
        }
        /// <summary>
        /// Post "analyze" request on the portal item
        /// </summary>
        /// <param name="baseURI"></param>
        /// <param name="itemId"></param>
        /// <param name="fileType"></param>
        /// <param name="analyzeParameters"></param>
        /// <returns></returns>
        Tuple <bool, string> AnalyzeService(string baseURI, string itemId, string fileType, string analyzeParameters)
        {
            EsriHttpClient myClient   = new EsriHttpClient();
            string         requestUrl = baseURI + @"/sharing/rest/content/features/analyze";

            var postData = new List <KeyValuePair <string, string> >();

            postData.Add(new KeyValuePair <string, string>("f", "json"));
            postData.Add(new KeyValuePair <string, string>("itemId", itemId));
            postData.Add(new KeyValuePair <string, string>("fileType", fileType));
            postData.Add(new KeyValuePair <string, string>("analyzeParameters", analyzeParameters));
            HttpContent content = new FormUrlEncodedContent(postData);

            EsriHttpResponseMessage respMsg = myClient.Post(requestUrl, content);

            if (respMsg == null)
            {
                return(new Tuple <bool, String>(false, "HTTP response is null"));
            }

            string outStr = respMsg.Content.ReadAsStringAsync().Result;
            //De-serialize the response in JSON into a usable object.
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            //De-serialize the response in JSON into a usable object.
            SharingContracts.AnalyzedService obj = (SharingContracts.AnalyzedService)serializer.Deserialize(outStr, typeof(SharingContracts.AnalyzedService));
            if (obj == null)
            {
                return(new Tuple <bool, String>(false, "Service fails to be analyzed - " + outStr));
            }
            if (obj.publishParameters != null)
            {
                string respReturn = SerializeToString(obj.publishParameters);
                if (respReturn == "")
                {
                    return(new Tuple <bool, String>(false, "Service fails to be analyzed - " + outStr));
                }
                else
                {
                    return(new Tuple <bool, String>(true, respReturn));
                }
            }
            return(new Tuple <bool, String>(false, "Service fails to be analyzed - " + outStr));
        }
        private async Task <object> PublishService(string id, string title)
        {
            string userName   = GetLoggedInUser();
            string publishURL = String.Format("{0}/sharing/rest/content/users/{1}/publish", PortalManager.GetActivePortal(), userName);

            EsriHttpClient myClient          = new EsriHttpClient();
            string         publishParameters = string.Format("{{\"name\":\"{0}\"}}", title);
            var            postData          = new List <KeyValuePair <string, string> >();

            postData.Add(new KeyValuePair <string, string>("f", "json"));
            postData.Add(new KeyValuePair <string, string>("itemId", id));
            postData.Add(new KeyValuePair <string, string>("filetype", "vectortilepackage"));
            postData.Add(new KeyValuePair <string, string>("outputType", "VectorTiles"));
            postData.Add(new KeyValuePair <string, string>("publishParameters", publishParameters));
            HttpContent content = new FormUrlEncodedContent(postData);

            EsriHttpResponseMessage respMsg       = myClient.Post(publishURL, content);
            PublishResult           publishResult = JsonConvert.DeserializeObject <PublishResult>(await respMsg.Content.ReadAsStringAsync());

            if (publishResult.services.Count() == 1)
            {
                ShowProgress("Publish started", string.Format("Job id: {0}", publishResult.services[0].jobId), 3);
                string jobid     = publishResult.services[0].jobId;
                string jobStatus = "processing";
                int    i         = 0;
                while (jobStatus == "processing")
                {
                    i += 1;
                    Task.Delay(5000).Wait();
                    jobStatus = GetJobStatus(jobid, "publish", userName, publishResult.services[0].serviceItemId);
                    ShowProgress("Publish in progress", string.Format("Job status: {0} - {1}", jobStatus, i), 3);
                }
                ShowProgress("Publish in progress", string.Format("Job status: {0} - {1}", jobStatus, i), 3);
            }

            return(null);
        }
Beispiel #4
0
        public void UpdateItem(string userName, string itemId, string strTitle, List <string> requiredTags, List <string> tags)
        {
            string strCredits = "Basin Analysis GIS is developed under the collaboration between NRCS NWCC " +
                                "and the Center for Spatial Analysis &Research at Portland State University.";
            string strDescription = "This report was generated in Basin Analysis GIS (BAGIS). See the " +
                                    "<a href=\"https://nrcs.maps.arcgis.com/sharing/rest/content/items/b121d25cc73c4b30a700b8d2d2ea23bc/data\" " +
                                    "target=\"_blank\">Basin Analysis Report Users Manual</a> for a complete description of the report. Please contact NWCC " +
                                    "(<a href=\"https://www.wcc.nrcs.usda.gov/\" target=\"_blank\">https://www.wcc.nrcs.usda.gov/</a>) for any questions.";
            string strLicense = "Public domain data. See <a href='https://www.wcc.nrcs.usda.gov/disclaimer.htm' target='_blank' " +
                                "rel='nofollow ugc noopener noreferrer'>https://www.wcc.nrcs.usda.gov/disclaimer.htm</a> for disclaimer.";

            if (tags == null)
            {
                tags = new List <string>();  // Ensure tags is never null to avoid exception
            }
            List <string> mergedTags = requiredTags.Union(tags).ToList();
            string        strMerged  = string.Join(",", mergedTags);

            // Generate summary from title
            string strSummary = "";

            if (!string.IsNullOrEmpty(strTitle))
            {
                string[] pieces = strTitle.Split(new char[0]);
                if (pieces.Length > 0)
                {
                    strSummary = pieces[0];
                }
                if (pieces.Length > 1)
                {
                    for (int i = 1; i < pieces.Length; i++)
                    {
                        strSummary = strSummary + " " + pieces[i].ToUpper();
                    }
                }
            }

            // Updating fields on item
            UriBuilder searchURL = new UriBuilder(ArcGISPortalManager.Current.GetActivePortal().PortalUri);

            searchURL.Path = "sharing/rest/content/users/" + userName + "/items/" + itemId + "/update";
            EsriHttpClient myClient = new EsriHttpClient();
            var            postData = new List <KeyValuePair <string, string> >();

            postData.Add(new KeyValuePair <string, string>("f", "json"));
            postData.Add(new KeyValuePair <string, string>("description", strDescription));
            postData.Add(new KeyValuePair <string, string>("snippet", strSummary));
            postData.Add(new KeyValuePair <string, string>("licenseInfo", strLicense));
            postData.Add(new KeyValuePair <string, string>("accessInformation", strCredits));
            postData.Add(new KeyValuePair <string, string>("tags", strMerged));

            using (HttpContent content = new FormUrlEncodedContent(postData))
            {
                EsriHttpResponseMessage respMsg = myClient.Post(searchURL.Uri.ToString(), content);
                if (respMsg == null)
                {
                    return;
                }
                string outStr = respMsg.Content.ReadAsStringAsync().Result;
            }

            // Updating sharing for item
            searchURL.Path = "sharing/rest/content/users/" + userName + "/items/" + itemId + "/share";
            postData.Clear();
            postData.Add(new KeyValuePair <string, string>("f", "json"));
            postData.Add(new KeyValuePair <string, string>("everyone", "true"));
            postData.Add(new KeyValuePair <string, string>("groups", "a4474cec000e46869a9980930c7c9bd0"));
            using (HttpContent content = new FormUrlEncodedContent(postData))
            {
                EsriHttpResponseMessage respMsg = myClient.Post(searchURL.Uri.ToString(), content);
                if (respMsg == null)
                {
                    return;
                }
                string outStr = respMsg.Content.ReadAsStringAsync().Result;
            }
        }
        /// <summary>
        /// Post "publish" request on the portal item
        /// </summary>
        /// <param name="baseURI"></param>
        /// <param name="username"></param>
        /// <param name="itemId"></param>
        /// <param name="filetype"></param>
        /// <param name="publishParameters"></param>
        /// <returns></returns>
        Tuple<bool, string> publishService(string baseURI, string username, string itemId, string filetype, string publishParameters)
        {
            EsriHttpClient myClient = new EsriHttpClient();
            string requestUrl = baseURI + @"/sharing/rest/content/users/" + username + @"/publish";

            var postData = new List<KeyValuePair<string, string>>();
            postData.Add(new KeyValuePair<string, string>("f", "json"));
            postData.Add(new KeyValuePair<string, string>("itemId", itemId));
            postData.Add(new KeyValuePair<string, string>("filetype", filetype));
            postData.Add(new KeyValuePair<string, string>("publishParameters", publishParameters));
            HttpContent content = new FormUrlEncodedContent(postData);

            EsriHttpResponseMessage respMsg = myClient.Post(requestUrl, content);
            if(respMsg==null)
                return new Tuple<bool, String>(false, "HTTP response is null");

            string outStr = respMsg.Content.ReadAsStringAsync().Result;
            //Deserialize the response in JSON into a usable object. 
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            //Deserialize the response in JSON into a usable object. 
            PublishedServices obj = (PublishedServices)serializer.Deserialize(outStr, typeof(PublishedServices));
            if (obj == null)
                return new Tuple<bool, String>(false, "Service creation fails - " + outStr);

            string respReturn = "";
            foreach (PublishedService ps in obj.services)
                respReturn += ps.serviceurl;
            if (respReturn == "")
                return new Tuple<bool, String>(false, "Service creation fails - " + outStr);
            else
                return new Tuple<bool, String>(true, respReturn);

        }
        /// <summary>
        /// Post "analyze" request on the portal item
        /// </summary>
        /// <param name="baseURI"></param>
        /// <param name="itemId"></param>
        /// <param name="filetype"></param>
        /// <param name="analyzeParameters"></param>
        /// <returns></returns>
        Tuple<bool, string> analyzeService(string baseURI, string itemId, string filetype, string analyzeParameters)
        {
            EsriHttpClient myClient = new EsriHttpClient();
            string requestUrl = baseURI + @"/sharing/rest/content/features/analyze";

            var postData = new List<KeyValuePair<string, string>>();
            postData.Add(new KeyValuePair<string, string>("f", "json"));
            postData.Add(new KeyValuePair<string, string>("itemId", itemId));
            postData.Add(new KeyValuePair<string, string>("filetype", filetype));
            postData.Add(new KeyValuePair<string, string>("analyzeParameters", analyzeParameters));
            HttpContent content = new FormUrlEncodedContent(postData);

            EsriHttpResponseMessage respMsg = myClient.Post(requestUrl, content);
            if (respMsg == null)
                return new Tuple<bool, String>(false, "HTTP response is null");

            string outStr = respMsg.Content.ReadAsStringAsync().Result;
            //Deserialize the response in JSON into a usable object. 
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            //Deserialize the response in JSON into a usable object. 
            AnalyzedService obj = (AnalyzedService)serializer.Deserialize(outStr, typeof(AnalyzedService));
            if (obj == null)
                return new Tuple<bool, String>(false, "Service fails to be analyzed - " + outStr);
            if (obj.publishParameters != null)
            {
                string respReturn = SerializeToString(obj.publishParameters);
                if (respReturn == "")
                    return new Tuple<bool, String>(false, "Service fails to be analyzed - " + outStr);
                else
                    return new Tuple<bool, String>(true, respReturn);
            }
            return new Tuple<bool, String>(false, "Service fails to be analyzed - " + outStr);
        }