Chapter163: Microsoft.Exchange.WebServices

No Comments

Section163.1:RetrieveSpecifiedUser’sOutofOfficeSettings

Firstlet’screateanExchangeManagerobject,wheretheconstructorwillconnecttotheservicesforus.Italsohasa

GetOofSettingsmethod,which will returnthe OofSettingsobject forthe specifiedemail address:

usingSystem;

usingSystem.Web.Configuration;

usingMicrosoft.Exchange.WebServices.Data;

namespaceSetOutOfOffice

{

classExchangeManager

{

privateExchangeServiceService;

publicExchangeManager()

{

varpassword=WebConfigurationManager.ConnectionStrings[“Password”].ConnectionString; Connect(“exchangeadmin”,password);

}

privatevoidConnect(stringusername,stringpassword)

{

varservice=newExchangeService(ExchangeVersion.Exchange2010_SP2); service.Credentials=newWebCredentials(username,password); service.AutodiscoverUrl(“autodiscoveremail@domain.com”,

RedirectionUrlValidationCallback);

Service=service;

}

privatestaticboolRedirectionUrlValidationCallback(stringredirectionUrl)

{

returnredirectionUrl.Equals(“https://mail.domain.com/autodiscover/autodiscover.xml”);

}

publicOofSettingsGetOofSettings(stringemail)

{

returnService.GetUserOofSettings(email);

}

}

}

Wecannowcallthiselsewherelikethis:

varem=newExchangeManager();

varoofSettings=em.GetOofSettings(“testemail@domain.com”);

Section163.2:UpdateSpecificUser’sOutofOfficeSettings

Usingtheclassbelow,wecanconnecttoExchangeandthensetaspecificuser’soutofofficesettingswith

UpdateUserOof:

usingSystem;

usingSystem.Web.Configuration;

usingMicrosoft.Exchange.WebServices.Data;

classExchangeManager

{

privateExchangeServiceService;

publicExchangeManager()

{

varpassword=WebConfigurationManager.ConnectionStrings[“Password”].ConnectionString; Connect(“exchangeadmin”,password);

}

privatevoidConnect(stringusername,stringpassword)

{

varservice=newExchangeService(ExchangeVersion.Exchange2010_SP2); service.Credentials=newWebCredentials(username,password);

service.AutodiscoverUrl(“autodiscoveremail@domain.com”,RedirectionUrlValidationCallback);

Service=service;

}

privatestaticboolRedirectionUrlValidationCallback(stringredirectionUrl)

{

returnredirectionUrl.Equals(“https://mail.domain.com/autodiscover/autodiscover.xml”);

}

///<summary>

///Updatesthegivenuser’sOofsettingswiththegivendetails

///</summary>

publicvoidUpdateUserOof(intoofstate,DateTimestarttime,DateTimeendtime,int externalaudience,stringinternalmsg,stringexternalmsg,stringemailaddress)

{

varnewSettings=newOofSettings

{

State=(OofState)oofstate,

Duration=newTimeWindow(starttime,endtime),

ExternalAudience=(OofExternalAudience)externalaudience,Internal

Reply=internalmsg,

ExternalReply=externalmsg

};

Service.SetUserOofSettings(emailaddress,newSettings);

}

}

Updatetheusersettingswiththefollowing:

varoofState=1;

varstartDate=newDateTime(01,08,2016);

varendDate=newDateTime(15,08,2016);

varexternalAudience=1;

varinternalMessage=”Iamnotintheoffice!”;

varexternalMessage=”Iamnotintheoffice<strong>andneitherareyou!</strong>”vartheUser=”theuser@domain.com”;

varem=newExchangeManager();

em.UpdateUserOof(oofstate,startDate,endDate,externalAudience,internalMessage,externalMessage, theUser);

Notethatyoucanformatthemessagesusingstandardhtmltags.

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts

Leave a Comment