I'm trying to publish a media item programmatically but I am encountering a problem with the PublishOptions object. At runtime, the line of code where the PublishOptions object is being instantiated is breaking when I upload a media item. I get the following message saying:
"Value cannot be null. Parameter Name: item"
And the stacktrace is:
at Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName)
at Sitecore.Data.Managers.ItemProvider.ApplySecurity(Item item, SecurityCheck securityCheck)
at Sitecore.Data.Managers.ItemProvider.GetRootItem(Language language, Version version, Database database, SecurityCheck securityCheck)
at Sitecore.Data.Managers.ItemManager.GetRootItem(Language language, Version version, Database database)
at Sitecore.Nexus.Data.DataCommands.ResolvePathCommand.(String itemPath, Database database)
at Sitecore.Nexus.Data.DataCommands.ResolvePathCommand.(String itemPath, Database database)
at Sitecore.Nexus.Data.NexusDataApi.ResolvePath(String itemPath, Database database)
at Sitecore.Data.Engines.DataCommands.ResolvePathCommand.DoExecute()
at Sitecore.Data.Engines.EngineCommand`2.Execute()
at Sitecore.Data.Engines.DataEngine.ResolvePath(String itemPath)
at Sitecore.Data.Managers.ItemProvider.ResolvePath(String itemPath, Database database)
at Sitecore.Data.Managers.ItemProvider.GetItem(String itemPath, Language language, Version version, Database database, SecurityCheck
Here's the code where the PublishOptions object is being instantiated (where it is breaking):
public void OnItemSaved(Object sender, EventArgs args)
{
var item = Event.ExtractParameter(args, 0) as Item;
using (new SecurityDisabler())
{
if (item != null)
{
if (item.Paths.IsMediaItem)
{
var source = new Database("master");
var target = new Database("web");
var options = new PublishOptions(source, target, PublishMode.SingleItem, item.Language, DateTime.Now)
{
RootItem = item,
Deep = true,
};
var publisher = new Publisher(options);
publisher.PublishAsync();
}
}
}
}
What could be the reason why I'm getting this error?
Try this and see if this works, I am guessing it is to with the database.
public void OnItemSaved(Object sender, EventArgs args)
{
var item = Event.ExtractParameter(args, 0) as Item;
using (new SecurityDisabler())
{
if (item != null)
{
if (item.Paths.IsMediaItem)
{
var source = Factory.GetDatabase("master");
var target = Factory.GetDatabase("web");
var options = new PublishOptions(source, target,
PublishMode.SingleItem, item.Language,
DateTime.Now)
{
RootItem = item,
Deep = true,
};
var publisher = new Publisher(options);
publisher.PublishAsync();
}
}
}
}