public static MsCrmResult SaveObjectProfileImage(Annotation note, string fieldName, SqlDataAccess sda) { MsCrmResult returnValue = new MsCrmResult(); try { IOrganizationService service = MSCRM.GetOrgService(true); int splitLength = note.FileName.Split('.').Length; note.FilePath = Guid.NewGuid() + "." + note.FileName.Split('.')[splitLength - 1].ToLower(); byte[] data = Convert.FromBase64String(note.File); //File.WriteAllBytes(HostingEnvironment.MapPath("~/attachments") + "/" + note.FilePath, data); File.WriteAllBytes(@Globals.AttachmentFolder + @"\" + note.FilePath, data); #region | DELETE PREVIOUS IMAGE FILE | string fileName = AttachmentFileHelper.GetEntityProfileImageFileName(note.Object.Id.ToString(), note.Object.LogicalName, fieldName, sda); if (!string.IsNullOrEmpty(fileName)) { //string filePath = HostingEnvironment.MapPath("~/attachments") + "/" + fileName; string filePath = @Globals.AttachmentFolder + @"\" + fileName; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } #endregion #region | UPDATE CRM ENTITY | Entity ent = new Entity(note.Object.LogicalName.ToLower()); ent[note.Object.LogicalName.ToLower() + "id"] = note.Object.Id; ent[fieldName] = note.FilePath; service.Update(ent); #endregion returnValue.Success = true; returnValue.Result = note.FilePath; } catch (Exception ex) { returnValue.Success = false; returnValue.Result = ex.Message; } return(returnValue); }
public static MsCrmResult UpdateProfileImage(Guid userId, string fileName, string fieldName, SqlDataAccess sda, IOrganizationService service) { MsCrmResult returnValue = new MsCrmResult(); try { #region | DELETE PREVIOUS IMAGE FILE | string oldFileName = AttachmentFileHelper.GetEntityProfileImageFileName(userId.ToString(), "new_user", fieldName, sda); if (!string.IsNullOrEmpty(oldFileName)) { //string filePath = HostingEnvironment.MapPath("~/attachments") + "/" + oldFileName; string filePath = @Globals.AttachmentFolder + @"\" + oldFileName; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } #endregion #region | UPDATE CRM ENTITY | Entity ent = new Entity("new_user"); ent.Id = userId; ent[fieldName] = fileName; service.Update(ent); #endregion returnValue.Success = true; returnValue.Result = fileName; } catch (Exception ex) { returnValue.Success = false; returnValue.Result = ex.Message; } return(returnValue); }