How to create task to other users task folder in exchange server.


I was doing some task related to creating¬† task to other users task folder in exchange server. Tried with lots of way but fail, finally got a way by using “Microsoft Exchange Server 2010 Exchange Web Services “.

Basically what I did is, there will be a user in exchange server who will have the permission to impersonate other users in exchange server, then the user will impersonate a user and will create the task to the users task list. And the task will be available in the sercond user’s task list in outlook or any other email client.

The details about the excahnge web services are available in http://msdn.microsoft.com/en-us/library/dd877037(v=EXCHG.140).aspx

The code of the task creation is given below :

Download exchange web service API from http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1 and add refference to the project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Exchange.WebServices;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.Data;
using System.IO;

private static ExchangeService GetExchangeService()
{
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationMethod;

ExchangeService oService = new ExchangeService();

#region Using Auto discover url

//oService.Credentials = new WebCredentials(“mainUser”, “Pass@123”, “domain”);

//oService.AutodiscoverUrl(“mainUser@Domain.com”);
#endregion Using Auto discover url
#region Using exchange URL
oService.Credentials =¬† new WebCredentials(“mainUser”, “Pass@123”, “domain”);

oService.Url = new Uri(“https://domain.com/ews/Exchange.asmx”);
#endregion Using exchange URL

return oService;
}

private void CreateTask() ////Main method
{
ExchangeService oService = GetExchangeService();

Task oTask = new Task(oService);
oService.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, “userTowhomtasktocreate@domain.com”);

oTask.ActualWork = 10;

oTask.Body = txtBody.Text;

oTask.Importance = Importance.High;
oTask.StartDate =  DateTime.Today;
oTask.Status = TaskStatus.InProgress;
oTask.Subject = txtSubject.Text;
Recurrence.WeeklyPattern weeklyPattern = new Recurrence.WeeklyPattern(DateTime.Today, 2, new DayOfTheWeek[] { DayOfTheWeek.Saturday, DayOfTheWeek.Wednesday });
oTask.Recurrence = weeklyPattern;
oTask.IsReminderSet = true;
oTask.ReminderDueBy = new DateTime(2010, 12, 5, 11, 05, 05);
oTask.PercentComplete = 11.10;
oTask.Attachments.AddFileAttachment(“TestAttachment”, File.ReadAllBytes(@”C:\temp.txt”));

StringList lstCategory = new StringList();
lstCategory.Add( “RED Category”);

lstCategory.Add( “BLACK Category”);
lstCategory.Add(“BLUE Category”);
oTask.Categories = lstCategory;

oTask.Save();
}

private static bool CertificateValidationMethod(

object sender,
System.Security.Cryptography.X509Certificates.

X509Certificate certificate,
System.Security.Cryptography.X509Certificates.

X509Chain chain,
System.Net.Security.

SslPolicyErrors sslPolicyErrors)
{

// If the certificate is a valid, signed certificate, return true.

if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
{

return true;
}

// If there are errors in the certificate chain, look at each error to determine the cause.

if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
{

if (chain != null && chain.ChainStatus != null)
{

foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
{

if ((certificate.Subject == certificate.Issuer) &&
(status.Status == System.Security.Cryptography.X509Certificates.

X509ChainStatusFlags.UntrustedRoot))
{

// Self-signed certificates with an untrusted root are valid.

continue;
}

else
{

if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
{

// If there are any other errors in the certificate chain, the certificate is invalid,

// so the method returns false.

return false;
}

}

}

}

// When processing reaches this line, the only errors in the certificate chain are

// untrusted root errors for self-signed certificates. These certificates are valid

// for default Exchange server installations, so return true.

return true;
}

else
{

// In all other cases, return false.

return false;
}

}