2

I have a Sitecore item that isn't getting published to the web database programmatically.

I've checked the Publishing Queue at the database level and the item is there. It gets added to run at 6:15am everyday, but does not get run.

I have it set to programmatically alter the update date of the Sitecore Item.

When I publish this item manually, it moves to the web database successfully and sets a new version.

I'm wondering what steps can I take to troubleshoot? Why is this item not being successfully published?

I am using the below code to run a publish https://sitecoreinfo.blogspot.com/2019/02/sitecore-publish-programmatically-not.html

The item being updated is part of a page, but is not the whole page. The item has HTML that is being programmatically altered.

4
  • What is the code that you use for publishing the item?
    – Marek Musielak
    Commented May 8, 2023 at 6:25
  • Added to the question
    – Shay Maor
    Commented May 8, 2023 at 19:39
  • You added a link to a blog post which contains 3 different versions of code. Which one do you use? Do you use user switcher?
    – Marek Musielak
    Commented May 9, 2023 at 6:01
  • I'm using the first snippet of code
    – Shay Maor
    Commented May 11, 2023 at 2:43

2 Answers 2

1

Here are a few pointers that will help to troubleshoot.

  1. Verify that your item is in the final workflow state, if a workflow is specified. If no workflow is specific, then workflow state should be empty.
  2. Verify that your item is publishable, or if a particular version is not getting published, make sure the version is publishable.
  3. Make sure your publishing targets are set correctly. By default, no publishing targets should be selected, which means Sitecore will deploy to all publishing targets. View the raw value of the Publishing Targets field, as sometimes an old, non-existing publishing target is specified and the UI will not show it. If there are any values specified for Publishing Targets, the best way to clear it is to reset the field.
  4. Make sure your publishing targets are setup correctly. Typically we have one publishing target named Internet and the target database is web.
  5. Make sure your item name is valid. Sometimes we update the invalid characters list after items have been created.
  6. Make sure none of the parent items fail to publish.

Once you have verified all of these, you should be able to publish the item and it should show in the web database. If your item is in the web database and you are still unable to access the item via the web, check these items:

  • Make sure your item name is valid. Sometimes we update the invalid characters list after items have been created and published. Item names with hyphens in particular usually cause issues.
  • Make sure there aren’t any Sitecore Aliases are interfering (see /sitecore/system/Aliases).

https://blog.martinmiles.net/post/item-not-published-troubleshooting

Also if your code is executed with no error then update the Publishing.CheckSecurity to true and validate your code with below.

<setting name="Publishing.CheckSecurity">
    <patch:attribute name="value">true</patch:attribute>
</setting>
private void PublishItem(Sitecore.Data.Items.Item item)
{
        using (new SecurityDisabler())
        {

                Sitecore.Publishing.PublishOptions publishOptions =
                new Sitecore.Publishing.PublishOptions(item.Database,
                             Database.GetDatabase("web"),
                             Sitecore.Publishing.PublishMode.SingleItem,
                             item.Language,
                             System.DateTime.Now);  // Create a publisher with the publishoptions


                Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions);

                publisher.Options.RootItem = item;

                publisher.Options.Deep = true;

                publisher.PublishAsync();
        }
}

https://sitecoreinfo.blogspot.com/2019/02/sitecore-publish-programmatically-not.html

Hope it helps!

1
  • I tried all the debug steps, but no dice. I want to add that a manual publish does successfully create a new version and it gets published to the web database. But the programmatic method (first snippet in your link that I used) does little other than pushing the item to the queue. It is not processed oddly.
    – Shay Maor
    Commented May 11, 2023 at 2:45
0

In your comment you wrote that you're using first snippet from the article.

First snippet will not work if you're running the code as a user which has not enough access rights, so e.g. running a scheduled task, despite the fact that there is SecurityDisabler used.

Instead of using SecurityDisabler change your code to use UserSwitcher or include username in publish options, like it's shown in the article you mention:

https://sitecoreinfo.blogspot.com/2019/02/sitecore-publish-programmatically-not.html

So either:

using (new UserSwitcher("sitecore\\admin"))

or

publishOptions.UserName = "sitecore\\admin";

And if you want to be able to see that it was your backend code that published the item, you may want to create a new Sitecore user, assign access rights for that user and use that user while publishing.

1
  • I'll try this out. Thanks!
    – Shay Maor
    Commented May 13, 2023 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.