November Happy Hour will be moved to Thursday December 5th.

Saving media with AccessLevel.NoAccess still throws exception: Access was denied to content 6116_10194. The required access level was "Edit, Publish".

Vote:
 

I have code to create media assets (in this example image files) which was previously working but now gives the access denied exception.  The same accesslevel mechanism seems to work fine for pages and blocks. NOTE: this is a slightly hacked up version showing the mechanism used, but mechanically is the same as code that previously worked.  So I'm wondering if anyone has ideas on where else to be looking in terms of what changed?  One question I have is why does it say it needs Edit access when we are  creating the asset (or does the GetDefault actually do the create?)  Could it be related to needing edit access to the parent folder into which the asset is going?

I have an update, after poking around I found that contrary to the exception itself, the ImageFile is in fact being created in the proper folder.  So the question is, why when it appears to be actually performing the Save am I getting this Exception?

public ContentReference SaveImage(ContentReference parentFolder, string data, string fileName, string fileExtension)
{
    var mediaType = _mediaDataResolver.GetFirstMatching(fileExtension);
    var contentType = _contentTypeRepository.Load(mediaType);

    if (mediaType != typeof(ImageFile))
        return null;

    var imageFile = _contentRepository.GetDefault(parentFolder, contentType.ID);

    imageFile.Name = fileName;

    var imageBlob = _blobFactory.CreateBlob(imageFile.BinaryDataContainer, fileExtension);
    using (MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(data)))
    {
        imageBlob.Write(memoryStream);
    }
    imageFile.BinaryData = imageBlob;

    return _contentRepository.Save(imageFile, SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
}
#194367
Edited, Jun 19, 2018 17:12
Vote:
 

Can you see from the call stack in the exception where it is thrown from? My guess is that there are some event handler that listens to e.g. IContentEvent.PublishedContent and in the event handler re-saves that content instance (but without AccessLevel.NoAccess)

#194387
Jun 20, 2018 9:24
Vote:
 
<p>Sure enough, someone had added an event handler catching the publish of MediaData and modifying them, and that code doesn't use the AccessLevel.NoAccess parameter on its save.&nbsp; Thanks for your help.</p>
#194402
Jun 20, 2018 14:01
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.