By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

Creating a new contact in Exchange

P: n/a
How can I create a new contact in exchange server from a vb.net web
application?
Sep 26 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hello Gerhard,

As for creating a new contact in exchange, based on my experience, there're
two possible appraoches:

1. use Outlook COM component to automate the create contact task

2. use exchange WebDav protocol to send http request to create contact

For your scenario, since you're developing in ASP.NET environment,
server-side COM automation is not recommended. I think you may consider
using HttpWebRequest component to send WebDav request to create contact
under a certain user's Contacts directory. Here is a KB article which
provide some code snippet on this:

#How to create a Contact item by using the HttpWebRequest class and the
HttpWebResponse class in Visual Basic .NET
http://support.microsoft.com/kb/314190/en-us

and below is my local test code (adjust according to the above kb article):

==========test code============
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim text1 As String =
"http://exchange_server_name/Exchange/username/Contacts/new_contact_name.eml
"
Dim uri1 As New Uri(text1)
Dim request1 As HttpWebRequest =
DirectCast(WebRequest.Create(uri1), HttpWebRequest)
Dim text2 As String = "xmlns:g=""DAV:""
xmlns:c=""urn:schemas:contacts:""
xmlns:e=""http://schemas.microsoft.com/exchange/""
xmlns:mapi=""http://schemas.microsoft.com/mapi/"" xmlns:x=""xml:""
xmlns:cal=""urn:schemas:calendar:"" xmlns:mail=""urn:schemas:httpmail:"">"
Dim text3 As String =
"<c:givenName>JoLynn</c:givenName><c:middlename>Julie</c:middlename><c:sn>Do
bney</c:sn><c:cn>JoLynn J. Dobney</c:cn><mail:subject>JoLynn
Dobney</mail:subject><c:fileas>Dobney,
JoLynn</c:fileas><c:initials>JJD</c:initials><c:nickname>Jo</c:nickname><c:p
ersonaltitle>Mrs.</c:personaltitle><c:namesuffix>MCSD</c:namesuffix>"
Dim text4 As String = "<c:street>Business Address
Information</c:street><c:postofficebox>PO Box
12345</c:postofficebox><c:l>AnyTown</c:l><c:st>WA</c:st><c:postalcode>12345-
1234</c:postalcode><c:co>United States</c:co>"
Dim text5 As String = "<c:homeStreet>Home Address
Information</c:homeStreet><c:homepostofficebox>PO Box
54321</c:homepostofficebox><c:homeCity>SomeTown</c:homeCity><c:homeState>WA<
/c:homeState><c:homePostalCode>12345</c:homePostalCode><c:homeCountry>United
States</c:homeCountry>"
Dim text6 As String = "<c:otherstreet>Other Address
Information</c:otherstreet><c:otherpostofficebox>PO Box
98765</c:otherpostofficebox><c:othercity>SomeTown</c:othercity><c:otherstate
>FL</c:otherstate><c:otherpostalcode>56789</c:otherpostalcode><c:othercountr
y>United States</c:othercountry>"
Dim text7 As String =
"<c:mailingaddressid>2</c:mailingaddressid>"
Dim text8 As String =
"<c:telephoneNumber>425-555-0110</c:telephoneNumber><c:telephonenumber2>425-
555-0111</c:telephonenumber2><c:facsimiletelephonenumber>425-555-0112</c:fac
similetelephonenumber><c:homePhone>425-555-0113</c:homePhone><c:homephone2>4
25-555-0114</c:homephone2><c:homefax>425-555-0115</c:homefax><c:otherTelepho
ne>206-555-0111</c:otherTelephone><c:otherfax>206-555-0112</c:otherfax><c:pa
ger>425-555-0116</c:pager><c:mobile>425-555-0117</c:mobile><c:othermobile>20
6-555-0113</c:othermobile><c:internationalisdnnumber>425-555-0118</c:interna
tionalisdnnumber><c:telexnumber>425-555-0119</c:telexnumber><c:ttytddphone>4
25-555-0120</c:ttytddphone><c:callbackphone>425-555-0121</c:callbackphone>"
Dim text9 As String =
"<mapi:email1addrtype>EX</mapi:email1addrtype><mapi:email1emailaddress>/o=Mi
crosoft/ou=First Administrative
Group/cn=Recipients/cn=jdobney</mapi:email1emailaddress><mapi:email1original
displayname>JoLynn Dobney
(Exchange)</mapi:email1originaldisplayname><mapi:email2addrtyp e>SMTP</mapi:e
mail2addrtype><mapi:email2emailaddress>Jo********* **@example.com</mapi:email
2emailaddress><mapi:email2originaldisplayname>JoLy nn Dobney
(SMTP)</mapi:email2originaldisplayname><mapi:email3addrtyp e>X400</mapi:email
3addrtype><mapi:email3emailaddress>c=us;a= ; p=Microsoft; o=Exchange;
s=Dobney; g=JoLynn;
i=J;</mapi:email3emailaddress><mapi:email3originaldispla yname>JoLynn Dobney
(X400)</mapi:email3originaldisplayname>"
Dim text10 As String = "<c:o>Fourth Coffee
</c:o><c:organizationmainphone>425-555-0199</c:organizationmainphone><c:busi
nesshomepage>http://www.fourthcoffee.com</c:businesshomepage><c:department>Y
YY</c:department><c:title>Lead Software Design
Engineer</c:title><c:manager>Karan Khanna</c:manager><c:secretarycn>Ken
Myer</c:secretarycn><c:secretaryphone>425-555-0123</c:secretaryphone><c:room
number>C-309</c:roomnumber><c:employeenumber>987654321</c:employeenumber><c:
computernetworkname>jdobney</c:computernetworkname><c:profession>Software
Designer</c:profession>"
Dim text11 As String =
"<c:bday>1974-01-01T08:00:00Z</c:bday><c:weddinganniversary>1995-01-01T08:00
:00Z</c:weddinganniversary><c:spousecn>Barry
Johnson</c:spousecn><c:childrensnames><x:v>David Johnson</x:v><x:v>Willis
Johnson</x:v></c:childrensnames><c:gender>Male</c:gender><c:personalHomePage
>http://www.example.com/JoLynnDobney/default.htm</c:personalHomePage><c:hobb
ies>Reading, lawn bowling</c:hobbies>"
Dim text12 As String =
"<c:customerid>YYY</c:customerid><c:account>YYY</c:account><c:billinginforma
tion>YYY</c:billinginformation>"
Dim text13 As String =
"<mapi:reminderset>1</mapi:reminderset><mapi:request>Call</mapi:request><map
i:remindertime>2001-12-01T08:00:00Z</mapi:remindertime><mapi:remindernexttim
e>2001-12-01T08:00:00Z</mapi:remindernexttime><e:reply-by-iso>2001-12-01T08:
00:00Z</e:reply-by-iso>"
Dim text14 As String =
"<e:keywords-utf8><x:v>Buddies</x:v><x:v>Engineers</x:v></e:keywords-utf8><m
api:contacts><x:v>Brian Johnson</x:v></mapi:contacts><e:mileage>Rarely used
string
property</e:mileage><c:ftpsite>ftp://ftp.example.com/</c:ftpsite><c:language
>US
English</c:language><c:governmentid>000-00-0000</c:governmentid><c:location>
Nowhere
Land</c:location><cal:fburl>http://www.example.com/JoLynnDobney/freebusy</ca
l:fburl><mapi:sensitivity>2</mapi:sensitivity>"
Dim text15 As String = "<e:extensionattribute1>User Data
1</e:extensionattribute1><e:extensionattribute2>User Data
2</e:extensionattribute2><e:extensionattribute3>User Data
3</e:extensionattribute3><e:extensionattribute4>User Data
4</e:extensionattribute4>"
Dim text16 As String = String.Concat(New String() {"<?xml
version='1.0'?><g:propertyupdate ", text2,
"<g:set><g:prop><g:contentclass>urn:content-classes:person</g:contentclass><
e:outlookmessageclass>IPM.Contact</e:outlookmessageclass>", text3, text4,
text5, text6, text7, text8, text9, text10, text11, text12, text13, text14,
text15, "</g:prop></g:set></g:propertyupdate>"})
request1.Credentials = CredentialCache.DefaultCredentials
request1.KeepAlive = False
request1.Headers.Set("Pragma", "no-cache")
request1.Headers.Set("Translate", "f")
request1.ContentType = "text/xml"
request1.ContentLength = text16.Length
request1.Timeout = 300000
request1.Method = "PROPPATCH"
Dim buffer1 As Byte() = Encoding.ASCII.GetBytes(text16)
request1.ContentLength = buffer1.Length
Dim stream1 As Stream = request1.GetRequestStream
stream1.Write(buffer1, 0, buffer1.Length)
stream1.Close()
Dim response1 As HttpWebResponse =
DirectCast(request1.GetResponse, HttpWebResponse)
Dim text17 As String = response1.StatusCode.ToString
Me.textBox1.Text = (Me.textBox1.Text & ChrW(13) & ChrW(10) &
"Status Code: {0}" & text17)
Dim text18 As String = request1.Headers.ToString
Me.textBox1.Text = (Me.textBox1.Text & ChrW(13) & ChrW(10) &
"headers: " & text18)
Dim stream2 As Stream = response1.GetResponseStream
Dim text19 As String = New StreamReader(stream2).ReadToEnd
Me.textBox1.Text = (Me.textBox1.Text & ChrW(13) & ChrW(10) &
"Response: " & text19)
stream2.Close()
request1 = Nothing
response1 = Nothing
stream1 = Nothing
stream2 = Nothing
Catch exception1 As Exception
Me.textBox1.Text = ("Exception:" & ChrW(13) & ChrW(10) & " " &
exception1.ToString)
End Try
End Sub
========================================

And you can also find similar C# code in the below site:

#Visual C# .NET 2003 KB Article(s)
http://www.kbalertz.com/technology_213_4.aspx
Hope this also helps. If you have anything unclear, please feel free to let
me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 27 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.