How do i update values in Blocks programmatically?

Vote:
 

Hi I have another specific feature i have to make:

I want to have a 'checkbox' in a block, that can be used to reset other properties of the same blocktype. I use a  page events InitializableModule that checks if a certain blocktype is updated:

 

var repo = ServiceLocator.Current.GetInstance();
            var content = repo.Get(e.ContentLink);

            try {
            
                if (content.ContentTypeID == Hint.Constants.PollBlockTypeID)
                {
                    PollBlock myPoll = content as PollBlock;
                    var clonePoll = (IContent) myPoll.CreateWritableClone();
                    bool resetValues = false;
                    Boolean.TryParse(clonePoll.Property["ResetVotes"].Value.ToString(), out resetValues);


                    if (resetValues)
                    {
                      
                        clonePoll.Property["CountAnswer1"].Value = 0;
                        clonePoll.Property["CountAnswer2"].Value = 0;
                        clonePoll.Property["CountAnswer3"].Value = 0;
                        clonePoll.Property["CountAnswer4"].Value = 0;
                        clonePoll.Property["CountAnswer5"].Value = 0;
                        clonePoll.Property["CountAnswerTotal"].Value = 0;
                        clonePoll.Property["VotedBy"].Value = string.Empty;
                        clonePoll.Property["ResetVotes"].Value = false;

                        repo.Save(clonePoll, SaveAction.Publish,AccessLevel.Publish);
                        e.CancelAction = false; //??
                        

                    }
                }
            }
            catch (Exception exc)
            {
            }

It all seems to work, however, because i change the block whilst it was active, it keeps asking me to publish the page. How can i properly update the Block and go back to previewing the block?

Thanks in advance

#141303
Nov 11, 2015 15:42
Vote:
 

Hi,

I don't know to which event you are attached, but I think that if you use ContentSaving event, than you don't need to create clone or save content - just reset all properties:

    [InitializableModule]
    public class CustomModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var contentEvents = context.Locate.ContentEvents();
            contentEvents.SavingContent += ContentEvents_SavingContent;
        }

        private void ContentEvents_SavingContent(object sender, ContentEventArgs e)
        {
            var pollBlock = e.Content as PollBlock;
            if (pollBlock == null || pollBlock.ResetVotes == false)
            {
                return;
            }

            pollBlock.CountAnswer1 = 0;
            pollBlock.CountAnswer2 = 0;
            pollBlock.CountAnswer3 = 0;
            pollBlock.CountAnswer4 = 0;
            pollBlock.CountAnswer5 = 0;
            pollBlock.CountAnswerTotal = 0;
            pollBlock.VotedBy = string.Empty;
            pollBlock.ResetVotes = false;
        }

        public void Uninitialize(InitializationEngine context)
        {

        }
    }
#141312
Nov 11, 2015 17:30
Vote:
 

Excellent it works! I was using the wrong event.Thanks so much

#141336
Nov 12, 2015 13:42
Vote:
 
#201182
Feb 08, 2019 8:57
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.