protected void AddNote_Click(object sender, EventArgs e)
        {
            var regardingContact = Permit.GetAttributeValue <EntityReference>(RegardingContactFieldName);

            if (regardingContact == null || Contact == null || regardingContact.Id != Contact.Id)
            {
                throw new InvalidOperationException("Unable to retrieve the order.");
            }

            if (!string.IsNullOrEmpty(NewNoteText.Text) || (NewNoteAttachment.PostedFile != null && NewNoteAttachment.PostedFile.ContentLength > 0))
            {
                var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(
                    requestContext: Request.RequestContext, portalName: PortalName);

                var dataAdapter = new AnnotationDataAdapter(dataAdapterDependencies);

                var annotation = new Annotation
                {
                    NoteText  = string.Format("{0}{1}", AnnotationHelper.WebAnnotationPrefix, NewNoteText.Text),
                    Subject   = AnnotationHelper.BuildNoteSubject(dataAdapterDependencies),
                    Regarding = Permit.ToEntityReference()
                };
                if (NewNoteAttachment.PostedFile != null && NewNoteAttachment.PostedFile.ContentLength > 0)
                {
                    annotation.FileAttachment = AnnotationDataAdapter.CreateFileAttachment(new HttpPostedFileWrapper(NewNoteAttachment.PostedFile));
                }
                dataAdapter.CreateAnnotation(annotation);
            }

            Response.Redirect(Request.Url.PathAndQuery);
        }
