private static bool TryGetEventOrganizerFromContact(OrganizationServiceContext serviceContext, EntityReference organizerEntityReference, out EntityListEventOrganizer organizer)
        {
            organizer = null;

            if (organizerEntityReference == null || organizerEntityReference.LogicalName != "contact")
            {
                return(false);
            }

            var contact = serviceContext.RetrieveSingle(organizerEntityReference.LogicalName,
                                                        new[] { "fullname", "emailaddress1" },
                                                        new[] {
                new Condition("statecode", ConditionOperator.Equal, 0),
                new Condition("contactid", ConditionOperator.Equal, organizerEntityReference.Id)
            });

            if (contact == null)
            {
                return(false);
            }

            organizer = new EntityListEventOrganizer
            {
                Name  = contact.GetAttributeValue <string>("fullname"),
                Email = contact.GetAttributeValue <string>("emailaddress1")
            };

            return(true);
        }
        private static bool TryGetEventOrganizerFromSystemUser(OrganizationServiceContext serviceContext, EntityReference organizerEntityReference, out EntityListEventOrganizer organizer)
        {
            organizer = null;

            if (organizerEntityReference == null || organizerEntityReference.LogicalName != "systemuser")
            {
                return(false);
            }

            var systemUser = serviceContext.RetrieveSingle(
                organizerEntityReference.LogicalName,
                new [] { "fullname", "internalemailaddress" },
                new Condition("systemuserid", ConditionOperator.Equal, organizerEntityReference.Id));

            if (systemUser == null)
            {
                return(false);
            }

            organizer = new EntityListEventOrganizer
            {
                Name  = systemUser.GetAttributeValue <string>("fullname"),
                Email = systemUser.GetAttributeValue <string>("internalemailaddress")
            };

            return(true);
        }
        private static EntityListEventOrganizer GetEventOrganizer(OrganizationServiceContext serviceContext, Entity entity, EntityListSettings settings, IDictionary <string, EntityListEventOrganizer> organizerCache)
        {
            if (string.IsNullOrWhiteSpace(settings.OrganizerFieldName))
            {
                return(null);
            }

            var organizerValue = entity.GetAttributeValue(settings.OrganizerFieldName);

            if (organizerValue == null)
            {
                return(null);
            }

            var organizerEntityReference = organizerValue as EntityReference;

            if (organizerEntityReference == null)
            {
                return(new EntityListEventOrganizer
                {
                    Name = organizerValue.ToString()
                });
            }

            var organizerCacheKey = "{0}:{1}".FormatWith(organizerEntityReference.LogicalName, organizerEntityReference.Id);

            EntityListEventOrganizer cachedOrganizer;

            if (organizerCache.TryGetValue(organizerCacheKey, out cachedOrganizer))
            {
                return(cachedOrganizer);
            }

            EntityListEventOrganizer contactOrganizer;

            if (TryGetEventOrganizerFromContact(serviceContext, organizerEntityReference, out contactOrganizer))
            {
                organizerCache[organizerCacheKey] = contactOrganizer;

                return(contactOrganizer);
            }

            EntityListEventOrganizer systemUserOrganizer;

            if (TryGetEventOrganizerFromSystemUser(serviceContext, organizerEntityReference, out systemUserOrganizer))
            {
                organizerCache[organizerCacheKey] = systemUserOrganizer;

                return(systemUserOrganizer);
            }

            if (!string.IsNullOrWhiteSpace(organizerEntityReference.Name))
            {
                var referenceNameOrganizer = new EntityListEventOrganizer
                {
                    Name = organizerEntityReference.Name
                };

                organizerCache[organizerCacheKey] = referenceNameOrganizer;

                return(referenceNameOrganizer);
            }

            return(null);
        }