public XrmServices()
 {
     try
     {
         CrmConnection connect =
             CrmConnection.Parse("Url=" + ConfigurationManager.AppSettings["UrlCrm"] + "; Username="******"Login"] + "; Password="******"Password"]);
         this._service        = new OrganizationService(connect);
         this._serviceContext = new OrganizationServiceContext(this._service);
         this.callerId        = SystemUser.GetUserFromLogin(ConfigurationManager.AppSettings["Login"], this) ??
                                new EntityReference();
         _serverUrl = ConfigurationManager.AppSettings["UrlCrm"];
     }
     catch (Exception e)
     {
         throw new Exception("Erreur de connexion à la CRM : \n" + e.Message);
     }
 }
        public XElement UpdateEmailByExchangeId(string messageId, string domainName = "", string userName = "")
        {
            //return new XElement("root");
            //return new XElement("");
            //string resultMessage = string.Empty;
            bool ownerChanged = false;

            try
            {
                crmService = new XrmServices();
                string          user  = domainName + "\\" + userName;
                EntityReference email = Activity.GetEmailById(messageId, crmService);

                if (email != null && email.Id != Guid.Empty)
                {
                    //Activity.ReopenActivity(email, crmService);
                    XrmUtility.ChangeOwner(email, SystemUser.GetUserFromLogin(user, crmService), crmService);
                    Entity updateEmail = new Entity("email");
                    updateEmail.Id = email.Id;
                    //Status du mail à "A traiter"
                    updateEmail.Attributes.Add("new_status", new OptionSetValue(100000000));
                    crmService.Update(updateEmail);
                    //Activity.CloseActivity(email, crmService);
                    ownerChanged = true;
                    // resultMessage = XrmUtility.FormatUrl(email.Id, email.LogicalName.ToLower());
                }
                else
                {
                    returnCode = (int)CustomReturnCode.MailNotFound;
                }

                result.SetError(returnCode, customError.getErrorMessage(returnCode));
            }
            catch (Exception ex)
            {
                result.SetError((int)CustomReturnCode.Unknown, ex.Message, ex);
            }
            result.AddResultElement("value", ownerChanged);
            return(result.getXml((Method == "post") ? new List <string> {
                messageId, domainName, userName
            } : null));
            //return resultMessage;
        }