U4-1371 - Create Media in RTE doesn't work - UmbracoCms.6.0.0.build.94

Created by Jeroen Breuer 29 Dec 2012, 20:38:24 Updated by Jeroen Breuer 14 Jan 2013, 21:35:33

Relates to: U4-1372

Relates to: U4-1368

Relates to: U4-1438

Creating Media in the RTE doesn't work anymore. See the attachments. I debugged the code and I know what the problem is. The following doesn't seem to work anymore: var media = Media.MakeNew(TextBoxTitle.Text, cms.businesslogic.media.MediaType.GetByAlias("image"), BasePage.Current.getUser(), int.Parse(MediaPickerControl.Value));

foreach (var property in media.GenericProperties) { if (property.PropertyType.DataTypeDefinition.DataType.Id == UploadField.Id) } UploadField.DataEditor.Save();

//this seems real ugly since we apparently already have the properties above (props)... but this data layer is insane and undecipherable:) string mainImage = media.getProperty("umbracoFile").Value.ToString(); string extension = mainImage.Substring(mainImage.LastIndexOf(".") + 1, mainImage.Length - mainImage.LastIndexOf(".") - 1); In v4 of Umbraco when UploadField.DataEditor.Save() was called the image was uploaded and set on the umbracoFile property. Now this doesn't work anymore so when the mainImage string tries to get the umbracoFile property it returns an empty string which goes wrong when the code tries to get the extension.

I'm also using the above code with DAMP and it also doesn't work there anymore:

//Get the propertyid and datatypeid and set them on the upload field. Now the upload field will automatically do the upload to the newly created media item. string sql = string.Format(@" select cpd.id as id, cpt.dataTypeId as datatypeid from cmsPropertyData cpd inner join cmsPropertyType cpt on cpd.propertytypeid = cpt.Id inner join cmsDataType cdt on cpt.dataTypeId = cdt.nodeId where cpd.contentNodeId = {0} and cdt.controlId = '{1}'", newMedia.Id, uploadField.Id);

using (IRecordsReader dr = SqlHelper.ExecuteReader(sql)) { while (dr.Read()) { //Set the values of the first upload datatype that was found on the media type. uploadField.Data.PropertyId = dr.GetInt("id"); uploadField.DataTypeDefinitionId = dr.GetInt("datatypeid");

    //We only need the first result. If other upload fields are found we don't need them.
    break;
}

}

//Upload the file and set all the related properties. uploadField.DataEditor.Save();

2 Attachments

Comments

Jeroen Breuer 29 Dec 2012, 20:57:39

Might be useful to add how the uploadField is created: public IDataType uploadField = new Factory().GetNewObject(new Guid("5032a6e6-69e3-491d-bb28-cd31cd11086c"));


Jeroen Breuer 01 Jan 2013, 16:16:03

I found out what's wrong and fixed this in DAMP. Here is the code:

//Create the new media item. Media newMedia = Media.MakeNew(TxtName.Text, mediaType, user, Convert.ToInt32(digibizPickerView.PickedValue.Value));

if (Convert.ToBoolean(HiddenFieldHasUploadFile.Value)) { string alias = string.Empty;

//Get the propertyid and datatypeid and set them on the upload field. Now the upload field will automatically do the upload to the newly created media item.
string sql = string.Format(@"
    select cpd.id as id, cpt.dataTypeId as datatypeid, cpt.Alias as alias  from cmsPropertyData cpd
        inner join cmsPropertyType cpt on cpd.propertytypeid = cpt.Id
        inner join cmsDataType cdt on cpt.dataTypeId = cdt.nodeId
    where cpd.contentNodeId = {0}
    and cdt.controlId = '{1}'", newMedia.Id, uploadField.Id);

using (IRecordsReader dr = SqlHelper.ExecuteReader(sql))
{
    while (dr.Read())
    {
        //Set the values of the first upload datatype that was found on the media type.
        uploadField.Data.PropertyId = dr.GetInt("id");
        uploadField.DataTypeDefinitionId = dr.GetInt("datatypeid");
        alias = dr.GetString("alias");

        //We only need the first result. If other upload fields are found we don't need them.
        break;
    }
}

//Upload the file and set all the related properties.
uploadField.DataEditor.Save();

//Set the value on the media object so it will be part of the transaction.
newMedia.getProperty(alias).Value = uploadField.Data.Value;

}

//Call the save method to update the media xml. //This way also the BeforeSave and AfterSave events will be called so they can be used. newMedia.Save(); newMedia.XmlGenerate(new XmlDocument());

It's the same as in [this|http://issues.umbraco.org/issue/U4-1372#comment=67-4411] issue. So the property needs to be set on the media object before .Save() is called. This probably also needs to be done in the code which creates media in the RTE.


Morten Christensen 03 Jan 2013, 10:34:48

I fixed this issue by adding the following code after the DataEditor save:

var prop = media.GenericProperties.FirstOrDefault(x => x.PropertyType.DataTypeDefinition.DataType.Id == UploadField.Id); prop.Value = UploadField.Data.Value; media.Save();


Priority: Major

Type: Bug

State: Fixed

Assignee: Morten Christensen

Difficulty: Normal

Category:

Backwards Compatible: True

Fix Submitted:

Affected versions: 6.0.0

Due in version: 6.0.0

Sprint:

Story Points:

Cycle: