Wednesday, 27 February 2019

Sitecore publish programmatically not working

This is a quick post in which I will explain an interesting setting with sitecore publishing programmatically, I believe every once and while we need to publish sitecore item from code, the code for doing this is very simple but in my case and for some reason it doesn’t work?! 



How I can publish Item programmatically? 


I believe everyone had the need to publish item through code, following is a simple code snippet for a C# code to publish a sitecore item:

What if the above didn’t work?

The above code is executed such fine on my environment with no errors, Sitecore logs shows that the needed item is being published, for some reason I can’t see it on my publish target (web database)!

After a good time of investigation, I found the following settings in a patch file:

What does this setting mean? 

If you set Publishing.CheckSecurity to true, then members of the Sitecore Client Publishing role must have both read and write access in order to publish an item, by default this setting is false.

Security checking depends on the Publishing.CheckSecurity Setting in the Web.config file. When Publishing.CheckSecurity is set to true, Sitecore uses the SecurityEnabler in the publishing code and publishing will be performed under the context user.

So, in other words, when the publishing check security setting is enabled; Sitecore will check if the publishing user has the read/write access to the targeted item before publishing, even the security disabled code in the above code will not be useful, Following are a two-code snippet that can fix this:

Use user switcher to change the context of the publishing operation to a user with valid permissions. See line 3:


Or set publish options username to a valid user, see link 14:

Hope this will help someone!

No comments:

Post a Comment