Here is my plugin code.. where runs on create (post operation) .
what i am doing is to change the status of emails where the sender (from) is "<Consumers>".
CODE :
================================================================
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GRID.CRM.Plugins.Email.SetStatusOnCreat
{
public class SetEmailStatus : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
//Initialize the service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.MessageName != "Create")
{
return;
}
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null && context.InputParameters["Target"] is Entity)
{
tracingService.Trace("Inside IF statement => " + context.InputParameters["Target"].ToString());
Entity EmailEntity = (Entity)context.InputParameters["Target"];
tracingService.Trace("Email GUID Retrieved : " + EmailEntity.Id);
if (EmailEntity.LogicalName != "email")
{
return;
}
tracingService.Trace("Email entity GUID : " + EmailEntity.Id);
Guid EmailId = EmailEntity.Id;
Entity Email = service.Retrieve("email", EmailId, new ColumnSet(true));
tracingService.Trace("Email Retrieved entity GUID : " + Email.Id);
tracingService.Trace("Email Retrieved entity logical name : " + Email.LogicalName);
tracingService.Trace("Email entity Party Id : " + (((EntityCollection)(Email.Attributes["from"])).Entities[0].Attributes["partyid"]));
if (Email != null)
{
string SenderName = ((EntityReference)(((EntityCollection)(Email.Attributes["from"])).Entities[0].Attributes["partyid"])).Name.ToString();
tracingService.Trace("Sender Name : " + SenderName);
if (SenderName == "<Consumers>")
{
tracingService.Trace("BEFORE : Email state code : " + Email.Attributes["statecode"]);
tracingService.Trace("BEFORE : Email status code : " + Email.Attributes["statuscode"]);
Email.Attributes["statecode"] = 1;
Email.Attributes["statuscode"] = 3;
service.Update(Email);
tracingService.Trace("Updated Email entity");
}
}
else
{
tracingService.Trace("attribute does not exist");
throw new Exception("attribute does not exist");
}
}
else
{
tracingService.Trace("else part : no entity found");
throw new Exception("else part : no entity found");
}
}
catch (Exception ex)
{
throw new Exception("Exception occured in Plugin SetEmailStatus at \n" + ex.Source + "\n" + ex.TargetSite +
"Message: \n" + ex.Message + Environment.NewLine +
"Inner Exception: \n" + ex.InnerException +
"Stack Trace: \n" + ex.StackTrace
);
}
}
}
}
===================================================================
Exception Error is below :
===================================================================
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Exception occured in Plugin SetEmailStatus at
mscorlib
T get_Item(Int32)Message:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Inner Exception:
Stack Trace:
at System.Collections.Generic.List`1.get_Item(Int32 index)
at GRID.CRM.Plugins.Email.SetStatusOnCreat.SetEmailStatus.Execute(IServiceProvider serviceProvider) in d:\office\GRIDCRMProjects\Plugins\GRID.CRM.Plugins.Email.SetStatusOnCreat\SetEmailStatus.cs:line 48Detail:
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
<ErrorCode>-2147220891</ErrorCode>
<ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<KeyValuePairOfstringanyType>
<d2p1:key>OperationStatus</d2p1:key>
<d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
</KeyValuePairOfstringanyType>
<KeyValuePairOfstringanyType>
<d2p1:key>SubErrorCode</d2p1:key>
<d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">-2146233088</d2p1:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<Message>Exception occured in Plugin SetEmailStatus at
mscorlib
T get_Item(Int32)Message:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Inner Exception:
Stack Trace:
at System.Collections.Generic.List`1.get_Item(Int32 index)
at GRID.CRM.Plugins.Email.SetStatusOnCreat.SetEmailStatus.Execute(IServiceProvider serviceProvider) in d:\office\GRIDCRMProjects\Plugins\GRID.CRM.Plugins.Email.SetStatusOnCreat\SetEmailStatus.cs:line 48</Message>
<Timestamp>2014-12-13T16:16:41.1302399Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText>
[GRID.CRM.Plugins.Email.SetStatusOnCreat: GRID.CRM.Plugins.Email.SetStatusOnCreat.SetEmailStatus]
[b7e8b942-e182-e411-a310-000c299c44ab: GRID.CRM.Plugins.Email.SetStatusOnCreat.SetEmailStatus: Create of email]
Inside IF statement => Microsoft.Xrm.Sdk.Entity
Email GUID Retrieved : b6308f67-e382-e411-a310-000c299c44ab
Email entity GUID : b6308f67-e382-e411-a310-000c299c44ab
Email Retrieved entity GUID : b6308f67-e382-e411-a310-000c299c44ab
Email Retrieved entity logical name : email
</TraceText>
</OrganizationServiceFault>