Custom property on a page

Vote:
 

So I am letting users authenticate from an external api. When these users create a page I just have CreatedBy in the currentPage instance. 

What I am trying to do is to add a property to the page which will be fullfilled by the PageCreatedEvent programtically and not be allowed to tempered with from te admin view.

I have this 

        public void Initialize(InitializationEngine context)
        {
            var events = context.Locate.ContentEvents();
            events.CreatedContent += CreatedContent;
        }


        private void CreatedContent(object sender, ContentEventArgs e)
        {
            var articlePage = e.Content as ArticlePage;
            if (articlePage != null)
            {
                // TODO: ...
            }
        }

Is there a way that I can create a property and make it hidden on the page edit page? And then how do I store it programmitcally? 

Thanks

#187321
Jan 19, 2018 10:12
Vote:
 

I don't know if there is a built-in way to hide a property in edit-mode, you might have to write your own renderer for that.

If it doesn't have to be hidden the editable attribute works fine and it will get stored automatically when you save.

[Editable(false)]
#187350
Jan 19, 2018 16:24
Vote:
 

You can use ScaffoldColumn to hide the property in edit-mode.

[ScaffoldColumn(false)]



#187362
Edited, Jan 21, 2018 13:22
Vote:
 

I have tried both the ways suggested above. It does hides the propety from the admin panel but when in my Page created event I try to store the value programmatically it throws error

The property Creator is read-only

I am trying this

        [ScaffoldColumn(false)]
        [Display(GroupName = SystemTabNames.Content,
        Name = "Creator")]
        public virtual string Creator { get; set; }
articlePage.Creator = "my value";
#187366
Jan 22, 2018 8:18
Vote:
 

First or all, which error does it throw? Dare I guess about the content is read only?

Secondly, mind the difference between the events PublishingContent and PublishedContent.

Publishing is triggered BEFORE the changes are published and saved to the database. You have time to make some final adjustments before the content is published.

Published is triggered after the content has been saved and published. You will need to create a writable clone and publish the content again.

#187367
Jan 22, 2018 8:33
Vote:
 

Thanks for the response. Now I am trying this,

        private void EventPublishing(object sender, ContentEventArgs e)
        {
            var repo = ServiceLocator.Current.GetInstance<IContentRepository>();

            if (repo == null)
                return;

            var articlePage = e.Content as ArticlePage;

            if (articlePage == null)
                return;

            if (RystadIdentity.Current == null)
                return;


            articlePage.Creator = RystadIdentity.Current.AuthenticatedUser.UserInfo.id;
            repo.Save(articlePage.CreateWritableClone());

        }

But the property Creator is null when I check it. Am I doing something wrong here?

#187368
Jan 22, 2018 8:41
Vote:
 

Hi,

You say that this page is created programmatically?

Could it be that the user has not been authenticated yet at the time you're creating the page?

Depending on how you handle your authentication just because you have performed the "authenticate" logic, it doesn't mean that the Identity.Current is updated with the current user information until your second request. Doesn't mean that it's the problem you have now but a guess.

#187370
Jan 22, 2018 8:45
Vote:
 

Hi,

No the page is created in the admin mode but I just a property to the page programmatically in the publishing content event. And just to be sure I have added a break point and I do have the value there but it is not stored for some reason. 

#187371
Jan 22, 2018 8:48
Vote:
 

Now the issue is that because I am in the publishing event and when I save it gets into an infinite loop. 

        private void EventPublishing(object sender, ContentEventArgs e)
        {
            var repo = ServiceLocator.Current.GetInstance<IContentRepository>();


            if (repo == null)
                return;

            var articlePage = e.Content as ArticlePage;

            if (articlePage == null)
                return;

            if (RystadIdentity.Current == null)
                return;

            var clone = articlePage.CreateWritableClone() as ArticlePage;

            clone.Creator = RystadIdentity.Current.AuthenticatedUser.UserInfo.id;


            repo.Save(clone, SaveAction.Publish);
        }

When I save it again comes into the publishing event and the same cycle continues.. 

#187372
Jan 22, 2018 8:57
Vote:
 

So to solve the loop I had to user 

repo.Save(clone, SaveAction.Patch);

And the reason why the property was not being updated was that I was using 

[Ignore]

as an attribute, I removed it and used 

[ScaffoldColumn(false)]

to hide it from the edit view. It works. :)

#187373
Jan 22, 2018 9:03
Vote:
 

When you are in a publishing event then you should not call repo.Save(). When you edit a attribute in a *ing event then the value will get saved. Also, you don't need to create a writeableclone as the Object allready is in a writeable state.

private void EventPublishing(object sender, ContentEventArgs e)
        {
            var articlePage = e.Content as ArticlePage;

            if (articlePage == null)
                return;

            if (RystadIdentity.Current == null)
                return;

            articlePage.Creator = RystadIdentity.Current.AuthenticatedUser.UserInfo.id;

        }

That should work if I don't remember totaly wrong :)

#187377
Jan 22, 2018 9:55
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.