exchangelib.services.create_folder API documentation

# Module `exchangelib.services.create_folder`

Expand source code

    from ..errors import ErrorFolderExists
    from ..util import MNS, create_element
    from .common import EWSAccountService, folder_ids_element, parse_folder_elem, set_xml_value

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

        SERVICE_NAME = "CreateFolder"
        element_container_name = f"{{{MNS}}}Folders"
        ERRORS_TO_CATCH_IN_RESPONSE = EWSAccountService.ERRORS_TO_CATCH_IN_RESPONSE + (ErrorFolderExists,)

        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.folders = []  # A hack to communicate parsing args to _elems_to_objs()

        def call(self, parent_folder, folders):
            # We can't easily find the correct folder class from the returned XML. Instead, return objects with the same
            # class as the folder instance it was requested with.
            self.folders = list(folders)  # Convert to a list, in case 'folders' is a generator. We're iterating twice.
            return self._elems_to_objs(
                self._chunked_get_elements(
                    self.get_payload,
                    items=self.folders,
                    parent_folder=parent_folder,
                )
            )

        def _elems_to_objs(self, elems):
            for folder, elem in zip(self.folders, elems):
                if isinstance(elem, Exception):
                    yield elem
                    continue
                yield parse_folder_elem(elem=elem, folder=folder)

        def get_payload(self, folders, parent_folder):
            payload = create_element(f"m:{self.SERVICE_NAME}")
            payload.append(
                folder_ids_element(folders=[parent_folder], version=self.account.version, tag="m:ParentFolderId")
            )
            folder_elems = create_element("m:Folders")
            for folder in folders:
                set_xml_value(folder_elems, folder, version=self.account.version)
            payload.append(folder_elems)
            return payload

## Classes

`class CreateFolder (*args, **kwargs)`

  MSDN: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/createfolder-operation

  Expand source code

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

          SERVICE_NAME = "CreateFolder"
          element_container_name = f"{{{MNS}}}Folders"
          ERRORS_TO_CATCH_IN_RESPONSE = EWSAccountService.ERRORS_TO_CATCH_IN_RESPONSE + (ErrorFolderExists,)

          def __init__(self, *args, **kwargs):
              super().__init__(*args, **kwargs)
              self.folders = []  # A hack to communicate parsing args to _elems_to_objs()

          def call(self, parent_folder, folders):
              # We can't easily find the correct folder class from the returned XML. Instead, return objects with the same
              # class as the folder instance it was requested with.
              self.folders = list(folders)  # Convert to a list, in case 'folders' is a generator. We're iterating twice.
              return self._elems_to_objs(
                  self._chunked_get_elements(
                      self.get_payload,
                      items=self.folders,
                      parent_folder=parent_folder,
                  )
              )

          def _elems_to_objs(self, elems):
              for folder, elem in zip(self.folders, elems):
                  if isinstance(elem, Exception):
                      yield elem
                      continue
                  yield parse_folder_elem(elem=elem, folder=folder)

          def get_payload(self, folders, parent_folder):
              payload = create_element(f"m:{self.SERVICE_NAME}")
              payload.append(
                  folder_ids_element(folders=[parent_folder], version=self.account.version, tag="m:ParentFolderId")
              )
              folder_elems = create_element("m:Folders")
              for folder in folders:
                  set_xml_value(folder_elems, folder, version=self.account.version)
              payload.append(folder_elems)
              return payload

  ### Ancestors

  - EWSAccountService
  - EWSService
  - SupportedVersionClassMixIn

  ### Class variables

  `var ERRORS_TO_CATCH_IN_RESPONSE`
  `var SERVICE_NAME`
  `var element_container_name`

  ### Methods

  `def call(self, parent_folder, folders)`
    Expand source code

        def call(self, parent_folder, folders):
            # We can't easily find the correct folder class from the returned XML. Instead, return objects with the same
            # class as the folder instance it was requested with.
            self.folders = list(folders)  # Convert to a list, in case 'folders' is a generator. We're iterating twice.
            return self._elems_to_objs(
                self._chunked_get_elements(
                    self.get_payload,
                    items=self.folders,
                    parent_folder=parent_folder,
                )
            )

  `def get_payload(self, folders, parent_folder)`
    Expand source code

        def get_payload(self, folders, parent_folder):
            payload = create_element(f"m:{self.SERVICE_NAME}")
            payload.append(
                folder_ids_element(folders=[parent_folder], version=self.account.version, tag="m:ParentFolderId")
            )
            folder_elems = create_element("m:Folders")
            for folder in folders:
                set_xml_value(folder_elems, folder, version=self.account.version)
            payload.append(folder_elems)
            return payload

  ### Inherited members

  - `EWSAccountService`:
    - `NO_VALID_SERVER_VERSIONS`
    - `WARNINGS_TO_CATCH_IN_RESPONSE`
    - `get`
    - `parse`
    - `supported_api_versions`
    - `wrap`