Skip to content

Instantly share code, notes, and snippets.

@blacktambourine
Last active November 14, 2017 03:17
Show Gist options
  • Save blacktambourine/4f19a04d977ef13db80ab7a51660631c to your computer and use it in GitHub Desktop.
Save blacktambourine/4f19a04d977ef13db80ab7a51660631c to your computer and use it in GitHub Desktop.
Task Agent to call webservice for Json data
using Sitecore.Data;
using Sitecore.Sites;
using System;
using Sitecore.Configuration;
using Sitecore.ContentSearch;
namespace Flights.Tasks
{
public class FidsJsonTask
{
public string Message
{
get;
set;
}
//refactored to use a single database write; parsing is done by the indexer
public void Run()
{
Sitecore.Diagnostics.Log.Info(this + " : Start : " + this.Message, this);
var saveWatch = System.Diagnostics.Stopwatch.StartNew();
var response = Downloader.GetRawJson();
Database masterDb = Factory.GetDatabase("master");
var fidsItem = masterDb.GetItem("/sitecore/content/global/content/fids"); //RawJson content item
using (new Sitecore.SecurityModel.SecurityDisabler())
{
if (!string.IsNullOrEmpty(response))
{
fidsItem.Editing.BeginEdit();
try
{
fidsItem.Fields["RawJson"].Value = response;
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error(
"Could not update item " + fidsItem.Paths.FullPath + ": " + ex.Message, this);
fidsItem.Editing.CancelEdit();
}
finally
{
fidsItem.Editing.EndEdit();
}
}
saveWatch.Stop();
var saveElapsedMs = saveWatch.ElapsedMilliseconds;
Sitecore.Diagnostics.Log.Info("Publish Info Task - Save to DB Time: " + saveElapsedMs, this);
try
{
using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext("shell")))
{
//master database
var scItem = fidsItem;
if (scItem != null && scItem.HasChildren)
{
//Get all publishing target
var publishingTargets = Sitecore.Publishing.PublishManager.GetPublishingTargets(masterDb);
// 4) Loop through each publishing target and publish
foreach (var publishingTarget in publishingTargets)
{
var useDatabaseName = publishingTarget.Fields["Target database"].Value;
var webDatabase = Database.GetDatabase(useDatabaseName);
Sitecore.Publishing.PublishOptions publishOptions =
new Sitecore.Publishing.PublishOptions(
scItem.Database,
webDatabase,
Sitecore.Publishing.PublishMode.Full,
scItem.Language,
System.DateTime.UtcNow);
// Create a publisher with the publishoptions
var publisher = new Sitecore.Publishing.Publisher(publishOptions);
publisher.Options.RootItem = scItem;
// 5) Publish Sitecore child items if needed with Deep = true
publisher.Options.Deep = true;
publisher.Publish();
}
}
Sitecore.Diagnostics.Log.Info("Publish Info Task - Index Rebuild", this);
//force index refresh
var index = ContentSearchManager.GetIndex("flight_json_index");
if (index != null)
{
index.Refresh(new SitecoreIndexableItem(scItem), IndexingOptions.ForcedIndexing);
}
}
Sitecore.Diagnostics.Log.Info(this + " : End : " + this.Message, this);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error(this + " : Error : " + ex.Message, ex, this);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment