Subscribe to Blog Post being Published

Posted by Community Admin on 04-Aug-2018 17:24

Subscribe to Blog Post being Published

All Replies

Posted by Community Admin on 16-Nov-2017 00:00

Goal To grab the URL of a blog post from a specific blog when it has been published.

Hello, I am new to Sitefinity and have searched Google and the forums and can't quite find a good example or information on what I need to know.

The closest things I've found so far:
https://docs.sitefinity.com/for-developers-idataevent
https://knowledgebase.progress.com/articles/Article/Overwriting-the-default-LifecycleDecorator-for-a-Content-manager
https://docs.sitefinity.com/for-developers-dynamic-modules-events

So first question. For my needs, which route should I go between IDataEvent and IDynamicContentUpdatedEvent?

Second. Should I be subscribed to Updated or Created? I would assume Created only fires once and does not fire again when the post is published (no posts would be created in a published state). If Updated, I can tell if the post is published by checking its status or IsPublished. But how would I differentiate between an update to publish a post and an update to an already published post? I would only want to run the code once when the post is initially published.

Third. Assuming that the item is of type BlogPost, How can I tell what Blog it belongs to? For example, under Blogs there is Blog1 and Blog2. Would I get this from BlogPost.Parent.Title?

Fourth. Does BlogPost.ItemDefaultUrl return the canonical url of the post? Is it also full path or just relative?

Thanks!

Update 1

I'm currently trying the IDataEvent route but am hitting a roadblock. Here is the gist so far. I'm also still not seeing a way to determine the answer to question 2.

void Content_Action(IDataEvent @event)
    
        var action = @event.Action;
        var contentType = @event.ItemType;
        var itemId = @event.ItemId;
        var providerName = @event.ProviderName;
        var manager = ManagerBase.GetMappedManager(contentType, providerName);
        var item = manager.GetItemOrDefault(contentType, itemId);
        var blogType = TypeResolutionService.ResolveType("Telerik.Sitefinity.Blogs.Model.BlogPost");
        if (item.GetType().FullName == blogType.FullName && action.ToLower() == "updated") //Blog post that has been updated
         //Working up to this point
            //BlogPost post = (BlogPost)item; //Doesn't seem to work
            BlogsManager blogManager = BlogsManager.GetManager();
            BlogPost post = blogManager.GetBlogPost(itemId); //This doesn't seem to work either
            if (post != null)
             //Not getting in here
                if (post.Status == ContentLifecycleStatus.Live && post.Parent.Title.ToLower() == "blog1") //blog1 post that is published
                
                    string url = post.ItemDefaultUrl;
                    using (WebClient client = new WebClient())
                    
                        var parm = new System.Collections.Specialized.NameValueCollection();
                        parm.Add("URL", url);
                        client.UploadValues("--------------", "POST", parm);
                    
                
            
        
    

Posted by Community Admin on 21-Nov-2017 00:00

Solved

After trial and error, I've figured out the parameters that are only true when a blog post has initially been published. Also, the item was not null and the problem causing the code to not execute was unrelated. These are what to check for:

action == "New"
item.ApprovalWorkflowState == "Published"
item.Status == ContentLifecycleStatus.Live
item.Visible == true

Those are the parameters that will only exist the first time an unpublished post is published, with the kicker being the action being "New" and not "Updated".

 

This thread is closed