WriteToXml method

void WriteToXml(
  1. EwsServiceXmlWriter writer
)
Writes XML. The writer.

Implementation

void WriteToXml(EwsServiceXmlWriter writer) {
  writer.WriteStartElement(
    XmlNamespace.Soap,
    XmlElementNames.SOAPEnvelopeElementName,
  );
  writer.WriteAttributeValueWithPrefix(
    "xmlns",
    EwsUtilities.EwsXmlSchemaInstanceNamespacePrefix,
    EwsUtilities.EwsXmlSchemaInstanceNamespace,
  );
  writer.WriteAttributeValueWithPrefix(
    "xmlns",
    EwsUtilities.EwsMessagesNamespacePrefix,
    EwsUtilities.EwsMessagesNamespace,
  );
  writer.WriteAttributeValueWithPrefix(
    "xmlns",
    EwsUtilities.EwsTypesNamespacePrefix,
    EwsUtilities.EwsTypesNamespace,
  );
  if (writer.RequireWSSecurityUtilityNamespace) {
    writer.WriteAttributeValueWithPrefix(
      "xmlns",
      EwsUtilities.WSSecurityUtilityNamespacePrefix,
      EwsUtilities.WSSecurityUtilityNamespace,
    );
  }

  writer.WriteStartElement(
    XmlNamespace.Soap,
    XmlElementNames.SOAPHeaderElementName,
  );

  if (Service.Credentials != null) {
    Service.Credentials!.EmitExtraSoapHeaderNamespaceAliases(
      writer.InternalWriter,
    );
  }

  // Emit the RequestServerVersion header
  if (!Service.SuppressXmlVersionHeader) {
    writer.WriteStartElement(
      XmlNamespace.Types,
      XmlElementNames.RequestServerVersion,
    );
    writer.WriteAttributeValue(
      XmlAttributeNames.Version,
      _GetRequestedServiceVersionString(),
    );
    writer.WriteEndElement(); // RequestServerVersion
  }

  // Against Exchange 2007 SP1, we always emit the simplified time zone header. It adds very little to
  // the request, so bandwidth consumption is not an issue. Against Exchange 2010 and above, we emit
  // the full time zone header but only when the request actually needs it.
  //
  // The exception to this is if we are in Exchange2007 Compat Mode, in which case we should never emit
  // the header.  (Note: Exchange2007 Compat Mode is enabled for testability purposes only.)
  //
  if ((Service.RequestedServerVersion == ExchangeVersion.Exchange2007_SP1 ||
          EmitTimeZoneHeader) &&
      (!Service.Exchange2007CompatibilityMode) &&
      // A Dart service has no time zone unless one is set (C# always has one,
      // TimeZoneInfo.Local by default). Without one, the header is omitted and
      // the server reads unbiased date/times as UTC.
      Service.TimeZone != null) {
    writer.WriteStartElement(
      XmlNamespace.Types,
      XmlElementNames.TimeZoneContext,
    );

    Service.TimeZoneDefinition.WriteToXmlElementName(writer);

    writer.WriteEndElement(); // TimeZoneContext

    writer.IsTimeZoneHeaderEmitted = true;
  }

  // Emit the MailboxCulture header
  // todo("restore this features")
  //            if (this.Service.PreferredCulture != null)
  //            {
  //                writer.WriteElementValueWithNamespace(
  //                    XmlNamespace.Types,
  //                    XmlElementNames.MailboxCulture,
  //                    this.Service.PreferredCulture.Name);
  //            }

  // Emit the DateTimePrecision header
  if (Service.DateTimePrecision != DateTimePrecision.Default) {
    writer.WriteElementValueWithNamespace(
      XmlNamespace.Types,
      XmlElementNames.DateTimePrecision,
      EnumToString.parse(Service.DateTimePrecision),
    );
  }

  // Emit the ExchangeImpersonation header
  if (Service.ImpersonatedUserId != null) {
    Service.ImpersonatedUserId!.WriteToXml(writer);
  } else if (Service.PrivilegedUserId != null) {
    Service.PrivilegedUserId!.WriteToXml(
      writer,
      Service.RequestedServerVersion,
    );
  } else if (Service.ManagementRoles != null) {
    Service.ManagementRoles!.WriteToXml(writer);
  }

  if (Service.Credentials != null) {
    Service.Credentials!.SerializeExtraSoapHeaders(
      writer.InternalWriter,
      GetXmlElementName(),
    );
  }

  Service.DoOnSerializeCustomSoapHeaders(writer.InternalWriter);

  writer.WriteEndElement(); // soap:Header

  writer.WriteStartElement(
    XmlNamespace.Soap,
    XmlElementNames.SOAPBodyElementName,
  );

  WriteBodyToXml(writer);

  writer.WriteEndElement(); // soap:Body
  writer.WriteEndElement(); // soap:Envelope
}