Skip to content

Instantly share code, notes, and snippets.

@jfoclpf
Last active March 4, 2022 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfoclpf/6891ed91df57b4c0d6f749cd143d1b50 to your computer and use it in GitHub Desktop.
Save jfoclpf/6891ed91df57b4c0d6f749cd143d1b50 to your computer and use it in GitHub Desktop.
Get public Distribution List email addresses from Microsoft EWS with Javascript, for MS Office JS API
// this will get an Array of Email addresses from Public Distribution List stored in Exchange Server
getDLFromEws('publiclist@example.com', (err, res) => {
if (!err) {
console.log(res)
}
})
function getDLFromEws (emailAddress, callback) {
const DLRequest = generateDistributionListSoapRequest(emailAddress)
console.log('request:', prettifyXml(DLRequest))
if (validateXML(DLRequest)) {
Office.context.mailbox.makeEwsRequestAsync(DLRequest, function (result) {
console.log('response:', result.value)
const res = $.parseXML(result.value)
const emailAddresses = res.getElementsByTagName('t:EmailAddress')
const emails = []
for (const ele of emailAddresses) {
emails.push(ele.textContent)
}
callback(null, emails)
})
} else {
console.error('invalid XML', prettifyXml(DLRequest))
callback(Error('invalid XML'))
}
}
// generate distribution list SOAP request
function generateDistributionListSoapRequest (emailAddress) {
const result =
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"' +
' xmlns:xsd="https://www.w3.org/2001/XMLSchema"' +
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
' <soap:Body>' +
' <ExpandDL xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"' +
' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
' <Mailbox>' +
' <t:EmailAddress>' + emailAddress + '</t:EmailAddress>' +
' </Mailbox>' +
' </ExpandDL>' +
' </soap:Body>' +
'</soap:Envelope>'
return result
}
function prettifyXml (sourceXml) {
const xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml')
const xsltDoc = new DOMParser().parseFromString([
// describes how we want to modify the XML - indent everything
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">',
' <xsl:strip-space elements="*"/>',
' <xsl:template match="para[content-style][not(text())]">', // change to just text() to strip space in text nodes
' <xsl:value-of select="normalize-space(.)"/>',
' </xsl:template>',
' <xsl:template match="node()|@*">',
' <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>',
' </xsl:template>',
' <xsl:output indent="yes"/>',
'</xsl:stylesheet>',
].join('\n'), 'application/xml')
const xsltProcessor = new XSLTProcessor()
xsltProcessor.importStylesheet(xsltDoc)
const resultDoc = xsltProcessor.transformToDocument(xmlDoc)
const resultXml = new XMLSerializer().serializeToString(resultDoc)
return resultXml
}
function validateXML (xmlString) {
const domParser = new DOMParser()
const dom = domParser.parseFromString(xmlString, 'text/xml')
return dom.documentElement.nodeName !== 'parsererror'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment