Thursday, July 5, 2012

Custom application page for the sharepoint RSS feeds.




Hello in this topic we will develope the custom application page for the sharepoint rss feeds.

- Here the different news(with different content type(1. Sharepoint News Page and 2.Sharepoint Article Page)) is associated with the Sharepoint Pages library.
And we have created the page library inside the "NewsSite" site.so my news site url will like "http://YourServer:Port/NewsSite/"

- Published date for the "Sharepoint News Page" content type is stored in "PressReleasePublihshedDate" field and
Published date for the "Sharepoint Article Page" content type is stored in the "PublihshedDate" field.

So above two types of news are stored in the SharePoint pages library.

- We have enabled the "Allow RSS for this list" settings for the Pages library.

- You can enabled it by go to the list settings - > go to the Communications section and click on the Rss Settings - >
  set the "Allow RSS for this list? " is true and press OK.
 
- We will display the news of both the content type together for the rss feeds.

- We will use the custom application page for that and stored it in the "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS" location.

- We will use the application page name as "SharepointCustomRSSFeeds.aspx"

- Now add the following line of code inside the aspx page.

- So SharepointCustomRSSFeeds.aspx application page will look like below

/*******************************************************************************************************************************/
<%@ Page Language="C#" %>

<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %>
<script runat="server">
public void Page_Load()
{
Response.Clear();
Response.ContentType = "text/xml";

try
{          
Response.Write(
GetRSSFromListView(
Request.QueryString["List"],
Request.QueryString["View"])
);
}
catch (Exception ex)
{
Response.Write("<pre>");
Response.Write(ex.ToString());
Response.Write("</pre>");
}
}
 
private static string GetRSSFromListView(string strListGUID, string strViewGUID)
{
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\"?>");
sb.Append("<rss version=\"2.0\">");
Guid listGuid = new Guid(
HttpContext.Current.Server.UrlDecode(strListGUID));
string siteUrl = SPContext.Current.Web.Url;
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = null;
try
{ list = web.Lists[listGuid]; }
catch { list = null; }
if (list != null)
{
SPView view = null;
if (strViewGUID != null)
{
Guid viewGuid = new Guid(
HttpContext.Current.Server.UrlDecode(strViewGUID));
view = list.Views[viewGuid];
}
else
{
view = list.DefaultView;
}
sb.Append("<channel>");
AddTag("title", list.Title, sb);
AddTag("description", list.Description, sb);
SPListItemCollection items = GetTheRssRecords(list);
DataTable FianlResults1 = MergedNewsRecords(items);
DataTable FianlResults = RecordsWithRFCDateFormat(FianlResults1);
string str="";
string str1="";
if (FianlResults.Rows.Count > 0)
{
foreach (DataRow row in FianlResults.Rows)
{
sb.Append("<item>");
AddTag("link", Convert.ToString(row["FileRef"]), sb);
AddTag("title", Convert.ToString(row["Title"]), sb);
IFormatProvider culture = new CultureInfo(SPContext.Current.Web.UICulture.ToString(), true);

str="<pubDate>";
str+=Convert.ToString(row["PublishedDate"]);
str+="</pubDate>";

sb.Append(str);

AddTag("description",Convert.ToString("<b style='font-size:12px'>"+row["PublishedDate"]+"</b>"), sb);

sb.Append("</item>");
}
}
sb.Append("</channel>");
sb.Append("</rss>");
}
}
}
return sb.ToString();
}
private static void AddTag(string tagText, string tagValue, StringBuilder sb)
{
sb.Append("<");
sb.Append(tagText.Replace(" ", "_"));
sb.Append(">");
sb.Append(HttpContext.Current.Server.HtmlEncode(tagValue));
sb.Append("</");
sb.Append(tagText.Replace(" ", "_"));
sb.Append(">");

}

