Beispiel #1
0
        /// <summary>
        /// Removes an entry (document or folder) from all folders.
        /// </summary>
        /// <param name="entryToBeRemoved">A Document object representing the entry (document or folder) that will be removed from all folders.</param>
        public static void RemoveEntryFromAllFolders(Document entryToBeRemoved)
        {
            Working = true;

            Document updatedEntry;

            try
            {
                if (entryToBeRemoved.ParentFolders.Count == 0)
                {
                    Working = false;
                    return;
                }

                _documentService.ProtocolMajor = 3;

                // we will loop through all of the entry's parent folders
                foreach (var folder in entryToBeRemoved.ParentFolders)
                {
                    // To move a document or folder out of a folder, we need to send a HTTP DELETE request
                    // to the destination folder's edit link. The edit link will have a folder id and a document id,
                    // represented by folder_id and document_id respectively in the example below.
                    // DELETE /feeds/default/private/full/folder%3A<folder_id>/contents/document%3A<document_id>

                    var uri = new Uri(string.Format("{0}/contents/{1}", folder, entryToBeRemoved.ResourceId));
                    _documentService.Delete(uri, "*");
                }

                // in order to "empty" Document.ParentFolders, we'll update the whole document
                updatedEntry = GetUpdatedDocument(entryToBeRemoved);
            }
            catch (GDataRequestException exRequest)
            {
                var error = GetErrorMessage(exRequest);
                if (exRequest.ResponseString == null && error.ToLowerInvariant().Contains("execution of request failed"))
                {
                    throw new GDataRequestException("Couldn't remove entry from all folders - internet down?");
                }

                Trace.WriteLine(DateTime.Now + " - NocsService - couldn't remove entry from all folders: " + error);
                throw new GDataRequestException(string.Format("Couldn't remove entry from all folders: {0} - {1}", entryToBeRemoved.DocumentEntry.Title.Text, Tools.TrimErrorMessage(error)));
            }
            catch (Exception ex)
            {
                var error = GetErrorMessage(ex);
                throw new Exception(string.Format("Couldn't remove entry from all folders: {0} - {1}", entryToBeRemoved.DocumentEntry.Title.Text, error));
            }
            finally
            {
                Working = false;
            }

            // let's update dictionaries
            if (updatedEntry != null)
            {
                if (updatedEntry.Type == Document.DocumentType.Folder)
                {
                    AllFolders[entryToBeRemoved.ResourceId] = updatedEntry;
                }
                else
                {
                    AllDocuments[entryToBeRemoved.ResourceId] = updatedEntry;
                }
            }
        }