by

Working with Referance Attachments in EWS on Trade On-line

Working with Referance Attachments in EWS on Trade On-line Final yr a brand new characteristic was launched into Trade On-line to permit you to create an Attachment the place the Attachment resided on OneDrive somewhat then the normal E mail attachment method you’ll be able to learn extra about this right here http://blogs.workplace.com/2014/10/08/introducing-new-way-share-files-outlook-web-app/ there was additionally an Ignite session you’ll be able to watch on how this has developed https://channel9.msdn.com/Occasions/Ignite/2015/BRK2196. MAPI has at all times had a wealthy set of attachment varieties since Trade’s inception to cope with Wealthy Textual content Messaging eg the assorted attachment varieties you’ll be able to encounter are listed in https://msdn.microsoft.com/en-us/library/workplace/cc815439.aspx  . In EWS these attachment varieties the place simplified some what into both Merchandise Attachments (for Connected Retailer Gadgets) or File Attachments. To cater for this new characteristic a brand new Attachment Sort known as a
ReferenceAttachment has been launched into EWS in Trade On-line. To see the
ReferenceAttachmentType it’s good to have your requested Server model set to Trade2003_SP1 else the attachment will simply be returned as a FileAttachment .  Important that is what you will notice in a EWS Response when you will have a reference attachment

 1
2
3
4
5
6
7
8
9
10
11
12
13
                  <t:ReferenceAttachment>
<t:AttachmentId Id="AAMkADczNDE4YWE...." />
<t:Identify>lb.txt</t:Identify>
<t:ContentType>picture/png</t:ContentType>
<t:ContentId>7cc92386-bee2-4db8-ae81-d17dad9350f8</t:ContentId>
<t:Dimension>598</t:Dimension>
<t:LastModifiedTime>2015-05-08T06:39:22</t:LastModifiedTime>
<t:IsInline>false</t:IsInline>
<t:AttachLongPathName>https://domain-my.sharepoint.com/private/gscales_datarumble_com/Paperwork/E mail%20attachments/lb.txt</t:AttachLongPathName>
<t:ProviderType>OneDrivePro</t:ProviderType>
<t:PermissionType>2</t:PermissionType>
<t:AttachmentIsFolder>false</t:AttachmentIsFolder>
</t:ReferenceAttachment>

In case your utilizing Proxy code and you have up to date your WSDL proxies try to be ready to make use of the  ReferenceAttachmentType to entry the AttachLongPathName property and so forth. In case your utilizing the EWS Managed API there at the moment is not any assist for Reference Attachments within the final launched model or the Open Supply Repo https://github.com/OfficeDev/ews-managed-api . So I’ve forked the open supply repo and added assist in for this (observe its learn solely assist) into my GitHub repo https://github.com/gscales/ews-managed-api . For those who simply need the binaries https://github.com/gscales/EWS-Binaries/blob/grasp/RAFork-Microsoft.Trade.WebServices.zip Notice that is patched construct not a steady construct so use it at your individual threat.

To make use of this in code with the CSOM to download the Attachment from One drive appears like

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
            FindItemsResults<Merchandise> fiResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(1));
Merchandise Message = fiResults.Gadgets[0];
Message.Load();
foreach (Attachment Attachment in Message.Attachments)
{
if (Attachment is ReferenceAttachment)
{
ReferenceAttachment rfAttach = (ReferenceAttachment)Attachment;
Console.WriteLine(rfAttach.ProviderType);
Console.WriteLine(rfAttach.AttachLongPathName);

var credentials = new Microsoft.SharePoint.Shopper.SharePointOnlineCredentials(username, securedPassword);
Uri url = new Uri(rfAttach.AttachLongPathName);
Microsoft.SharePoint.Shopper.ClientContext clientContext = new Microsoft.SharePoint.Shopper.ClientContext("https://" + url.Host);
clientContext.Credentials = credentials;
Microsoft.SharePoint.Shopper.FileInformation fileInfo = Microsoft.SharePoint.Shopper.File.OpenBinaryDirect(clientContext, url.LocalPath);
FileStream fs = new FileStream("c:temp" + rfAttach.Identify, FileMode.Create);
fileInfo.Stream.CopyTo(fs);
fs.Flush();
fs.Shut();

}

}

Or in a Script that may download the final e-mail within the Mailbox and download any reference attachments from that message I’ve put a download of this script right here

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
####################### 
<#
.SYNOPSIS
Download any OneDrive Attachments on the final message obtained in a Mailbox in Trade On-line

.DESCRIPTION
Utilizing the EWS Managed API and the Sharepoint CSOM this scripts downloads the any OneDrive Attachments
on the final message obtained in a Mailbox in Trade On-line

