/// <summary> /// Gets the content of the current user. /// <remarks> /// If folderId is null gets the content from the root level, otherwise the content in the specified folder. /// </remarks> /// </summary> /// <param name="folderId">The id of the folder to retrive content from. If null the root level content is retrieved.</param> /// <param name="callback"></param> void GetUserContent(string folderId, EventHandler <UserContentEventArgs> callback) { string url = _agol.Url + "content/users/" + _agol.User.Current.Username + (string.IsNullOrEmpty(folderId) ? "" : "/" + folderId) + "?f=json&token=" + _agol.User.Token; // add a bogus parameter to avoid the caching that happens with the WebClient // url += "&tickCount=" + Environment.TickCount.ToString(); WebUtil.OpenReadAsync(url, null, (sender, e) => { if (e.Error != null) { callback(null, new UserContentEventArgs() { Error = e.Error }); return; } UserContent userContent = WebUtil.ReadObject <UserContent>(e.Result); callback(null, new UserContentEventArgs() { Content = userContent }); }); }
/// <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; } }); } } }); }