- Leverage the solution created earlier in this chapter and add a class called UpdateActivity.
- Import the following namespaces:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using Packt.Xrm.Entities;
using Microsoft.Crm.Sdk.Messages;
- Initialize a service context and IOrganizationService at the class scope level:
OrganisationServiceContext _organizationContext;
IOrganizationService _organizationService;
- Add a GetEmails method that takes a Guid as a parameter:
public IEnumerable<Email> GetEmails(Guid parentEntityId)
{
var query = from email in _organizationContext.EmailSet
where email.RegardingObjectId.Id == parentEntityId
&&
email.StateCode == EmailState.Open
select new Email
{
Id = email.Id,
Subject = email.Subject
};
return query.ToList();
}
- Add a closeEmailAsCancelled method that takes an e-mail as a parameter:
public void CloseEmailAsCancelled(Email email)
{
email.StateCode = EmailState.Canceled;
var setStateRequest = new SetStateRequest()
{
Status = new OptionSetValue((int)email_statuscode.Canceled),
State = new OptionSetValue((int)EmailState.Canceled),
EntityMoniker = new EntityReference(Email.EntityLogicalName, email.Id)
};
_organizationContext.Execute(setStateRequest);
}
- Add an UpdateEntity method that takes an entity as a parameter:
public void UpdateEntity(Entity entity)
{
_organizationContext.UpdateObject(entity);
}
- Add a Commit() method that calls the SaveChanges on the context:
public void Commit()
{
_organizationContext.SaveChanges();
}