How to do it...

  1. Leverage the solution created earlier in this chapter and add a class called UpdateActivity.
  2. 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;
  1. Initialize a service context and IOrganizationService at the class scope level:
OrganisationServiceContext _organizationContext;
IOrganizationService _organizationService;
  1. 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();
}
  1. 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);
}
  1. Add an UpdateEntity method that takes an entity as a parameter:
public void UpdateEntity(Entity entity) 
{
_organizationContext.UpdateObject(entity);
}
  1. Add a Commit() method that calls the SaveChanges on the context:
public void Commit() 
{
_organizationContext.SaveChanges();
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.102.124