Hi,
I have been searching for a way to filter birthdays in CRM and found a plugin from CRM MVP Darren Liu, which would perfectly fit to our needs:
http://blogs.msdn.com/b/crm/archive/2009/02/27/creating-a-birthday-contact-list.aspx
We'll I tried to build it in Visual Studio but suddenly recognized that the code is for CRM 4, so I need to rewrite parts of the plugin, because e.g. the data types changed from CRM 4 to CRM 2011 (to the .NET data types).
Then I registered the plugin for create and update, entity contact as pre-operation steps.
The plugin works fine. It writes the birthdate to the three new fields when creating a new contact, or when updating the birthdate for a contact.
BUT: I need to restrict the plugin that it doesn't run for contacts with a certain ownerid. Unfortunately I didn't manage to write that code, because I just started writing plugins (based on SDK samples) and I didn't study development.
The plugin shall not run, if the owner of the contact is = "04700952-321B-E111-B232-005056830021" (-> that's the GUID of the owner).
I tried it like that:
Entity current = service.Retrieve("entityname", context.PrimaryEntityId, new ColumnSet("ownerid"));
EntityReference OwnerId = (EntityReference)current["ownerid"];
if (entity.Attributes.Contains("birthdate") && OwnerId.ToString() != "04700952-321B-E111-B232-005056830021")
--> but that didn't work out and threw an exception
It would be really great if somebody could help me out with that issue :)
thx a lot in advance
Thomas
That's my current source code (without the restriction):
using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace BirthdayPlugin
{
public class BirthdayField : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
tracingService.Trace("EntityName:" + entity.LogicalName);
// Verify that the target entity represents a contact.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName == "contact")
{
//Check if birthdate is filled in CRM and if new birthday fields are empty. If yes --> copy to new birthday/month/year fields
if (entity.Attributes.Contains("birthdate"))
{
//Neue Variable die den Geburtstag eines Contacts enthält
DateTime birthday = (DateTime) entity["birthdate"];
//Update bzw. Befüllung der 3 neuen Geburtstagsfelder am Contact
entity ["new_birthday"] = birthday.ToLocalTime().Day;
entity ["new_birthmonth"] = new OptionSetValue (birthday.ToLocalTime().Month);
entity["new_birthyear"] = birthday.ToLocalTime().Year;
}
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in Birthday plugin", ex);
}
}
}
}
*This post is locked for comments