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);
        }
 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;
     }
 }
        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));
        }
示例#4
0
        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));
        }