public static DataTable MergedNewsRecords(SPListItemCollection items)
{
DataTable dtFinalRecords = new DataTable();
DataColumn Title = new DataColumn("Title", typeof(string));
DataColumn FileRef = new DataColumn("FileRef", typeof(string));
DataColumn PublishedDate = new DataColumn("PublishedDate", typeof(DateTime));
dtFinalRecords.Columns.Add(Title);
dtFinalRecords.Columns.Add(FileRef);
dtFinalRecords.Columns.Add(PublishedDate);
try
{
foreach (SPListItem item in items)
{
if (item.File.Item.ContentType.Name == "Sharepoint News Page" && !string.IsNullOrEmpty(Convert.ToString(item.Properties["PressReleasePublihshedDate"])))
{
DataRow rowFinal = dtFinalRecords.NewRow();
rowFinal["Title"] = item.File.Title;
rowFinal["FileRef"] = item.File.ServerRelativeUrl;
rowFinal["PublishedDate"] = Convert.ToDateTime(SPUtility.CreateDateTimeFromISO8601DateTimeString(item.Properties["PressReleasePublihshedDate"].ToString()));
dtFinalRecords.Rows.Add(rowFinal);
}
else if (item.File.Item.ContentType.Name == "Sharepoint Article Page" && !string.IsNullOrEmpty(Convert.ToString(item.Properties["PublihshedDate"])))
{
DataRow rowFinal = dtFinalRecords.NewRow();
rowFinal["Title"] = item.File.Title;
rowFinal["FileRef"] = item.File.ServerRelativeUrl;
rowFinal["PublishedDate"] = Convert.ToDateTime(SPUtility.CreateDateTimeFromISO8601DateTimeString(item.Properties["PublihshedDate"].ToString()));
dtFinalRecords.Rows.Add(rowFinal);
}
}
if (dtFinalRecords.Rows.Count > 0)
{
DataView viewResults = new DataView(dtFinalRecords);
viewResults.Sort = "PublishedDate DESC";
dtFinalRecords = viewResults.ToTable();
}
}
catch { }
return dtFinalRecords;
}

public static DataTable RecordsWithRFCDateFormat(DataTable items)
{
DataTable dtFinalRecords = new DataTable();
DataColumn Title = new DataColumn("Title", typeof(string));
DataColumn FileRef = new DataColumn("FileRef", typeof(string));
DataColumn PublishedDate = new DataColumn("PublishedDate", typeof(string));
dtFinalRecords.Columns.Add(Title);
dtFinalRecords.Columns.Add(FileRef);
dtFinalRecords.Columns.Add(PublishedDate);
try
{
if (items != null && items.Rows.Count > 0)
{
foreach (DataRow row in items.Rows)
{
DataRow rowFinal = dtFinalRecords.NewRow();
rowFinal["Title"] = row["Title"];
rowFinal["FileRef"] = row["FileRef"];
rowFinal["PublishedDate"] = Convert.ToDateTime(row["PublishedDate"]).ToString("dddd, d MMM yyyy hh:mm:ss");
dtFinalRecords.Rows.Add(rowFinal);
}
}
}
catch { }
return dtFinalRecords;
}

public static SPListItemCollection GetTheRssRecords(SPList list)
{
SPQuery query = new SPQuery();
SPListItemCollection results = null;
string strQuery = string.Empty;
strQuery = @"
<Where>
<Or>
<Eq>
<FieldRef Name='ContentType' />
<Value Type='Text'>Sharepoint News Page</Value>
</Eq>
<Eq>
<FieldRef Name='ContentType' />
<Value Type='Text'>Sharepoint Article Page</Value>
</Eq>
</Or>
</Where>
";
}
query.Query = strQuery;
results = list.GetItems(query);
return results;
}
</script>
/*******************************************************************************************************************************/

- Now put this aspx page to "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS"

- Now use our custom application page to rendered the RSS feeds.
  i.e. http://YourServer:Port/NewsSite/_layouts/SharepointCustomRSSFeeds.aspx?List=%7BA5EEB0B7%2DA64E%2D40F6%2D9F8D%2D319B3A86A337%7D

- You can get the list id from the address bar by go to the list settings page.
  Here we have not created any list view therefore the Query string parameter "View" will be null.

- Now paste the http://YourServer:Port/NewsSite/_layouts/SharepointCustomRSSFeeds.aspx?List=%7BA5EEB0B7%2DA64E%2D40F6%2D9F8D%2D319B3A86A337%7D url to the address bar,
  Our custom application page will render the rss feed.

 Regards
 Hiren Patel


3 comments:

  1. somehow its not displaying any data from the list. BTW, I had to remove the extra tag top of the query to fix it.

    }
    query.Query = strQuery;

    ReplyDelete
  2. Sharepoint's option to provide an RSS feed will allow users to get up-dates on the tasks that matter most. Customers simply need to register to the RSS feed to see what improvement is being made on the venture. RSS is one of the simplest methods to transmitted information to a large team.

    Custom Application Development

    ReplyDelete
  3. Nice post .Useful content.thanks to share this information about Web Application keep it up.

    ReplyDelete