Exemple #2
0
        private static void CreateFiles(ICommandContext commandContext, DirectoryUploadInfo uploadInfo, IEnumerable <HttpPostedFile> files, EntityReference publishingState, out List <string> @select, out List <Tuple <string, string> > errors)
        {
            @select = new List <string>();
            errors  = new List <Tuple <string, string> >();

            var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies();
            var annotationDataAdapter   = new AnnotationDataAdapter(dataAdapterDependencies);
            var website = HttpContext.Current.GetWebsite();

            var location = website.Settings.Get <string>("WebFiles/StorageLocation");

            StorageLocation storageLocation;

            if (!Enum.TryParse(location, true, out storageLocation))
            {
                storageLocation = StorageLocation.CrmDocument;
            }

            var maxFileSizeErrorMessage = website.Settings.Get <string>("WebFiles/MaxFileSizeErrorMessage");

            var annotationSettings = new AnnotationSettings(dataAdapterDependencies.GetServiceContext(),
                                                            storageLocation: storageLocation, maxFileSizeErrorMessage: maxFileSizeErrorMessage);

            foreach (var file in files)
            {
                var serviceContext = commandContext.CreateServiceContext();

                try
                {
                    var webFile = new Entity("adx_webfile");

                    var fileName = Path.GetFileName(file.FileName);

                    webFile.Attributes["adx_name"]              = fileName;
                    webFile.Attributes["adx_partialurl"]        = GetPartialUrlFromFileName(fileName);
                    webFile.Attributes["adx_websiteid"]         = website.Entity.ToEntityReference();
                    webFile.Attributes["adx_publishingstateid"] = publishingState;
                    webFile.Attributes["adx_hiddenfromsitemap"] = true;
                    webFile.Attributes[uploadInfo.WebFileForeignKeyAttribute] = uploadInfo.EntityReference;

                    serviceContext.AddObject(webFile);
                    serviceContext.SaveChanges();

                    annotationDataAdapter.CreateAnnotation(new Annotation
                    {
                        Regarding      = webFile.ToEntityReference(),
                        FileAttachment = AnnotationDataAdapter.CreateFileAttachment(new HttpPostedFileWrapper(file), annotationSettings.StorageLocation)
                    }, annotationSettings);

                    @select.Add(new DirectoryContentHash(webFile.ToEntityReference()).ToString());
                }
                catch (Exception e)
                {
                    ADXTrace.Instance.TraceError(TraceCategory.Application, string.Format(@"Exception uploading file: {0}", e.ToString()));

                    errors.Add(new Tuple <string, string>(file.FileName, e.Message));
                }
            }
        }
        protected virtual void LogPaymentRequest(HttpContext context, PortalConfigurationDataAdapterDependencies dataAdapterDependencies, Tuple <Guid, string> quoteAndReturnUrl, string subject, string log)
        {
            var dataAdapter = new AnnotationDataAdapter(dataAdapterDependencies);
            var note        = new Annotation
            {
                Subject        = subject,
                Regarding      = new EntityReference("quote", quoteAndReturnUrl.Item1),
                FileAttachment = AnnotationDataAdapter.CreateFileAttachment("log.txt", "text/plain", Encoding.UTF8.GetBytes(log))
            };

            dataAdapter.CreateAnnotation(note);
        }
        protected override void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, Entity entity, CmsEntityMetadata entityMetadata, ICrmEntitySecurityProvider security)
        {
            if (!IsRequestMethod(context.Request, "POST"))
            {
                throw new CmsEntityServiceException(HttpStatusCode.MethodNotAllowed, "Request method {0} not allowed for this resource.".FormatWith(context.Request.HttpMethod));
            }

            var dataAdapterDependencies =
                new PortalConfigurationDataAdapterDependencies(requestContext: context.Request.RequestContext,
                                                               portalName: PortalName);
            var annotationDataAdapter = new AnnotationDataAdapter(dataAdapterDependencies);
            var website = context.GetWebsite();

            var             location = website.Settings.Get <string>("WebFiles/StorageLocation");
            StorageLocation storageLocation;

            if (!Enum.TryParse(location, true, out storageLocation))
            {
                storageLocation = StorageLocation.CrmDocument;
            }

            var maxFileSizeErrorMessage = website.Settings.Get <string>("WebFiles/MaxFileSizeErrorMessage");

            var annotationSettings = new AnnotationSettings(dataAdapterDependencies.GetServiceContext(),
                                                            storageLocation: storageLocation, maxFileSizeErrorMessage: maxFileSizeErrorMessage);

            var files       = context.Request.Files;
            var postedFiles = new List <HttpPostedFile>();

            for (var i = 0; i < files.Count; i++)
            {
                postedFiles.Add(files[i]);
            }

            foreach (var file in postedFiles)
            {
                annotationDataAdapter.CreateAnnotation(new Annotation
                {
                    Regarding      = entity.ToEntityReference(),
                    FileAttachment = AnnotationDataAdapter.CreateFileAttachment(new HttpPostedFileWrapper(file), annotationSettings.StorageLocation)
                }, annotationSettings);
            }

            context.Response.ContentType = "text/plain";
            context.Response.Write("OK");
        }
 public virtual void AddNote(string text, string fileName = null, string contentType = null, byte[] fileContent = null, EntityReference ownerId = null)
 {
     try
     {
         var da         = new AnnotationDataAdapter(Dependencies);
         var annotation = new Annotation
         {
             Subject   = AnnotationHelper.BuildNoteSubject(Dependencies),
             NoteText  = string.Format("{0}{1}", AnnotationHelper.WebAnnotationPrefix, text),
             Regarding = Incident,
             Owner     = ownerId
         };
         if (fileContent != null && fileContent.Length > 0 && !string.IsNullOrEmpty(fileName) && !string.IsNullOrEmpty(contentType))
         {
             annotation.FileAttachment = AnnotationDataAdapter.CreateFileAttachment(EnsureValidFileName(fileName), contentType, fileContent);
         }
         da.CreateAnnotation(annotation);
     }
     catch (Exception e)
     {
         WebEventSource.Log.GenericErrorException(new Exception("Create annotation error", e));
         throw;
     }
 }
