Category Archives: .net

Handle birthdays in Microsoft CRM 2011/2013

The birthday field in Microsoft Dynamics CRM in the contact entity is nice, but if you want to see the birthdays today or even want to have the birthdays in your calendar you have to spend some effort. Some solutions split the birthday-date in several fields for processing => day, month. Or they use a workflow to set the next birthday.

Here I want to describe a new approach. Why don’t we use the recurring appointment for displaying the birthdates. This is a standard entity and synchronized with outlook.

Basically it is a plugin. Additionally you add a lookupfield to the contact entity to have a reference to the master recurring appointment entity.

Birthday PluginSample

 

In your code replace this name with your custom attributename:

private string appointment_birthday_attribute_name = "soho_birthday";

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Linq;
using Microsoft.Xrm.Sdk.Query;

namespace CRM_2011_Plugins
{
    public class soho_birthday : IPlugin
    {
        public enum RecurrenceRulePatternEndType
        {
            NoEndDate = 1,
            Occurrences = 2,
            PatternEndDate = 3
        };

        public enum RecurrencePatternTypes
        {
            Daily = 0,
            Weekly = 1,
            Monthly = 2,
            Yearly = 3
        };

        public enum DayOfWeek
        {
            Sunday = 0x01,
            Monday = 0x02,
            Tuesday = 0x04,
            Wednesday = 0x08,
            Thursday = 0x10,
            Friday = 0x20,
            Saturday = 0x40
        };

        private string appointment_birthday_attribute_name = "soho_birthday";

        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));

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName == "contact" && entity.Attributes.Contains("birthdate"))
                {
                    //  if the entity is created => create the recurring appointment and update your lookupfield with the created master
                    if (context.MessageName == "Create")
                    {
                        Entity newRecurringAppointment = this.createRecurringAppointment(entity, context.UserId);
                        Guid recurringappointmentmasterID = service.Create(newRecurringAppointment);
                        Entity cUpdate = new Entity("contact");
                        cUpdate.Id = entity.Id;
                        cUpdate.Attributes.Add(appointment_birthday_attribute_name, new EntityReference("recurringappointmentmaster", recurringappointmentmasterID));
                        service.Update(cUpdate);
                    }
                    // If the entity is updated get the "old" contact
                    if (context.MessageName == "Update")
                    {
                        if (entity.LogicalName == "contact" && entity.Attributes.Contains("birthdate"))
                        {
                            Entity oldContact = service.Retrieve("contact", entity.Id, new ColumnSet(new string[] { "fullname", "ownerid", appointment_birthday_attribute_name }));
                            // if a birthday is set to null it means the birthday filed was deleted. 
                            //In this case we delete the recurring appointment
                            if (entity.Attributes["birthdate"] != null)
                            {
                                // we need the contacts fullname for the subject. so if we do not update the field we get it from the "old" contact
                                if (!entity.Attributes.Contains("fullname"))
                                    entity.Attributes.Add("fullname", oldContact["fullname"]);

                                Guid? OwnerID = null;
                                if (entity.Attributes.Contains("ownerid"))
                                {
                                    OwnerID = ((EntityReference)entity["ownerid"]).Id;
                                }
                                else
                                {
                                    OwnerID = ((EntityReference)oldContact["ownerid"]).Id;
                                }

                                // Create a new recurringappointment
                                Entity newRecurringAppointment = this.createRecurringAppointment(entity, OwnerID);

                                // If there is a existing recurringappointment pointing to birthday update this entity, else create a new one
                                if (oldContact.Attributes.Contains(appointment_birthday_attribute_name))
                                {
                                    newRecurringAppointment.Id = ((EntityReference)oldContact[appointment_birthday_attribute_name]).Id;
                                    service.Update(newRecurringAppointment);
                                    entity[appointment_birthday_attribute_name] = new EntityReference("recurringappointmentmaster", newRecurringAppointment.Id);
                                }
                                else
                                {
                                    Guid recurringappointmentmasterID = service.Create(newRecurringAppointment);
                                    entity.Attributes.Add(appointment_birthday_attribute_name, new EntityReference("recurringappointmentmaster", recurringappointmentmasterID));
                                }

                            }
                            else
                            {
                                if (oldContact.Attributes.Contains(appointment_birthday_attribute_name))
                                {
                                    service.Delete("recurringappointmentmaster", ((EntityReference)oldContact[appointment_birthday_attribute_name]).Id);
                                    entity[appointment_birthday_attribute_name] = null;
                                }
                            }
                        }
                    }
                }
            }
        }

        // Creates an recurring appointment
        public Entity createRecurringAppointment(Entity entity, Guid? OwnerID)
        {
            // if the contact has an owner use his id to create a required attendee
            Guid? ownerid = null;
            if (entity.Attributes.Contains("ownerid"))
            {
                ownerid = ((EntityReference)entity["ownerid"]).Id;
            }
            else
            {
                if (OwnerID != null)
                {
                    ownerid = OwnerID.Value; ;
                }
            }

            // Create a recurring appointment
            Entity newRecurringAppointment = new Entity("recurringappointmentmaster");
            DateTime birthday = (DateTime)entity["birthdate"];
            // its a allday event
            newRecurringAppointment.Attributes.Add("isalldayevent", true);
            if (entity.Attributes.Contains("fullname"))
            {
                newRecurringAppointment.Attributes.Add("subject", "Geburtstag von " + entity["fullname"].ToString());
            }
            // set the reagrding filed to the contact
            newRecurringAppointment.Attributes.Add("regardingobjectid", new EntityReference(entity.LogicalName, entity.Id));
            // Birthday is recurring once a year
            newRecurringAppointment.Attributes.Add("recurrencepatterntype", new OptionSetValue((int)RecurrencePatternTypes.Yearly));
            // start the pattern today
            newRecurringAppointment.Attributes.Add("patternstartdate", DateTime.Today);
            // Dont stop the recurring appointment at a certain date
            newRecurringAppointment.Attributes.Add("patternendtype", new OptionSetValue((int)RecurrenceRulePatternEndType.NoEndDate));
            // every year => interval = 1
            newRecurringAppointment.Attributes.Add("interval", 1);
            // Set the day (zero-based!)
            newRecurringAppointment.Attributes.Add("dayofmonth", birthday.Day + 1);
            // set the month
            newRecurringAppointment.Attributes.Add("monthofyear", new OptionSetValue(birthday.Month));
            // pretty useless because its a all day event, but if this is not set, the start and end-date were set incorrectly
            newRecurringAppointment.Attributes.Add("starttime", DateTime.Now);
            newRecurringAppointment.Attributes.Add("endtime", DateTime.Now.AddHours(1));

            // set the required attendee
            if (ownerid != null)
            {
                Entity activityparty = new Entity("activityparty");
                activityparty.Attributes.Add("partyid", new EntityReference("systemuser", ownerid.Value));
                newRecurringAppointment.Attributes.Add("requiredattendees", new Entity[] { activityparty });
            }
            return newRecurringAppointment;
        }
    }
}

