Пример #1
0
        public void OnItemSaved(object sender, EventArgs args)
        {
            var item        = Event.ExtractParameter(args, 0) as Item;
            var itemChanges = Event.ExtractParameter(args, 1) as ItemChanges;

            if (item == null ||
                item.Paths.FullPath.StartsWith("/sitecore/templates") ||
                !item.TemplateID.Equals(Settings.PersonTemplateId) ||
                itemChanges == null)
            {
                return;
            }

            using (new Sitecore.Data.Events.EventDisabler())
            {
                var personName = item.Fields[Settings.FaceEntityNameFieldId]?.Value ?? string.Empty;
                var groupId    = item.Parent.Fields[Settings.FaceEntityIdFieldId]?.Value ?? string.Empty;
                if (string.IsNullOrWhiteSpace(personName) ||
                    string.IsNullOrWhiteSpace(groupId))
                {
                    return;
                }

                var oldPersonId    = itemChanges.FieldChanges[Settings.FaceEntityIdFieldId]?.OriginalValue;
                var personId       = item.Fields[Settings.FaceEntityIdFieldId]?.Value ?? string.Empty;
                var personUserData = item.Fields[Settings.FaceEntityUserDataFieldId]?.Value ?? string.Empty;

                //if they modified the person id you have to replace the person
                if (!string.IsNullOrWhiteSpace(oldPersonId) && oldPersonId != personId)
                {
                    PersonGroupService.DeletePerson(groupId, new Guid(oldPersonId));
                }

                //if not found, create. otherwise update
                if (string.IsNullOrWhiteSpace(personId) || FaceService.GetPerson(groupId, new Guid(personId)) == null)
                {
                    PersonGroupService.CreatePerson(item, groupId, personName, personUserData);
                }
                else
                {
                    PersonGroupService.UpdatePerson(item, groupId, new Guid(personId), personName, personUserData);
                }
            }
        }
        public void OnItemDeleted(object sender, EventArgs args)
        {
            var item = Event.ExtractParameter(args, 0) as Item;

            if (item == null ||
                item.Paths.FullPath.StartsWith("/sitecore/templates") ||
                !item.TemplateID.Equals(Settings.PersonTemplateId))
            {
                return;
            }

            var groupId  = item.Parent?.Fields[Settings.FaceEntityIdFieldId]?.Value ?? string.Empty;
            var personId = item.Fields[Settings.FaceEntityIdFieldId]?.Value ?? string.Empty;

            if (string.IsNullOrWhiteSpace(groupId) || string.IsNullOrWhiteSpace(personId))
            {
                return;
            }

            PersonGroupService.DeletePerson(groupId, new Guid(personId));
        }