Exemple #6
0
        public ActionResult AddPortalComment(string regardingEntityLogicalName, string regardingEntityId, string text,
                                             HttpPostedFileBase file = null, string attachmentSettings = null)
        {
            if (string.IsNullOrWhiteSpace(text) || string.IsNullOrWhiteSpace(StringHelper.StripHtml(text)))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.ExpectationFailed, ResourceManager.GetString("Required_Field_Error").FormatWith(ResourceManager.GetString("Comment_DefaultText"))));
            }

            Guid regardingId;

            Guid.TryParse(regardingEntityId, out regardingId);
            var regarding = new EntityReference(regardingEntityLogicalName, regardingId);

            var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext);
            var serviceContext          = dataAdapterDependencies.GetServiceContext();

            var dataAdapter = new ActivityDataAdapter(dataAdapterDependencies);
            var settings    = EntityNotesController.GetAnnotationSettings(serviceContext, attachmentSettings);
            var crmUser     = dataAdapter.GetCRMUserActivityParty(regarding, "ownerid");
            var portalUser  = new Entity("activityparty");

            portalUser["partyid"] = dataAdapterDependencies.GetPortalUser();

            var portalComment = new PortalComment
            {
                Description        = text,
                From               = portalUser,
                To                 = crmUser,
                Regarding          = regarding,
                AttachmentSettings = settings,
                StateCode          = StateCode.Completed,
                StatusCode         = StatusCode.Received,
                DirectionCode      = PortalCommentDirectionCode.Incoming
            };

            if (file != null && file.ContentLength > 0)
            {
                // Soon we will change the UI/controller to accept multiple attachments during the create dialog, so the data adapter takes in a list of attachments
                portalComment.FileAttachments = new IAnnotationFile[]
                { AnnotationDataAdapter.CreateFileAttachment(file, settings.StorageLocation) };
            }

            var result = dataAdapter.CreatePortalComment(portalComment);

            if (!result.PermissionsExist)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden,
                                                ResourceManager.GetString("Entity_Permissions_Have_Not_Been_Defined_Message")));
            }

            if (!result.CanCreate)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden,
                                                ResourceManager.GetString("No_Permissions_To_Create_Notes")));
            }

            if (!result.CanAppendTo)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden,
                                                ResourceManager.GetString("No_Permissions_To_Append_Record")));
            }

            if (!result.CanAppend)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden,
                                                ResourceManager.GetString("No_Permissions_To_Append_Notes")));
            }

            if (FeatureCheckHelper.IsFeatureEnabled(FeatureNames.TelemetryFeatureUsage))
            {
                PortalFeatureTrace.TraceInstance.LogFeatureUsage(FeatureTraceCategory.Comments, this.HttpContext, "create_comment_" + regardingEntityLogicalName, 1, regarding, "create");
            }

            return(new HttpStatusCodeResult(HttpStatusCode.Created));
        }
        public ActionResult UpdateNote(string id, string text, string subject, bool isPrivate = false, HttpPostedFileBase file = null, string attachmentSettings = null)
        {
            if (string.IsNullOrWhiteSpace(text) || string.IsNullOrWhiteSpace(StringHelper.StripHtml(text)))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.ExpectationFailed, ResourceManager.GetString("Required_Field_Error").FormatWith(ResourceManager.GetString("Note_DefaultText"))));
            }
            Guid annotationId;

            Guid.TryParse(id, out annotationId);
            string portalName          = null;
            var    portalContext       = PortalCrmConfigurationManager.CreatePortalContext();
            var    languageCodeSetting = portalContext.ServiceContext.GetSiteSettingValueByName(portalContext.Website, "Language Code");

            if (!string.IsNullOrWhiteSpace(languageCodeSetting))
            {
                int languageCode;
                if (int.TryParse(languageCodeSetting, out languageCode))
                {
                    portalName = languageCode.ToString(CultureInfo.InvariantCulture);
                }
            }

            var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext, portalName: portalName);
            var dataAdapter             = new AnnotationDataAdapter(dataAdapterDependencies);
            var settings = GetAnnotationSettings(dataAdapterDependencies.GetServiceContext(), attachmentSettings);

            var annotation = dataAdapter.GetAnnotation(annotationId);

            annotation.AnnotationId = annotationId;

            annotation.NoteText = string.Format("{0}{1}", AnnotationHelper.WebAnnotationPrefix, text);

            if (!isPrivate && !string.IsNullOrWhiteSpace(subject) && subject.Contains(AnnotationHelper.PrivateAnnotationPrefix))
            {
                annotation.Subject = subject.Replace(AnnotationHelper.PrivateAnnotationPrefix, string.Empty);
            }

            if (isPrivate && !string.IsNullOrWhiteSpace(subject) && !subject.Contains(AnnotationHelper.PrivateAnnotationPrefix))
            {
                annotation.Subject = subject + AnnotationHelper.PrivateAnnotationPrefix;
            }

            if (file != null && file.ContentLength > 0)
            {
                annotation.FileAttachment = AnnotationDataAdapter.CreateFileAttachment(file, settings.StorageLocation);
            }

            try
            {
                var result = dataAdapter.UpdateAnnotation(annotation, settings);

                if (!result.PermissionsExist)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden,
                                                    ResourceManager.GetString("Entity_Permissions_Have_Not_Been_Defined_Message")));
                }

                if (!result.PermissionGranted)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, string.Format(ResourceManager.GetString("No_Entity_Permissions"), "update notes")));
                }

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            catch (AnnotationException ex)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, ex.Message));
            }
        }
        public ActionResult AddNote(string regardingEntityLogicalName, string regardingEntityId, string text, bool isPrivate = false, HttpPostedFileBase file = null, string attachmentSettings = null)
        {
            if (string.IsNullOrWhiteSpace(text) || string.IsNullOrWhiteSpace(StringHelper.StripHtml(text)))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.ExpectationFailed, ResourceManager.GetString("Required_Field_Error").FormatWith(ResourceManager.GetString("Note_DefaultText"))));
            }

            Guid regardingId;

            Guid.TryParse(regardingEntityId, out regardingId);
            var    regarding           = new EntityReference(regardingEntityLogicalName, regardingId);
            string portalName          = null;
            var    portalContext       = PortalCrmConfigurationManager.CreatePortalContext();
            var    languageCodeSetting = portalContext.ServiceContext.GetSiteSettingValueByName(portalContext.Website, "Language Code");

            if (!string.IsNullOrWhiteSpace(languageCodeSetting))
            {
                int languageCode;
                if (int.TryParse(languageCodeSetting, out languageCode))
                {
                    portalName = languageCode.ToString(CultureInfo.InvariantCulture);
                }
            }

            var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext, portalName: portalName);
            var serviceContext          = dataAdapterDependencies.GetServiceContext();
            var user = Request.GetOwinContext().GetUser();

            var dataAdapter = new AnnotationDataAdapter(dataAdapterDependencies);
            var settings    = GetAnnotationSettings(serviceContext, attachmentSettings);

            var annotation = new Annotation
            {
                NoteText  = string.Format("{0}{1}", AnnotationHelper.WebAnnotationPrefix, text),
                Subject   = AnnotationHelper.BuildNoteSubject(serviceContext, user.ContactId, isPrivate),
                Regarding = regarding
            };

            if (file != null && file.ContentLength > 0)
            {
                annotation.FileAttachment = AnnotationDataAdapter.CreateFileAttachment(file, settings.StorageLocation);
            }

            var result = (AnnotationCreateResult)dataAdapter.CreateAnnotation(annotation, settings);

            if (!result.PermissionsExist)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, ResourceManager.GetString("Entity_Permissions_Have_Not_Been_Defined_Message")));
            }

            if (!result.CanCreate)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, string.Format(ResourceManager.GetString("No_Entity_Permissions"), "create notes")));
            }

            if (!result.CanAppendTo)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, string.Format(ResourceManager.GetString("No_Entity_Permissions"), "append to record")));
            }

            if (!result.CanAppend)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, string.Format(ResourceManager.GetString("No_Entity_Permissions"), "append notes")));
            }

            return(new HttpStatusCodeResult(HttpStatusCode.Created));
        }
        public ActionResult UpdateNote(string id, string text, string subject, bool isPrivate = false, HttpPostedFileBase file = null, string attachmentSettings = null)
        {
            Guid annotationId;

            Guid.TryParse(id, out annotationId);
            string portalName          = null;
            var    portalContext       = PortalCrmConfigurationManager.CreatePortalContext();
            var    languageCodeSetting = portalContext.ServiceContext.GetSiteSettingValueByName(portalContext.Website, "Language Code");

            if (!string.IsNullOrWhiteSpace(languageCodeSetting))
            {
                int languageCode;
                if (int.TryParse(languageCodeSetting, out languageCode))
                {
                    portalName = languageCode.ToString(CultureInfo.InvariantCulture);
                }
            }

            var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext, portalName: portalName);
            var dataAdapter             = new AnnotationDataAdapter(dataAdapterDependencies);
            var settings = JsonConvert.DeserializeObject <AnnotationSettings>(attachmentSettings) ??
                           new AnnotationSettings(dataAdapterDependencies.GetServiceContext(), true);

            var annotation = dataAdapter.GetAnnotation(annotationId);

            annotation.AnnotationId = annotationId;

            annotation.NoteText = string.Format("{0}{1}", AnnotationHelper.WebAnnotationPrefix, text);

            if (!isPrivate && !string.IsNullOrWhiteSpace(subject) && subject.Contains(AnnotationHelper.PrivateAnnotationPrefix))
            {
                annotation.Subject = subject.Replace(AnnotationHelper.PrivateAnnotationPrefix, string.Empty);
            }

            if (isPrivate && !string.IsNullOrWhiteSpace(subject) && !subject.Contains(AnnotationHelper.PrivateAnnotationPrefix))
            {
                annotation.Subject = subject + AnnotationHelper.PrivateAnnotationPrefix;
            }

            if (file != null && file.ContentLength > 0)
            {
                annotation.FileAttachment = AnnotationDataAdapter.CreateFileAttachment(file, settings.StorageLocation);
            }

            try
            {
                var result = dataAdapter.UpdateAnnotation(annotation, settings);

                if (!result.PermissionsExist)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden,
                                                    "Entity Permissions have not been defined. Your request could not be completed."));
                }

                if (!result.CanWrite)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden,
                                                    "Permission Denied. You do not have the appropriate Entity Permissions to update notes."));
                }

                return(new HttpStatusCodeResult(HttpStatusCode.NoContent));
            }
            catch (AnnotationException ex)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, ex.Message));
            }
        }
        public ActionResult AddNote(string regardingEntityLogicalName, string regardingEntityId, string text, bool isPrivate = false, HttpPostedFileBase file = null, string attachmentSettings = null)
        {
            Guid regardingId;

            Guid.TryParse(regardingEntityId, out regardingId);
            var    regarding           = new EntityReference(regardingEntityLogicalName, regardingId);
            string portalName          = null;
            var    portalContext       = PortalCrmConfigurationManager.CreatePortalContext();
            var    languageCodeSetting = portalContext.ServiceContext.GetSiteSettingValueByName(portalContext.Website, "Language Code");

            if (!string.IsNullOrWhiteSpace(languageCodeSetting))
            {
                int languageCode;
                if (int.TryParse(languageCodeSetting, out languageCode))
                {
                    portalName = languageCode.ToString(CultureInfo.InvariantCulture);
                }
            }

            var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext, portalName: portalName);
            var serviceContext          = dataAdapterDependencies.GetServiceContext();

            var dataAdapter = new AnnotationDataAdapter(dataAdapterDependencies);
            var settings    = JsonConvert.DeserializeObject <AnnotationSettings>(attachmentSettings) ??
                              new AnnotationSettings(serviceContext, true);

            var annotation = new Annotation
            {
                NoteText  = string.Format("{0}{1}", AnnotationHelper.WebAnnotationPrefix, text),
                Subject   = AnnotationHelper.BuildNoteSubject(serviceContext, dataAdapterDependencies.GetPortalUser(), isPrivate),
                Regarding = regarding
            };

            if (file != null && file.ContentLength > 0)
            {
                annotation.FileAttachment = AnnotationDataAdapter.CreateFileAttachment(file, settings.StorageLocation);
            }

            var result = dataAdapter.CreateAnnotation(annotation, settings);

            if (!result.PermissionsExist)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, "Entity Permissions have not been defined. Your request could not be completed."));
            }

            if (!result.CanCreate)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, "Permission Denied. You do not have the appropriate Entity Permissions to create notes."));
            }

            if (!result.CanAppendTo)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, "Permission Denied. You do not have the appropriate Entity Permissions to append to record."));
            }

            if (!result.CanAppend)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, "Permission Denied. You do not have the appropriate Entity Permissions to append notes."));
            }

            return(new HttpStatusCodeResult(HttpStatusCode.NoContent));
        }