public void OnSaveChange(SPField field, bool isNewField) { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID)) { // Save field properties ImageFieldProperties properties = new ImageFieldProperties { WebId = ddlWebs.SelectedValue, PictureLibraryId = ddlPictureLibrary.SelectedValue, FormatName = tbFormatName.Text, Overwrite = cbOverwrite.Checked, DefaultPictureId = fieldProperties == null ? -1 : fieldProperties.DefaultPictureId }; if (!cbClearDefaultPicture.Checked) { if (fuDefaultPicture.HasFile) { // Get the posted file HttpPostedFile file = fuDefaultPicture.PostedFile; if (file.ContentLength > 0) { using (SPWeb web = currentSite.OpenWeb(new Guid(properties.WebId))) { SPList pictureLibrary = web.Lists[new Guid(properties.PictureLibraryId)]; string uploadedFileName = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1, file.FileName.LastIndexOf(".") - file.FileName.LastIndexOf("\\") - 1); string uploadedFileExtension = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1); string pictureFileName = string.Format("dv_{0}.{1}", Guid.NewGuid().ToString("N"), uploadedFileExtension); string fileUrl = SPUrlUtility.CombineUrl(pictureLibrary.RootFolder.ToString(), pictureFileName); // Add the selected picture in the list SPListItem pictureItem = web.Files.Add(fileUrl, file.InputStream, true).Item; properties.DefaultPictureId = pictureItem.ID; } } } } else { properties.DefaultPictureId = -1; } FieldManagement<ImageFieldProperties>.SaveProperties(properties); } }); }
/// <summary> /// Create child controls /// </summary> protected override void CreateChildControls() { // Retrieve field properties fieldProperties = FieldManagement<ImageFieldProperties>.GetFieldProperties(this.Field); if (string.IsNullOrEmpty(fieldProperties.WebId)) { fieldProperties.WebId = SPContext.Current.Web.ID.ToString(); } if (ControlMode != SPControlMode.Display) { base.CreateChildControls(); BindControls(); if (ddlExistingPicture != null) { if (!Page.IsPostBack) { // Retrieve all pictures in order to add them in the drop down list using (SPWeb web = SPContext.Current.Site.OpenWeb(new Guid(fieldProperties.WebId))) { if (!fieldProperties.PictureLibraryId.IsGuid()) { var folder = web.Folders[fieldProperties.PictureLibraryId]; var list = web.Lists[folder.ParentListId]; fieldProperties.PictureLibraryId = list.ID.ToString(); } SPList pictureLibrary = web.Lists[new Guid(fieldProperties.PictureLibraryId)]; SPListItemCollection pictures = pictureLibrary.GetItems(new SPQuery { Query = "<Query />" }); if (fieldProperties.DefaultPictureId != -1) { ddlExistingPicture.Items.Add(new ListItem(SPUtility.GetLocalizedString("$Resources:Default", "ImageField", (uint)SPContext.Current.Web.Locale.LCID), fieldProperties.DefaultPictureId.ToString())); } ddlExistingPicture.Items.Add(new ListItem(SPUtility.GetLocalizedString("$Resources:None", "ImageField", (uint)SPContext.Current.Web.Locale.LCID), "-1")); imgExistingPicture.Visible = false; foreach (SPListItem picture in pictures) { if (!picture.Name.StartsWith("dv_") || picture.Name.Length != 39) { string pictureUrl = Utility.GetRelativeUrl(picture["EncodedAbsThumbnailUrl"].ToString()); ListItem pictureItem = new ListItem(picture.Name, picture.ID.ToString()); // If the picture is the one referenced by the field if (this.ControlMode == SPControlMode.Edit && this.ItemFieldValue != null && new FieldContent(this.ItemFieldValue.ToString()).Thumbnail == pictureUrl) { pictureItem.Selected = true; imgExistingPicture.Visible = true; imgExistingPicture.ImageUrl = pictureUrl; } ddlExistingPicture.Items.Add(pictureItem); } } if (imgExistingPicture.Visible == false) { ddlExistingPicture_OnSelectedIndexChanged(null, null); } } } ddlExistingPicture.SelectedIndexChanged += ddlExistingPicture_OnSelectedIndexChanged; } } }
/// <summary> /// Create child controls /// </summary> protected override void CreateChildControls() { // Retrieve field properties fieldProperties = FieldManagement <ImageFieldProperties> .GetFieldProperties(this.Field); if (string.IsNullOrEmpty(fieldProperties.WebId)) { fieldProperties.WebId = SPContext.Current.Web.ID.ToString(); } if (ControlMode != SPControlMode.Display) { base.CreateChildControls(); BindControls(); if (ddlExistingPicture != null) { if (!Page.IsPostBack) { // Retrieve all pictures in order to add them in the drop down list using (SPWeb web = SPContext.Current.Site.OpenWeb(new Guid(fieldProperties.WebId))) { if (!fieldProperties.PictureLibraryId.IsGuid()) { var folder = web.Folders[fieldProperties.PictureLibraryId]; var list = web.Lists[folder.ParentListId]; fieldProperties.PictureLibraryId = list.ID.ToString(); } SPList pictureLibrary = web.Lists[new Guid(fieldProperties.PictureLibraryId)]; SPListItemCollection pictures = pictureLibrary.GetItems(new SPQuery { Query = "<Query />" }); if (fieldProperties.DefaultPictureId != -1) { ddlExistingPicture.Items.Add(new ListItem(SPUtility.GetLocalizedString("$Resources:Default", "ImageField", (uint)SPContext.Current.Web.Locale.LCID), fieldProperties.DefaultPictureId.ToString())); } ddlExistingPicture.Items.Add(new ListItem(SPUtility.GetLocalizedString("$Resources:None", "ImageField", (uint)SPContext.Current.Web.Locale.LCID), "-1")); imgExistingPicture.Visible = false; foreach (SPListItem picture in pictures) { if (!picture.Name.StartsWith("dv_") || picture.Name.Length != 39) { string pictureUrl = Utility.GetRelativeUrl(picture["EncodedAbsThumbnailUrl"].ToString()); ListItem pictureItem = new ListItem(picture.Name, picture.ID.ToString()); // If the picture is the one referenced by the field if (this.ControlMode == SPControlMode.Edit && this.ItemFieldValue != null && new FieldContent(this.ItemFieldValue.ToString()).Thumbnail == pictureUrl) { pictureItem.Selected = true; imgExistingPicture.Visible = true; imgExistingPicture.ImageUrl = pictureUrl; } ddlExistingPicture.Items.Add(pictureItem); } } if (imgExistingPicture.Visible == false) { ddlExistingPicture_OnSelectedIndexChanged(null, null); } } } ddlExistingPicture.SelectedIndexChanged += ddlExistingPicture_OnSelectedIndexChanged; } } }
public void InitializeWithField(SPField field) { // Retrieves field properties fieldProperties = FieldManagement<ImageFieldProperties>.GetFieldProperties(field); }