Turn Off Content Approvals on AssetFolders Or Uploaded Media

GOSSO
Member since: 2007
 

CMS 10.10

I'm trying to disable programmatically the legacy of populating down Approval Sequence to local asset folder "for this page". 

"ApprovalDefinitionOperationException: Not allowed to create a ContentApprovalDefinition inside a local asset folder"

Even if I have a Approval Sequence on a page, I don't want its behavior on un uploaded image under "for this page", I want the image to be published automatically. 

                var folder = contentAssetHelper.GetOrCreateAssetFolder(page.ContentLink);

                if (folder != null)
                {
                    Task.Run(async () => { await (DisableContentApprovalDefinition(folder.ContentLink)); }).Wait(); 
                }

        public async Task DisableContentApprovalDefinition(ContentReference contentLink)
        {
            IApprovalDefinitionRepository definitionRepository= ServiceLocator.Current.GetInstance();

            // Gets a definition
            ApprovalDefinition definition = await definitionRepository.GetAsync(contentLink);
            if (definition == null) // allways null
            {
                definition = new ContentApprovalDefinition
                {
                    ContentLink = contentLink
                };
            }
            else
                definition = definition.CreateWritableClone();


            definition.IsEnabled = false;
            // Saves a definition
            await definitionRepository.SaveAsync(definition);
        }

Any ideas?

#183469 Edited, Oct 16, 2017 9:48
  • GOSSO
    Member since: 2007
     

    The approvals wasn't the problem, it was the parents publish rights. Solved it by automatically publishing it when created:

            public void Initialize(InitializationEngine context)
            {
                IContentEvents events = ServiceLocator.Current.GetInstance<IContentEvents>();
                events.CreatedContent += CreatedContent;
            }
    
        
            private void CreatedContent(object sender, ContentEventArgs e)
            {
    
                if (e.Content != null && e.Content is ImageData)
                {
                    SetPublished(e.Content as ImageData);
                }
            }
    
        private void SetPublished(ImageData content)
            {
                if (content.IsPendingPublish)
                {
                    var clone = content.CreateWritableClone() as ImageData;
                    var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
                    contentRepository.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);
                }
            }
    
    #183470 Oct 16, 2017 10:33