Skip to content

Commit

Permalink
Implement MoveFolder and add folder.move()
Browse files Browse the repository at this point in the history
  • Loading branch information
Halla Moore authored and hallamoore committed Jun 24, 2020
1 parent c412274 commit c0acb58
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
5 changes: 4 additions & 1 deletion exchangelib/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from .queryset import QuerySet, SearchableMixIn
from .restriction import Restriction
from .services import FindFolder, GetFolder, FindItem, CreateFolder, UpdateFolder, DeleteFolder, EmptyFolder, FindPeople, \
SyncFolderItems
SyncFolderItems, MoveFolder
from .util import TNS, MNS
from .version import EXCHANGE_2007_SP1, EXCHANGE_2010_SP1, EXCHANGE_2013, EXCHANGE_2013_SP1

Expand Down Expand Up @@ -742,6 +742,9 @@ def empty(self, delete_type=HARD_DELETE, delete_sub_folders=False):
# We don't know exactly what was deleted, so invalidate the entire folder cache to be safe
self.account.root.clear_cache()

def move(self, to_folder):
return MoveFolder(account=self.account).call(folders=[self], to_folder=to_folder)

def wipe(self):
# Recursively deletes all items in this folder, and all subfolders and their content. Attempts to protect
# distinguished folders from being deleted. Use with caution!
Expand Down
34 changes: 34 additions & 0 deletions exchangelib/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,40 @@ def _get_elements_in_response(self, response):
yield c


class MoveFolder(EWSAccountService):
"""
https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/movefolder-operation
"""

SERVICE_NAME = "MoveFolder"
element_container_name = None

def call(self, folders, to_folder):
return list(self._get_elements(payload=self.get_payload(folders, to_folder)))

def get_payload(self, folders, to_folder):
from .folders import Folder, FolderId, DistinguishedFolderId

payload = create_element("m:{}".format(self.SERVICE_NAME))

to_folder_elem = create_element("m:ToFolderId")
if not isinstance(to_folder, (Folder, FolderId, DistinguishedFolderId)):
to_folder = to_item_id(to_folder, FolderId)
set_xml_value(to_folder_elem, to_folder, version=self.account.version)

folder_ids = create_element("m:FolderIds")
for folder in folders:
if not isinstance(folder, (Folder, FolderId, DistinguishedFolderId)):
folder = to_item_id(folder, FolderId)
set_xml_value(folder_ids, folder, version=self.account.version)
if not len(folder_ids):
raise ValueError('"folders" must not be empty')

payload.append(to_folder_elem)
payload.append(folder_ids)
return payload


def to_item_id(item, item_cls):
# Coerce a tuple, dict or object to an 'item_cls' instance. Used to create [Parent][Item|Folder]Id instances from a
# variety of input.
Expand Down

0 comments on commit c0acb58

Please sign in to comment.