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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<setting name="Publishing.CheckSecurity"> | |
<patch:attribute name="value">true</patch:attribute> | |
</setting> |
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.
Use user switcher to change the context of the publishing operation to a user with valid permissions. See line 3:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void PublishItem(Sitecore.Data.Items.Item item)
{
using (new UserSwitcher("sitecore\\admin"))
{
// The publishOptions determine the source and target database,
// the publish mode and language, and the publish date
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);
// Choose where to publish from
publisher.Options.RootItem = item;
// Publish children as well?
publisher.Options.Deep = true;
// Do the publish!
publisher.PublishAsync();
item.Publishing.ClearPublishingCache();
}
}
Or set
publish options username to a valid user, see link 14:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void PublishItem(Sitecore.Data.Items.Item item)
{
using (new SecurityDisabler())
{
// The publishOptions determine the source and target database,
// the publish mode and language, and the publish date
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
publishOptions.UserName = "sitecore\\admin";
Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions);
// Choose where to publish from
publisher.Options.RootItem = item;
// Publish children as well?
publisher.Options.Deep = true;
// Do the publish!
publisher.PublishAsync();
item.Publishing.ClearPublishingCache();
}
}
Hope this will help someone!
No comments:
Post a Comment