コード例 #1
0
        private void ValidateFileAccept(FileUpload fileUpload, ServerValidateEventArgs args)
        {
            args.IsValid = true;

            if (!fileUpload.HasFiles)
            {
                return;
            }

            var regex = AnnotationDataAdapter.GetAcceptRegex(_attachFileAccept);

            foreach (var uploadedFile in fileUpload.PostedFiles)
            {
                var path = System.IO.Path.GetExtension(uploadedFile.FileName);
                args.IsValid = regex.IsMatch(uploadedFile.ContentType) || regex.IsMatch(path);
                if (!args.IsValid)
                {
                    break;
                }
            }
        }
        /// <summary>
        /// Creates a Portal Comment entity, as well Annotation entities for any attachments
        /// </summary>
        /// <param name="portalComment"></param>
        /// <returns></returns>
        public PortalCommentCreateResult CreatePortalComment(PortalComment portalComment)
        {
            var serviceContext         = _dependencies.GetServiceContext();
            var serviceContextForWrite = _dependencies.GetServiceContextForWrite();

            PortalCommentCreateResult result = null;

            var entityPermissionProvider = new CrmEntityPermissionProvider();

            result = new PortalCommentCreateResult(entityPermissionProvider, serviceContext, portalComment.Regarding);

            if (result.PermissionsExist && result.PermissionGranted)
            {
                var acceptMimeTypes      = AnnotationDataAdapter.GetAcceptRegex(portalComment.AttachmentSettings.AcceptMimeTypes);
                var acceptExtensionTypes = AnnotationDataAdapter.GetAcceptRegex(portalComment.AttachmentSettings.AcceptExtensionTypes);
                if (portalComment.FileAttachments != null)
                {
                    portalComment.FileAttachments.ForEach(attachment =>
                    {
                        if (!(acceptExtensionTypes.IsMatch(Path.GetExtension(attachment.FileName).ToLower()) ||
                              acceptMimeTypes.IsMatch(attachment.MimeType)))
                        {
                            throw new AnnotationException(portalComment.AttachmentSettings.RestrictMimeTypesErrorMessage);
                        }
                    });
                }

                var owner = portalComment.To?.GetAttributeValue <EntityReference>("partyid");

                var entity = new Entity("adx_portalcomment");

                entity.SetAttributeValue("description", portalComment.Description);
                entity.SetAttributeValue("regardingobjectid", portalComment.Regarding);
                entity.SetAttributeValue("regardingobjecttypecode", portalComment.Regarding.LogicalName);
                entity.SetAttributeValue("from", new Entity[] { portalComment.From });
                entity.SetAttributeValue("adx_portalcommentdirectioncode", new OptionSetValue((int)portalComment.DirectionCode));

                if (owner != null)
                {
                    entity.SetAttributeValue("ownerid", owner);

                    if (!string.Equals(owner.LogicalName, "team", StringComparison.OrdinalIgnoreCase))
                    {
                        entity.SetAttributeValue("to", new Entity[] { portalComment.To });
                    }
                }

                // Create adx_portalcomment but skip cache invalidation.
                var id = (serviceContext as IOrganizationService).ExecuteCreate(entity, RequestFlag.ByPassCacheInvalidation);

                portalComment.ActivityId = entity.Id = id;
                portalComment.Entity     = entity;

                // Can only change state code value after entity creation
                entity.SetAttributeValue("statecode", new OptionSetValue((int)portalComment.StateCode));
                entity.SetAttributeValue("statuscode", new OptionSetValue((int)portalComment.StatusCode));

                // Explicitly include the activityid, this way the Cache will know to add dependency on "activitypointer" for this "adx_portalcomment" entity.
                entity.SetAttributeValue("activityid", id);

                (serviceContext as IOrganizationService).ExecuteUpdate(entity);

                if (portalComment.FileAttachments != null)
                {
                    // permission for Portal Comment implies permission for attachment to Portal Comment
                    portalComment.AttachmentSettings.RespectPermissions = false;

                    foreach (IAnnotationFile attachment in portalComment.FileAttachments)
                    {
                        IAnnotation annotation = new Annotation
                        {
                            Subject        = String.Empty,
                            NoteText       = String.Empty,
                            Regarding      = portalComment.Entity.ToEntityReference(),
                            FileAttachment = attachment
                        };

                        IAnnotationDataAdapter da = new AnnotationDataAdapter(_dependencies);
                        da.CreateAnnotation(annotation, portalComment.AttachmentSettings);
                    }
                }
            }

            return(result);
        }