Create Content and publish a Feed in CMP
Step 1: Go to this link: https://app.welcomesoftware.com/cloud/settings/integrations?activeTab=websiteAndFeed and Create/Select a Feed.
Please note that all content that has been published can be obtained via Feed URL (JSON type).
Step 2: Create Content for Publish
- Create a new task:
- In the Content Tab, create a new Text Editor:
- Add some Content/Text to the Text Editor
- Now in the Publishing Tab, select Add Channel, you can Schedule or Post the Content whenever it Completes the Task to the Channel
Scheduled Job in CMS Admin
The Idea of this process is to create a Page/Block that can handle "Feed URL" and then use a Scheduled Job to read and process the JSON response.
Note: This code can be applied to Alloy Demo, for other projects, you will need additional Code.
Step 1: Create a Page to handle the Feed URL
[ContentType(DisplayName = "Welcome Content page", GUID = "93B0F228-DB1E-4431-AB4B-6AA0EC34853F")]
public class WelcomeContentPage : StandardPage
{
[Display(GroupName = SystemTabNames.Content,Order = 1)]
public virtual Url LinkUrl { get; set; }
[Display(GroupName = SystemTabNames.Content,Order = 305)]
public virtual PageListBlock NewsList { get; set; }
public override void SetDefaultValues(ContentType contentType)
{
base.SetDefaultValues(contentType);
NewsList.Count = 20;
NewsList.Heading = "Heading";
NewsList.IncludeIntroduction = true;
NewsList.IncludePublishDate = true;
NewsList.Recursive = true;
NewsList.PageTypeFilter = typeof(WelcomeContentDataPage).GetPageType();
NewsList.SortOrder = FilterSortOrder.PublishedDescending;
}
}
public class WelcomeContentPageController : PageControllerBase<WelcomeContentPage>
{
public IActionResult Index(WelcomeContentPage currentPage)
{
var model = PageViewModel.Create(currentPage);
return View(model);
}
}
Please note that you need a Front end code (.cshtml) for it: Views/WelcomeContentPage/Index.cshtml
@model PageViewModel<Testing.Controllers.test.WelcomeContentPage>
@{ Layout = "~/Views/Shared/Layouts/_LeftNavigation.cshtml"; }
<div class="content">
<h1 @Html.EditAttributes(x => x.CurrentPage.PageName)>@Model.CurrentPage.PageName</h1>
<p class="lead" @Html.EditAttributes(x => x.CurrentPage.MetaDescription)>@Model.CurrentPage.MetaDescription</p>
<div class="clearfix">
@Html.PropertyFor(m => m.CurrentPage.MainBody)
</div>
@Html.PropertyFor(x => x.CurrentPage.NewsList)
</div>
@Html.PropertyFor(x => x.CurrentPage.MainContentArea, new { CssClass = "row" })
Note: "_LeftNavigation.cshtml" will show the left panel of the listing Item which is available in the Content Tree. In case this code is not available in your project, you can remove it.
Step 2: Create a SubPage
This page will be used to show data for CMP Content. Because there will be a lot of Content/Data will be shown and Published at the same time, each SubPage will handle data for one Content
[ContentType(DisplayName = "Welcome Data Content page", GUID = "2F8487A0-81EA-4643-8600-1C201DF80E41")]
public class WelcomeContentDataPage : StandardPageWelcomePage
{
public virtual string Title { get; set; }
public virtual XhtmlString ContentString { get; set; }
}
public class WelcomeContentDataPageController : PageControllerBase<WelcomeContentDataPage>
{
public IActionResult Index(WelcomeContentDataPage currentPage)
{
var model = PageViewModel.Create(currentPage);
return View(model);
}
}
Front end code: Views/WelcomeContentDataPage/Index.cshtml
@{ Layout = "~/Views/Shared/Layouts/_LeftNavigation.cshtml"; }
@model IPageViewModel<Testing.Controllers.test.WelcomeContentDataPage>
<h1>@Html.PropertyFor(x => x.CurrentPage.Title)</h1>
<div>
@Html.PropertyFor(x => x.CurrentPage.ContentString)
</div>
Step 3: Create a Scheduled Job:
Please go through this document for more details about the Scheduled Job: https://docs.developers.optimizely.com/content-management-system/docs/scheduled-jobs
[ScheduledPlugIn(DisplayName = "WelcomeIntegrationScheduledJob", GUID = "d6619008-3e76-4886-b3c7-12325a0c2603")] public class WelcomeIntegrationScheduledJob : ScheduledJobBase { private bool _stopSignaled; private readonly IClient _client; public WelcomeIntegrationScheduledJob(IClient client) { IsStoppable = true; _client = client; } public override void Stop() { _stopSignaled = true; } protected override void OnStatusChanged(string statusMessage) { base.OnStatusChanged(statusMessage); } public override string Execute() { //Call OnStatusChanged to periodically notify progress of job for manually started jobs OnStatusChanged(String.Format("Starting execution of {0}", this.GetType())); var typeRepo = ServiceLocator.Current.GetInstance(); var modelUsage = ServiceLocator.Current.GetInstance(); var repo = ServiceLocator.Current.GetInstance(); var parentPages = modelUsage.ListContentOfContentType(typeRepo.Load(typeof(WelcomeContentPage))).Select(x = x.ContentLink); foreach(var page in parentPages) { var itemrep = repo.TryGet(page, out var item); if(item.Status != VersionStatus.Published) { if (item.LinkUrl != null) { var welcomeURL = item.LinkUrl; var result = new HttpClient().GetStringAsync(welcomeURL.ToString()); int sleepCount = 0; if (result.Result == null && sleepCount <10) {
Thread.Sleep(1000);
sleepCount++;
} if (result.Result == null) { //Not able to get data from URL link continue; } var content = JObject.Parse(result.Result); var entries = content["entries"].ToArray(); repo.DeleteChildren(item.ContentLink, true, EPiServer.Security.AccessLevel.NoAccess); foreach (var entry in entries) { var contentItem = entry["content"]; var newPage = repo.GetDefault(item.ContentLink); newPage.Title = newPage.Name = contentItem.Value("title"); newPage.ContentString = new XhtmlString(contentItem.Value("description")); repo.Save(newPage, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess); } } } } //For long running jobs periodically check if stop is signaled and if so stop execution if (_stopSignaled) { return "Stop of job was called"; } return "Change to message that describes outcome of execution"; } }
Note: This Scheduled Job only copies the "content" field and "title" field. You can based on that then copy/create new fields that you need.
To sync data every minute, please enable Scheduled for the Job (10-20minutes per one) so that after you publish the content, data can automatically update to your CMS.
How to use
Step 1: Create a new Welcome Content Page:
Step 2: Add the Feed URL to the Link URL as an External Link then Save and Publish the Page.
Note: Each Feed URL Link should have a new WelcomeContentPage for it.
Step 3: Run the WelcomeIntegrationScheduledJob.
Please note this Scheduled Job will remove all content under WelcomeContentPage and add new Content from the Feed URL to the Content Tree
After the Scheduled Job is Completed, you can see that new content has been added to the WelcomeContentPage, you now can see it in Front End.
Please sign in to leave a comment.