Requires Forked model of the EWS Managed API dll from
https://github.com/gscales/EWS-Binaries/blob/grasp/RAFork-Microsoft.Trade.WebServices.zip
Requires Sharepoint CSOM

.EXAMPLE
PS C:>Download-LastOneDriveAttachment -MailboxName consumer.title@area.com -FilePath c:temp

This instance downloads any OneDrive Attachments on the final message obtained in a Mailbox to the c:temp listing
#>
operate Download-LastOneDriveAttachment
{
[CmdletBinding()]
param(
[Parameter(Place=0, Necessary=$true)] [string]$MailboxName,
[Parameter(Place=0, Necessary=$true)] [string]$FilePath
)
Start
{
$SharePointClientDll = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path 'Registry::HKEY_LOCAL_MACHINESOFTWAREMicrosoftSharePoint Shopper Elements'|Type-Object Identify -Descending| Choose-Object -First 1 -ExpandProperty Identify)).'Location') + "ISAPIMicrosoft.SharePoint.Client.dll")
Add-Sort -Path $SharePointClientDll

## Load Managed API dll observe forked model to assist referance attachments
$EWSDLL = "c:tempMicrosoft.Exchange.WebServices.dll"
Import-Module $EWSDLL

## Set Trade Model
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Trade2013_SP1

## Create Trade Service Object
$service = New-Object Microsoft.Trade.WebServices.Information.ExchangeService($ExchangeVersion)

## Set Credentials to make use of two choices are availible Option1 to make use of explict credentials or Possibility 2 use the Default (logged On) credentials

#Credentials Possibility 1 utilizing UPN for the home windows Account
$psCred = Get-Credential
$creds = New-Object System.Web.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
$soCredentials = New-Object Microsoft.SharePoint.Shopper.SharePointOnlineCredentials($psCred.UserName.ToString(),$psCred.password)
$service.Credentials = $creds
#Credentials Possibility 2
#service.UseDefaultCredentials = $true

## Select to disregard any SSL Warning points brought on by Self Signed Certificates

## Code From http://poshcode.org/624
## Create a compilation surroundings
$Supplier=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Supplier.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null

$TASource=@'
namespace Native.ToolkitExtensions.Web.CertificatePolicy{
public class TrustAll : System.Web.ICertificatePolicy {
public TrustAll() {
}
public bool CheckValidationResult(System.Web.ServicePoint sp,
System.Safety.Cryptography.X509Certificates.X509Certificates cert,
System.Web.WebRequest req, int downside) {
return true;
}
}
}
'@
$TAResults=$Supplier.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly

## We now create an occasion of the TrustAll and fasten it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll

## finish code from http://poshcode.org/624

## Set the URL of the CAS (Shopper Entry Server) to make use of two choices are availbe to make use of Autodiscover to search out the CAS URL or Hardcode the CAS to make use of

#CAS URL Possibility 1 Autodiscover
$service.AutodiscoverUrl($MailboxName,{$true})
"Using CAS Server : " + $Service.url

#CAS URL Possibility 2 Hardcoded

#$uri=[system.URI] "https://casservername/ews/exchange.asmx"
#$service.Url = $uri

## Non-obligatory part for Trade Impersonation
}
Course of
{
# Bind to the Inbox Folder
$folderid= new-object Microsoft.Trade.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
#Outline ItemView to retrive simply 1000 Gadgets
$ivItemView = New-Object Microsoft.Trade.WebServices.Information.ItemView(1)
$psPropset= new-object Microsoft.Trade.WebServices.Information.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$fiItems = $service.FindItems($Inbox.Id,$ivItemView)
[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)
foreach($Merchandise in $fiItems.Gadgets){
Write-Host ("Processing : " + $Merchandise.Topic)
foreach($Attachment in $Merchandise.Attachments){
if($Attachment -is [Microsoft.Exchange.WebServices.Data.ReferenceAttachment]){
$DownloadURI = New-Object System.Uri($Attachment.AttachLongPathName);
$SharepointHost = "https://" + $DownloadURI.Host
$clientContext = New-Object Microsoft.SharePoint.Shopper.ClientContext($SharepointHost)
$clientContext.Credentials = $soCredentials;
$destFile = $FilePath + "" + [System.IO.Path]::GetFileName($DownloadURI.LocalPath)
$fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($clientContext, $DownloadURI.LocalPath);
$fstream = New-Object System.IO.FileStream($destFile, [System.IO.FileMode]::Create);
$fileInfo.Stream.CopyTo($fstream)
$fstream.Flush()
$fstream.Shut()
Write-Host ("File downloaded to " + ($destFile))
}
}
}
}
Finish{}
}