Posted on 04/11/2019 12:52:47
The entire code is listed below. I have done a little additional testing with three scenarioes:
Test 1:
- Build
- Activate controller action
Test 2:
- Build
- Login
- Activate controller action
Test 3:
- Build
- Login
- Logout
- Activate
I stated in my previous post that the page service does not work, if no user is logged into the backend, which was based on running Test 1 and Test 2. However, in Test 3 the page service works as well, so it seems that it is not whether a user is logged in or not that determines whether the page service will work, but rather if the page has just been restarted.
using Website.CustomModules.NotificationSubscribers;
using System;
using System.Collections.Generic;
using System.Web.Http;
using Dynamicweb.Content;
using Website.CustomModules.Extensions;
using Website.CustomModules.Models;
using System.Linq;
using Website.CustomModules.Helpers;
using Website.CustomModules.Services;
namespace Website.CustomModules.Controllers
{
[RoutePrefix(WebApiEnabler.RoutePrefix + "/joboffer")]
public class JobOffersController : ApiController
{
private readonly IPageService _pageService = Dynamicweb.Services.Pages;
private readonly IItemService _itemService = Dynamicweb.Services.Items;
private readonly IAreaService _areaService = Dynamicweb.Services.Areas;
[HttpGet]
[Route("GetJobOffers")]
public IHttpActionResult GetJobOffers()
{
var logger = new Logger("JobOffers");
var areas = _areaService.GetAreas();
foreach (var area in areas)
{
if (area.Item == null || area.Item["JobListIds"] == null || area.Item["JobListLanguageId"] == null)
{
logger.AppendLine("Item or one or more job list properties are null on area with ID " + area.ID);
continue;
}
var jobOffersParentPage = _pageService.GetPageByNavigationTag(area.ID, Constants.JobOffersNavigationTag);
if (jobOffersParentPage == null)
{
logger.AppendLine("Job offers parent page does not exist on area with ID " + area.ID);
continue;
}
// delete all job offers currently existing in the database before import
var existingJobOffers = jobOffersParentPage.GetChildren();
foreach (var jobOffer in existingJobOffers)
{
_pageService.DeletePage(jobOffer.ID);
}
logger.AppendLine("Existing job offers deleted from area with ID " + area.ID);
// get the language id and the ids for the lists to import stated in the area settings
string jobOfferListIds = area.Item["JobListIds"].ToString();
string jobListLanguageId = area.Item["JobListLanguageId"].ToString();
// import the job offers matching the criteria
if (jobOfferListIds.Any() && !string.IsNullOrWhiteSpace(jobListLanguageId))
{
List<JobOffer> jobOffers = new List<JobOffer>();
var jobOfferListUrl = System.Configuration.ConfigurationManager.AppSettings["JobOfferListUrl"] + "?JobListId={0}&Language={1}";
// convert XML to list of JobOffer models
if (!string.IsNullOrWhiteSpace(jobOfferListIds))
{
foreach (var i in jobOfferListIds.Split(','))
{
IEnumerable<JobOffer> jobOfferList = JobOfferService.GetJobOffers(i, jobListLanguageId);
if (jobOfferList.Count() > 0)
{
jobOffers.AddRange(jobOfferList);
logger.AppendLine("Retrieved " + jobOfferList.Count() + " job offer(s) with list ID " + i + " and language code " + jobListLanguageId + " for area with ID " + area.ID);
}
else
{
logger.AppendLine("List with ID " + i + " and language code " + jobListLanguageId + " contained no job offers");
}
}
}
try
{
// create each job offer as a page
foreach (var jobOffer in jobOffers)
{
// create the page item
var itemType = Constants.JobOfferItemTypeSystemName;
var item = new Dynamicweb.Content.Items.Item(itemType)
{
new KeyValuePair<string, object>("JobId", jobOffer.JobOfferInstanceId),
new KeyValuePair<string, object>("Title", jobOffer.JobTitle),
new KeyValuePair<string, object>("Description", jobOffer.Description),
new KeyValuePair<string, object>("ApplicationUrl", jobOffer.ApplyForJobUrl),
new KeyValuePair<string, object>("OfferType", jobOffer.JobListId)
};
var savedItem = _itemService.SaveItem(item);
// create the page and refer to the page item
var page = new Page
{
AreaId = 1,
ParentPageId = jobOffersParentPage.ID,
MenuText = jobOffer.JobTitle,
ItemId = savedItem.Id,
ItemType = itemType
};
var savedPage = _pageService.SavePage(page);
}
logger.AppendLine("Added " + jobOffers.Count() + " job offers to area with ID " + area.ID);
}
catch (Exception e)
{
logger.AppendLine("Exception thrown while adding job offers to area with ID " + area.ID);
logger.AppendLine(e.ToString());
}
}
}
logger.Log();
return Ok();
}
}
}
Best regards,
Roald