Hello All,
In our recent project, we encountered
a perplexing issue where an item in draft state was being published to the web
when our custom code was executed. Interestingly, during normal publishing, the
workflow was respected, and the item remained in draft state as expected. This
inconsistency led us to investigate further and seek help from the Sitecore
community.
The Issue
Our custom code was causing items in
draft state to be published, bypassing the workflow state. This was not the
case with normal publishing, where the workflow was correctly followed. The
challenge was to understand why the workflow state was not being respected when
using custom code.
Community Insight
After extensive investigation, we
posted our query on Sitecore Stack Exchange and received a prompt and
insightful response. The key points from the answer were:
- Execution Context: The custom code might be executed as a scheduled task or something
similar. These jobs run in the context of the "scheduler" site,
which does not respect workflows by default.
- Site Configuration: To ensure workflows are respected, you need to either select a
different site to run in context of or set the "scheduler" site
to enable workflows in your site configuration.
- SiteContextSwitcher: The recommended solution is to use a
SiteContextSwitcher to change to the "shell" site within your
code. This ensures that the workflow is enabled and respected during the
execution of your custom code.
Implementing the Solution
To resolve the issue, we implemented
the suggested solution using the SiteContextSwitcher. Here’s how you can do it:
C#
// Code Generated by Sidekick is for
learning and experimentation purposes only.
using(newSiteContextSwitcher(SiteContextFactory.GetSiteContext("shell")))
{
// Perform your publish logic here
}
//
Alternativelyusing(newSiteContextSwitcher(SiteContext.GetSite("shell")))
{
// Perform your publishing logic here
}
By switching the site context to
"shell", we ensured that the workflow state was respected, and items
in draft state were not published prematurely.
Additional Resources
For more detailed information and
further reference, you can visit the original Sitecore Stack Exchange post here.
Conclusion
This experience highlighted the
importance of understanding the execution context in Sitecore and how it can
impact workflow behavior. By leveraging the community's expertise and
implementing the SiteContextSwitcher, we were able to resolve the issue and
ensure our custom code respected the workflow state.
We hope this post helps others facing
similar issues and encourages them to seek support from the vibrant Sitecore
community.
I hope you enjoy this
Sitecore blog. Stay tuned for more Sitecore related articles.
Till that happy
Sitecoring :)
Please leave your comments or share this article if it’s useful for you.
No comments:
Post a Comment