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); } }); }
public void InitializeWithField(SPField field) { // Retrieves field properties fieldProperties = FieldManagement <ImageFieldProperties> .GetFieldProperties(field); }