BlogFeeds.ascx:-
<asp:Label ID="lblHeaderImage" runat="server"></asp:Label>
BlogFeeds.ascx.cs:-
using Microsoft.SharePoint;
using System;
using System.Net;
using System.ComponentModel;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
namespace BlogFeeds.BlogFeeds
{
[ToolboxItemAttribute(false)]
public partial class BlogFeeds : WebPart
{
public BlogFeeds()
{
}
private string rssUrl;
[Personalizable(true)]
[WebBrowsable()]
public virtual string RssUrl
{
get { return "http://ankushbhatia.wordpress.com/feed/"; }
set { rssUrl = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeControl();
}
private StringBuilder AddContainer(StringBuilder sbContent)
{
sbContent.Append("<div class='box2'>");
sbContent Append("<div class='head-icon'><img runat='server' width='60' height='60' src='/_layouts/15/BlogFeeds/blog-feed-icon.png' /></div>");
sbContent.Append("<div class='head-text'>");
sbContent.Append("Blog Feeds");
sbContent.Append("</div><div class='clear'></div>");
return sbContent;
}
private StringBuilder AddItemToContainer(XmlReader itemReader, StringBuilder sbContent)
{
string link = string.Empty; ;
string title = string.Empty;
string description = string.Empty;
while (itemReader.Read())
{
if (itemReader.NodeType == XmlNodeType.Element)
{
if (itemReader.Name == "link")
link = itemReader.ReadElementContentAsString();
else if (itemReader.Name == "title")
title = itemReader.ReadElementContentAsString();
else if (itemReader.Name == "description")
description = itemReader.ReadElementContentAsString();
}
}
sbContent.Append("<div class='icon'>");
sbContent.Append("<img src='/_layouts/15/BlogFeed/post-blog-icon.png' width='26' height='26' />");
sbContent.Append("</div>");
sbContent.Append("<div class='blog-text'>");
sbContent.Append("<span>");
sbContent.Append(title == null ? string.Empty : title);
sbContent.Append("</span><br />");
sbContent.Append(description == null ? string.Empty : description);
sbContent.Append("<div class='tol'>TOI</div>");
sbContent.Append("</div>");
sbContent.Append("<div class='clear'></div>");
return sbContent;
}
private void loadBlogFeeds()
{
StringBuilder sbContent= new StringBuilder();
int topFour = 0;
sbContent= AddContainer(sbContent);
sbContent Append("<div id='blogFeeds' class='blog-comment-box' style='width:675px; height:290px; overflow:scroll;'>");
try
{
using (XmlReader reader = XmlReader.Create(RssUrl))
{
reader.MoveToContent();
reader.ReadToDescendant("channel");
reader.ReadToDescendant("item");
do
{
using (XmlReader itemReader = reader.ReadSubtree())
{
sbContent= AddItemToContainer(itemReader, sbContent);
topFour++;
}
} while (reader.ReadToNextSibling("item") && topFour < 4);
sbContent.Append("<div class='clear'></div>");
sbContent.Append("</div>");
sbContent.Append("<a href='" + RssUrl + "' class='view-all-btn'>View All</a>");
sbContent.Append("<div class='clear'></div>");
sbContent.Append("</div>");
}
lblHeaderImage.Text = sbContent.ToString();
}
catch (Exception ex)
{
sbContent.Append("Exception " + ex.Message + " while reading rss at " + RssUrl);
sbContent.Append("</div><div class='clear'></div></div>");
lblHeaderImage.Text = sbContent.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
loadBlogFeeds();
}
}
}
<asp:Label ID="lblHeaderImage" runat="server"></asp:Label>
BlogFeeds.ascx.cs:-
using Microsoft.SharePoint;
using System;
using System.Net;
using System.ComponentModel;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
namespace BlogFeeds.BlogFeeds
{
[ToolboxItemAttribute(false)]
public partial class BlogFeeds : WebPart
{
public BlogFeeds()
{
}
private string rssUrl;
[Personalizable(true)]
[WebBrowsable()]
public virtual string RssUrl
{
get { return "http://ankushbhatia.wordpress.com/feed/"; }
set { rssUrl = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeControl();
}
private StringBuilder AddContainer(StringBuilder sbContent)
{
sbContent.Append("<div class='box2'>");
sbContent Append("<div class='head-icon'><img runat='server' width='60' height='60' src='/_layouts/15/BlogFeeds/blog-feed-icon.png' /></div>");
sbContent.Append("<div class='head-text'>");
sbContent.Append("Blog Feeds");
sbContent.Append("</div><div class='clear'></div>");
return sbContent;
}
private StringBuilder AddItemToContainer(XmlReader itemReader, StringBuilder sbContent)
{
string link = string.Empty; ;
string title = string.Empty;
string description = string.Empty;
while (itemReader.Read())
{
if (itemReader.NodeType == XmlNodeType.Element)
{
if (itemReader.Name == "link")
link = itemReader.ReadElementContentAsString();
else if (itemReader.Name == "title")
title = itemReader.ReadElementContentAsString();
else if (itemReader.Name == "description")
description = itemReader.ReadElementContentAsString();
}
}
sbContent.Append("<div class='icon'>");
sbContent.Append("<img src='/_layouts/15/BlogFeed/post-blog-icon.png' width='26' height='26' />");
sbContent.Append("</div>");
sbContent.Append("<div class='blog-text'>");
sbContent.Append("<span>");
sbContent.Append(title == null ? string.Empty : title);
sbContent.Append("</span><br />");
sbContent.Append(description == null ? string.Empty : description);
sbContent.Append("<div class='tol'>TOI</div>");
sbContent.Append("</div>");
sbContent.Append("<div class='clear'></div>");
return sbContent;
}
private void loadBlogFeeds()
{
StringBuilder sbContent= new StringBuilder();
int topFour = 0;
sbContent= AddContainer(sbContent);
sbContent Append("<div id='blogFeeds' class='blog-comment-box' style='width:675px; height:290px; overflow:scroll;'>");
try
{
using (XmlReader reader = XmlReader.Create(RssUrl))
{
reader.MoveToContent();
reader.ReadToDescendant("channel");
reader.ReadToDescendant("item");
do
{
using (XmlReader itemReader = reader.ReadSubtree())
{
sbContent= AddItemToContainer(itemReader, sbContent);
topFour++;
}
} while (reader.ReadToNextSibling("item") && topFour < 4);
sbContent.Append("<div class='clear'></div>");
sbContent.Append("</div>");
sbContent.Append("<a href='" + RssUrl + "' class='view-all-btn'>View All</a>");
sbContent.Append("<div class='clear'></div>");
sbContent.Append("</div>");
}
lblHeaderImage.Text = sbContent.ToString();
}
catch (Exception ex)
{
sbContent.Append("Exception " + ex.Message + " while reading rss at " + RssUrl);
sbContent.Append("</div><div class='clear'></div></div>");
lblHeaderImage.Text = sbContent.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
loadBlogFeeds();
}
}
}
Thank You.
No comments:
Post a Comment