Thats it. Compile, Register, Enjoy…

 

PS: WHat I forgot to mention.

Register the Create / Contact Step als Postcreate and the Update / Contact Step as PreUpdate :-)

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Microsoft Dynamics CRM: Simple File Logging for Plugins (and Workflows)

Well, there are many logging tools out there, but most of them depend on external assemblies. It is (not impossible but) more or less difficult to implement them in plugins.

So I decided to write a very simple file logger for myself. And this is also the tool I use to demonstrate what happens within the execution of plugins in my “Plugin messages – deep dive”. I tested it with Dynamics CRM 2013, but it should work the same way with every other version!

The Logger

The XrmLogger class is very easy to understand. There is a XrmLoggerConfig class where you specify where to write the files and the name you like your logs to have. The filename is a combination of a string and the date. In our Example the name would be “2013-10-09-XrmLoggerDemo.log”. Every day a new file is created. But you can change this very easily.

XrmLoggerConfig config = new XrmLoggerConfig()
            {
                LogDirectoryName = @"C:\LOG",
                LogFileGroup = "XrmLoggerDemo",
                logLevel = LogLevel.VERBOSE
            };

Next step is to get the execution context and do whatever your plugin is supposed to do. In my example I write the entity name, the message and the stage the plugin is running at.

xLog.ialert(String.Format("Entity: {0} => Message: {1} => Stage: {2}", context.PrimaryEntityName, context.MessageName, context.Stage));

So thats it more or less. Compile the code and register the assembly. Next choose the entity and the messages you want to listen at. And here is the result:

09.10.2013 14:27:28.400 :: INFO :: Entity: account => Message: RetrieveMultiple => Stage: 10
09.10.2013 14:27:28.405 :: INFO :: Entity: account => Message: RetrieveMultiple => Stage: 20
09.10.2013 14:27:28.458 :: INFO :: Entity: account => Message: RetrieveMultiple => Stage: 40

Below you find a listing of the complete code and the files do download.

xrlmLogger.cs

using System;
using System.Text;
using System.IO;

