Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sitefinity twitter MVC widget
@model SitefinityWebApp.Mvc.Models.Twitter.TwitterModel
@using Telerik.Sitefinity.Frontend.Mvc.Helpers;
@Html.Script("/Mvc/Views/Twitter/Resources/twitter.js", "bottom")
<div id="twitter-feed" class="sfTweetsList" data-serviceurl="@Model.ServiceUrl">
<div class="sfTweetItem">
<h6>Latest Tweet</h6>
<div class="sfTweetWrp">
</div>
</div>
</div>
$(document).ready(function () {
var wrapper = $("#twitter-feed");
var service = wrapper.data("serviceurl");
$.getJSON(service).then(function (data) {
if (data.Items.length > 0) {
var tweet = data.Items[0].dynamicProperties;
var username = tweet.filter(tweet => tweet.Key === "Username")[0].Value;
var date = wcftimeconvert(tweet.filter(tweet => tweet.Key === "LastModified")[0].Value);
var time = wcfformatAMPM(date);
var dateString = date.toLocaleDateString();
var content = tweet.filter(tweet => tweet.Key === "Content")[0].Value;
var link = tweet.filter(tweet => tweet.Key === "Link")[0].Value;
var html = "<b class='sfTweetAuthor'>" + username + "</b> <i class='sfTweetDate'>" + dateString + " " + time + "</i>\
<p class='sfTweetContent'>"+content+"</p>\
<a href='"+link+"' class='sfExpand'>Expand</a>";
wrapper.find(".sfTweetWrp").html(html);
} else {
wrapper.hide();
}
});
});
function wcftimeconvert(ds) {
var D, dtime, T, tz, off,
dobj = ds.match(/(\d+)|([+-])|(\d{4})/g);
T = parseInt(dobj[0]);
tz = dobj[1];
off = dobj[2];
if (off) {
off = (parseInt(off.substring(0, 2), 10) * 3600000) +
(parseInt(off.substring(2), 10) * 60000);
if (tz == '-') off *= -1;
}
else off = 0;
return new Date(T += off);
}
function wcfformatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
using SitefinityWebApp.Mvc.Models.Twitter;
using System;
using System.Web.Mvc;
using Telerik.Sitefinity.Mvc;
namespace SitefinityWebApp.Mvc.Controllers
{
[ControllerToolboxItem(Name = "Twitter_MVC", Title = "Twitter", SectionName = "CustomWidgets")]
public class TwitterController : Controller
{
// GET: Twitter
public ActionResult Index()
{
var model = new TwitterModel(this.ServiceUrl, this.TwitterPipeId);
return View(model);
}
protected override void HandleUnknownAction(string actionName)
{
// Handles the Action and invokes the Index
this.ActionInvoker.InvokeAction(this.ControllerContext, "Index");
}
private string _serviceUrl = "~/Services/Content/PublishingDataService.svc/";
public string ServiceUrl
{
get { return _serviceUrl; }
set
{
_serviceUrl = value;
}
}
private string _twitterPipeId = "4e608c09-9d42-622b-8544-ff00009b8a11";
public string TwitterPipeId
{
get { return _twitterPipeId; }
set
{
_twitterPipeId = value;
}
}
}
}
/* ------------------------------------------------------------------------------
<auto-generated>
This file was generated by Sitefinity CLI v1.0.0.2
</auto-generated>
------------------------------------------------------------------------------ */
using System;
using System.Web;
using System.Web.Mvc;
using Telerik.Sitefinity.Mvc;
using System.Linq;
using Telerik.Sitefinity.Publishing;
using Telerik.Sitefinity.Publishing.Model;
namespace SitefinityWebApp.Mvc.Models.Twitter
{
public class TwitterModel
{
public TwitterModel(string svcUrl, string twitterpipeId)
{
string absolute = VirtualPathUtility.ToAbsolute(svcUrl);
PipeSettings pipeSetting = (
from ps in PublishingManager.GetManager().GetPipeSettings()
where ps.Id == new Guid(twitterpipeId)
select ps).FirstOrDefault<PipeSettings>();
if (pipeSetting == null)
{
//Try to search by name
pipeSetting = (
from ps in PublishingManager.GetManager().GetPipeSettings()
where ps.PipeName.Contains("twitter")
orderby ps.LastModified descending
select ps).FirstOrDefault<PipeSettings>();
}
if (pipeSetting != null)
{
object[] str = new object[] { absolute, null, null, null };
str[1] = pipeSetting.PublishingPoint.Id.ToString();
str[2] = "/?take=";
str[3] = 1;
absolute = string.Concat(str);
}
this.ServiceUrl = absolute;
}
public string ServiceUrl { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.