public JsonAddItemResponse AddItem(AddItemParameters addItemParams, string folderId, string token) { try { if (folderId != String.Empty) folderId += "/"; //TODO: figure out how to generate the URL //01a1484edcb14e0d99cb5b1266e9657b Uri url = new Uri(string.Format("http://www.arcgis.com/sharing/content/users/{0}/{1}addItem?f=json&token={2}", userCredentials.Username, folderId, token)); HttpPostRequest request = new HttpPostRequest(url); if (addItemParams.File != String.Empty) { request.AddFile("file", addItemParams.File, addItemParams.Filename); } request.AddFields(addItemParams.GetParameters()); HttpWebResponse response = request.PostData(); StreamReader sr = new StreamReader(response.GetResponseStream()); JsonAddItemResponse jsonAddItemResponse = JsonConvert.DeserializeObject<JsonAddItemResponse>(sr.ReadToEnd()); //TODO: do this error-checking better if (!jsonAddItemResponse.success) { throw new Exception(); } return jsonAddItemResponse; } catch (Exception e) { throw e; } }
static void Main(string[] args) { //ENTER CREDENTIALS #region credentials userCreds.Username = ""; userCreds.Password = ""; userCreds.Referer = ""; #endregion // Console.WriteLine("Authenticating user\n"); AGOLClient client = new AGOLClient(userCreds); TokenParameters tokenParams = new TokenParameters(); tokenParams.Username = userCreds.Username; tokenParams.Password = userCreds.Password; tokenParams.Expiration = "540"; tokenParams.Referer = userCreds.Referer; token = client.GenerateToken(tokenParams); foreach (string arg in args) { Console.WriteLine("Uploading shapefile"); AddItemParameters addItemParams = new AddItemParameters(); addItemParams.ItemInfoParameters.Type = "Shapefile"; addItemParams.File = arg; string name = arg.Substring(arg.LastIndexOf('\\') + 1); addItemParams.Filename = name; addItemParams.Async = true; addItemParams.ItemInfoParameters.Title = name.Substring(0, name.Length-4); addItemParams.ItemInfoParameters.Tags = "bulkupload"; JsonAddItemResponse addItemResponse = client.AddItem(addItemParams, token); if (addItemResponse.success) { Console.WriteLine(string.Format("{0}\nid {1}\nPreparing item for publishing...\n", addItemResponse.item, addItemResponse.id)); StatusManager statusManager = new StatusManager(userCreds, token); statusManager.CheckStatusCompletedEvent += new CheckStatusCompletedEventHandler(CheckStatusCompletedEventHandler); Task t = statusManager.CheckStatusAsync(addItemResponse.id); } else { Console.WriteLine(string.Format("Upload failed {0}\n", addItemResponse.id)); } } //press Esc to quit while (true) { if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape) break; } }
static void Main(string[] args) { if (args.Length > 0) { #region credentials //ENTER CREDENTIALS userCreds.Username = ""; userCreds.Password = ""; userCreds.Referer = ""; #endregion // Console.WriteLine("Authenticating user"); AGOLClient client = new AGOLClient(userCreds); TokenParameters tokenParams = new TokenParameters(); tokenParams.Username = userCreds.Username; tokenParams.Password = userCreds.Password; tokenParams.Expiration = "540"; tokenParams.Referer = userCreds.Referer; token = client.GenerateToken(tokenParams); string file = args[0]; string filename = file.Substring(file.LastIndexOf('\\') + 1); Console.WriteLine("Uploading shapefile"); AddItemParameters addItemParams = new AddItemParameters(); addItemParams.ItemInfoParameters.Type = "Shapefile"; addItemParams.File = file; addItemParams.Filename = filename; addItemParams.Async = true; addItemParams.ItemInfoParameters.Title = filename.Substring(0, filename.Length - 4); addItemParams.ItemInfoParameters.Tags = filename.Substring(0, filename.Length - 4); JsonAddItemResponse addItemResponse = client.AddItem(addItemParams, token); if (addItemResponse.success) { Console.WriteLine(string.Format("{0}\n", addItemResponse.id)); Console.WriteLine("Preparing item for publishing..."); StatusManager statusManager = new StatusManager(userCreds, token); statusManager.CheckStatusCompletedEvent += new CheckStatusCompletedEventHandler(CheckStatusCompletedEventHandler); //TODO: make a synchronous version of CheckStatus(), in case the user wants a blocking call Task t = statusManager.CheckStatusAsync(addItemResponse.id); } else { Console.WriteLine(string.Format("Upload failed {0}\n", addItemResponse.id)); } } else { Console.WriteLine("Please specify a shapefile as an input parameter at runtime"); } //press Esc to quit while (true) { if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape) break; } }
static void Main(string[] args) { //ENTER CREDENTIALS userCreds.Username = ""; userCreds.Password = ""; userCreds.Referer = ""; // Console.WriteLine("Authenticating user\n"); AGOLClient client = new AGOLClient(userCreds); TokenParameters tokenParams = new TokenParameters(); tokenParams.Username = userCreds.Username; tokenParams.Password = userCreds.Password; tokenParams.Expiration = "540"; tokenParams.Referer = userCreds.Referer; token = client.GenerateToken(tokenParams); foreach (string arg in args) { Console.WriteLine(string.Format("Processing {0}\n", arg)); AddItemParameters addItemParams = new AddItemParameters(); string[] names = arg.Split('/'); string name = string.Format("{0} ({1})", names[names.Length - 2], names[names.Length - 1]); //TODO: create proper lookup to do this string type = String.Empty; switch (names[names.Length - 1].ToLower()) { case "featureserver": type = "Feature Service"; break; case "mapserver": type = "Map Service"; break; case "imageserver": type = "Image Service"; break; case "gpserver": type = "Geoprocessing Service"; break; case "geometryserver": type = "Geometry Service"; break; } addItemParams.Url = arg; addItemParams.ItemInfoParameters.Type = type; addItemParams.Async = true; addItemParams.ItemInfoParameters.Title = name; addItemParams.ItemInfoParameters.Tags = "index"; Console.WriteLine("Indexing service with ArcGIS Online\n"); JsonAddItemResponse addItemResponse = client.AddItem(addItemParams, token); if (addItemResponse.success) { Console.WriteLine(string.Format("Submitted {0}\nid {1}\n", addItemResponse.item, addItemResponse.id)); } else { Console.WriteLine(string.Format("Error indexing item {0}\n", addItemResponse.id)); } } Console.WriteLine("\nPress Esc to quit"); //press Esc to quit while (true) { if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape) break; } }
private static void QueryArcGISServer(string server, string directory, JsonFolderResponse parentDirectory) { Tools tools = new Tools(); JsonArcGISServerResponse agsResponse = tools.QueryArcGISServer(server, directory); foreach (string folder in agsResponse.folders) { Console.WriteLine(string.Format("====================\n\nCreating folder: {0}", folder)); CreateFolderParameters folderParams = new CreateFolderParameters(); folderParams.Title = folder; AGOLClient client = new AGOLClient(userCreds); JsonFolderResponse response = client.CreateFolder(folderParams, token); folderIds.Add(response.folder.id); //recursive Console.WriteLine(string.Format("Recursing {0} for services\n\n", folder)); QueryArcGISServer(server, folder, response); } foreach (JsonArcGISServerService service in agsResponse.services) { Console.WriteLine(string.Format("Indexing service {0}", service.name)); AddItemParameters addItemParams = new AddItemParameters(); //TODO: create proper lookup to do this string type = String.Empty; switch (service.type.ToLower()) { case "featureserver": type = "Feature Service"; break; case "mapserver": type = "Map Service"; break; case "imageserver": type = "Image Service"; break; case "gpserver": type = "Geoprocessing Service"; break; case "geometryserver": type = "Geometry Service"; break; case "geocodeserver": type = "Geocoding Service"; break; } if (!server.EndsWith("/")) server += "/"; addItemParams.Url = String.Format("{0}{1}/{2}", server, service.name, service.type); addItemParams.ItemInfoParameters.Type = type; addItemParams.Async = true; addItemParams.ItemInfoParameters.Title = directory == String.Empty ? service.name : service.name.Substring(directory.Length+1); addItemParams.ItemInfoParameters.Tags = "bulkindex"; AGOLClient client = new AGOLClient(userCreds); JsonAddItemResponse addItemResponse = null; if (parentDirectory == null) { addItemResponse = client.AddItem(addItemParams, token); itemIds.Add(addItemResponse.id); } else { addItemResponse = client.AddItem(addItemParams, parentDirectory.folder.id, token); } Console.WriteLine(string.Format("Completed {0}\n", addItemResponse.item)); } }
public JsonAddItemResponse AddItem(AddItemParameters addItemParams, string token) { return AddItem(addItemParams, String.Empty, token); }