namespace xrmLog
{
    public enum LogLevel
    {
        VERBOSE = 0,
        INFO = 10,
        WARNING = 20,
        ERROR = 30,
    }

    public class XrmLoggerConfig
    {
        private string _LogDirectoryName = @"C:\LOG\";
        private string _LogFileName = "{0}-{1}.log";
        private string _LogFileGroup = "XrmAppLog";
        private LogLevel _logLevel = LogLevel.VERBOSE;

        public string LogDirectoryName
        {
            set
            {
                this._LogDirectoryName = value;
            }
            get
            {
                return this._LogDirectoryName;
            }
        }

        public string LogFileName
        {
            set
            {
                this._LogFileName = value;
            }
            get
            {
                return this._LogFileName;
            }
        }

        public string LogFileGroup
        {
            set
            {
                this._LogFileGroup = value;
            }
            get
            {
                return this._LogFileGroup;
            }
        }

        public LogLevel logLevel
        {
            set
            {
                this._logLevel = value;
            }
            get
            {
                return this._logLevel;
            }
        }

    }

    public class XrmLogger
    {

        private Boolean _dologging = true;
        private XrmLoggerConfig _config = new XrmLoggerConfig();

        private FileStream _fileStream;

        public XrmLogger()
        {
            this.init(_config);
        }

        public XrmLogger(string logFileName)
        {
            _config.LogFileName = logFileName;
            init(_config);
        }

        public XrmLogger(XrmLoggerConfig config)
        {
            this._config = config;
            init(config);
        }

        public Boolean doLogging
        {
            set
            {
                this._dologging = value;
            }
            get
            {
                return this._dologging;
            }
        }

        private string getDateTimeString()
        {
            return DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff");
        }

        private string getDateString()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }

        private string getFileName()
        {
            return String.Format(_config.LogFileName, this.getDateString(), _config.LogFileGroup);

        }

        private void init(XrmLoggerConfig config)
        {
            string _LogDirectoryName = config.LogDirectoryName.EndsWith(@"\") ? config.LogDirectoryName : config.LogDirectoryName + @"\";

            string path = String.Format("{0}{1}", _LogDirectoryName, this.getFileName());
            _fileStream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
        }

        public void close()
        {
            _fileStream.Close();
        }

        private void writeFile(string message)
        {
            string line = this.getDateTimeString() + message + Environment.NewLine;
            byte[] info = new UTF8Encoding(true).GetBytes(line);
            this._fileStream.Write(info, 0, info.Length);
            this._fileStream.Flush();
        }

        public void valert(string msg)
        {
            if (this._dologging && _config.logLevel <= LogLevel.VERBOSE)
            {
                writeFile(" :: VERBOSE :: " + msg);
            }
        }

        public void ialert(string msg)
        {
            if (this._dologging && _config.logLevel <= LogLevel.INFO)
            {
                writeFile(" :: INFO :: " + msg);
            }
        }

        public void walert(string msg)
        {
            if (this._dologging && _config.logLevel <= LogLevel.WARNING)
            {
                writeFile(" :: WARNINIG :: " + msg);
            }
        }

        public void ealert(string msg)
        {
            if (this._dologging && _config.logLevel <= LogLevel.ERROR)
            {
                writeFile(" :: ERROR :: " + msg);
            }
        }
    }

}

The Sample Plugin

If you want to write a informational message use ialert, if you want to indicate a warning walert and on errors use the ealert function.

XrmLoggerDemo.cs

using System;
using Microsoft.Xrm.Sdk;

// 
// This is  demo of the XrmLogger class.
// How to use it:
// Create a C:\LOG on the Server and give your CRM Users RW rights
// Compile this class (don't forget to sign it with a strong key!)
// Use the pluginregistrationtool from the CRM.SDK to register the assembly (don't specify sandbox isolation mode! 
// => this plugin works only in onpremise or hosting environments
// deploy it for any message and entity you like
// 

namespace xrmLog
{
    public class XrmLoggerDemo : IPlugin
    {

        public void Execute(System.IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            XrmLoggerConfig config = new XrmLoggerConfig()
            {
                LogDirectoryName = @"C:\LOG",
                LogFileGroup = "XrmLoggerDemo",
                logLevel = LogLevel.VERBOSE
            };

            XrmLogger xLog = new XrmLogger(config);

            xLog.ialert(String.Format("Entity: {0} => Message: {1} => Stage: {2}", context.PrimaryEntityName, context.MessageName, context.Stage));
        }
    }
}

Download Visual Studio 2012 Project:
XrmLoggerDemo

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>