/// <summary> /// Gets the folder of a content item if the item is in a folder. /// </summary> void GetFolder(ContentItem item, EventHandler <RequestEventArgs> callback) { if (_agol.User.Current == null) { if (callback != null) { callback(null, new RequestEventArgs() { Error = new Exception(ESRI.ArcGIS.Mapping.Controls.ArcGISOnline.Resources.Strings.ExceptionGetFolderFailedUserSignedIn) }); } return; } //if the folder has already been determined call back immediately // if (item.Folder != null) { callback(null, new RequestEventArgs()); return; } //get the user's content and find the item // GetUserContent(null, (object sender, UserContentEventArgs e) => { if (e.Error != null) { callback(null, new RequestEventArgs() { Error = e.Error }); return; } UserContent userContent = e.Content; if (userContent == null) { callback(null, new RequestEventArgs() { Error = new Exception(string.Format(ESRI.ArcGIS.Mapping.Controls.ArcGISOnline.Resources.Strings.ItemNotFound, item.Id)) }); return; } //first check if the item is in the content root level // if (userContent.ContentItems != null) { foreach (ContentItem contentItem in userContent.ContentItems) { if (contentItem.Id == item.Id) { //create a folder that represents the root folder by assigning an empty id item.Folder = new ContentFolder() { Id = "" }; callback(null, new RequestEventArgs()); return; } } } //search folders for the content item if (userContent.Folders == null || userContent.Folders.Length == 0) { callback(null, new RequestEventArgs() { Error = new Exception(string.Format(ESRI.ArcGIS.Mapping.Controls.ArcGISOnline.Resources.Strings.ItemNotFound, item.Id)) }); } else { bool folderFound = false; int foldersToSearch = 0; int foldersSearched = 0; foreach (ContentFolder folder in userContent.Folders) { foldersToSearch++; ContentFolder folderToSearch = folder; //cache for reference in lambda expression GetUserContent(folderToSearch.Id, (object sender2, UserContentEventArgs e2) => { foldersSearched++; if (e2.Content != null && e2.Content.ContentItems != null) { foreach (ContentItem contentItem in e2.Content.ContentItems) { if (contentItem.Id == item.Id) { folderFound = true; //make sure callback is not invoked on subsequent responses item.Folder = folderToSearch; callback(null, new RequestEventArgs()); return; } } } if (foldersSearched == foldersToSearch && !folderFound) { callback(null, new RequestEventArgs() { Error = new Exception(string.Format(ESRI.ArcGIS.Mapping.Controls.ArcGISOnline.Resources.Strings.ItemNotFound, item.Id)) }); return; } }); } } }); }
public static bool IsSubfolder(ContentFolder folder) { return (folder != null && !string.IsNullOrEmpty(folder.Id)); }
/// <summary> /// Deletes the content item specified by its Id. /// </summary> public void Delete(ContentItem item, EventHandler <ContentItemEventArgs> callback) { if (_agol.User.Current == null) { if (callback != null) { callback(null, new ContentItemEventArgs() { Error = new Exception(ESRI.ArcGIS.Mapping.Controls.ArcGISOnline.Resources.Strings.ExceptionDeleteFailedUserSignIn), Id = item.Id }); } return; } //find out if the item is located in a folder // GetFolder(item, (object sender, RequestEventArgs e) => { if (e.Error != null) { if (callback != null) { callback(null, new ContentItemEventArgs() { Error = e.Error }); } return; } string folderUrl = ContentFolder.IsSubfolder(item.Folder) ? "/" + item.Folder.Id : ""; string url = _agol.Url + "/content/users/" + _agol.User.Current.Username + folderUrl + "/items/" + item.Id + "/delete?f=json&token=" + _agol.User.Token; WebUtil.UploadStringAsync(url, null, "", (object sender2, UploadStringCompletedEventArgs e2) => { if (e2.Error != null) { if (callback != null) { callback(null, new ContentItemEventArgs() { Error = e2.Error, Id = item.Id }); } return; } Success result = WebUtil.ReadObject <Success>(new MemoryStream(Encoding.UTF8.GetBytes(e2.Result))); ContentItemEventArgs eventArgs = new ContentItemEventArgs() { Error = (result == null || !result.Succeeded) ? new Exception(ESRI.ArcGIS.Mapping.Controls.ArcGISOnline.Resources.Strings.ExceptionDeleteFailed) : null, Id = item.Id }; if (callback != null) { callback(null, eventArgs); } //raise event if (ItemDeleted != null) { ItemDeleted(null, eventArgs); } }); }); }