Last active
April 12, 2016 13:14
-
-
Save fredrikhaglund/9a2ef1caeaa35f74d82440a68a650e93 to your computer and use it in GitHub Desktop.
This lab shows how to add a system integration job in EPiServer that loads a list of data and create, update and delete pages programmatically.
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
This lab shows how to add a system integration job in EPiServer that loads a list of data and create, update and delete pages programmatically. |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Web; | |
using EPiServer.DataAbstraction; | |
using EPiServer.PlugIn; | |
using System.Net.Http; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.DataAccess; | |
using EPiServer.Security; | |
using EPiServer.ServiceLocation; | |
using AlloyTraining.Models.Pages; | |
namespace AlloyTraining.Business.Jobs | |
{ | |
[ScheduledPlugIn(DisplayName = "Course Importer Job", | |
DefaultEnabled = false, | |
IntervalLength = 1, | |
IntervalType = ScheduledIntervalType.Hours)] | |
public class CourseImportJob | |
{ | |
public static string Execute() | |
{ | |
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
var startPage = contentRepository.Get<StartPage>(ContentReference.StartPage); | |
// Input validation - Check that we have resonable settings. | |
if (startPage.CourseListUrl == null) | |
{ | |
throw new Exception("Set CourseListUrl on start page"); | |
} | |
if (ContentReference.IsNullOrEmpty(startPage.CourseParentPage)) | |
{ | |
throw new Exception("Select parent page with CourseParentPage on start page"); | |
} | |
var url = startPage.CourseListUrl.Uri; | |
var courseListRootpage = startPage.CourseParentPage; | |
var webClient = new WebClient(); | |
var data = webClient.DownloadString(url); | |
var rows = data.Split(new string[] {"\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries); | |
var coursePages = contentRepository.GetChildren<ProductPage>(courseListRootpage).ToList(); | |
foreach (var row in rows) | |
{ | |
var cols = row.Split(';'); | |
var coursename = cols[0]; | |
var teacher = cols[1]; | |
var description = cols[2]; | |
var coursePage = coursePages.FirstOrDefault(c => c.Name == coursename); | |
if (coursePage == null) | |
{ | |
coursePage = contentRepository.GetDefault<ProductPage>(courseListRootpage); | |
} | |
else | |
{ | |
coursePages.Remove(coursePage); | |
coursePage = coursePage.CreateWritableClone() as ProductPage; | |
} | |
coursePage.Name = coursename; | |
coursePage.MetaDescription = description; | |
coursePage.UniqueSellingPoints = teacher; // Use this for Alloy demo templates: new[] {teacher}; | |
contentRepository.Save(coursePage, SaveAction.Publish, AccessLevel.NoAccess); | |
} | |
foreach (var coursePage in coursePages) | |
{ | |
contentRepository.Delete(coursePage.ContentLink, false, AccessLevel.NoAccess); | |
} | |
return string.Format("Done importing {0} courses", rows.Length); | |
} | |
} | |
} |
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
using System.ComponentModel.DataAnnotations; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.DataAbstraction; | |
using EPiServer.DataAnnotations; | |
using EPiServer.SpecializedProperties; | |
using EPiServerSite14.Models.Blocks; | |
namespace AlloyTraining.Models.Pages | |
{ | |
... | |
// Add the following properties to your Start Page Type. | |
[Display( | |
GroupName = "Course Import Settings")] | |
public virtual Url CourseListUrl { get; set; } | |
[Display( | |
GroupName = "Course Import Settings")] | |
public virtual ContentReference CourseParentPage { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment