Thursday, November 20, 2008

Stock Ticker Web Part in SharePoint 2007 using web service

Requirement: To display our company stock quote details on a SharePoint HomePage

Solution:

Two ways:

1) Create web part to display the stock details in a Table,

i) Using rss feed url

ii) Using Web Service.

The sample code using Web Service is as follows:


StockQuote.StockQuote stkquote = new StockQuote.StockQuote();
string quote = stkquote.GetQuote("MCHP");//MCHP is the symbol for Microchip stock
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(quote);
XmlNodeList nodelist = xmldoc.GetElementsByTagName("Stock");
XmlNode xmlnode = nodelist[0];
string symbol = "", last = "", date, time, change = "", open, high, low, volume = "", mktcap, prevclose, percentgechange = "", annrange, earns, pe, name;
for (int i = 0; i < xmlnode.ChildNodes.Count; i++)
{
XmlNode node = xmlnode.ChildNodes[i];
if (node.Name == "Symbol")
symbol = node.InnerText;
if (node.Name == "Last")
last = node.InnerText;
if (node.Name == "Volume")
volume = node.InnerText;
if (node.Name == "Change")
change = node.InnerText;
if (node.Name == "PercentageChange")
percentgechange = node.InnerText;
}
tblStockTicker = new Table();
TableRow trStockTicker = new TableRow();
TableCell tcStockQuote = new TableCell();
HyperLink hlStockQuote = new HyperLink();
hlStockQuote.Text = "MCHP Quote";
hlStockQuote.NavigateUrl = "http://quotes.nasdaq.com/asp/SummaryQuote.asp?symbol=MCHP&selected=MCHP";
tcStockQuote.Controls.Add(hlStockQuote);
trStockTicker.Cells.Add(tcStockQuote);
TableCell tcBlank = new TableCell();
tcBlank.Text = " ";
trStockTicker.Cells.Add(tcBlank);
TableCell tcQuoteText = new TableCell();
tcQuoteText.Text = "(NASDAQ Exchange - quotes delayed 15 min)";
trStockTicker.Cells.Add(tcQuoteText);
trStockTicker.Cells.Add(tcBlank);
TableCell tcVolume = new TableCell();
tcVolume.Text = "Volume: " + volume;
trStockTicker.Cells.Add(tcVolume);
trStockTicker.Cells.Add(tcBlank);
TableCell tcLast = new TableCell();
tcLast.Text = last;
trStockTicker.Cells.Add(tcLast);
trStockTicker.Cells.Add(tcBlank);
TableCell tcChange = new TableCell();
tcChange.Text = change;
trStockTicker.Cells.Add(tcChange);
trStockTicker.Cells.Add(tcBlank);
TableCell tcPercentageChange = new TableCell();
tcPercentageChange.Text = percentgechange;
trStockTicker.Cells.Add(tcPercentageChange);
trStockTicker.Cells.Add(tcBlank);
tblStockTicker.Rows.Add(trStockTicker);
this.Controls.Add(tblStockTicker);

(Note: I added a web referrence named "Stock Quote" to refer to the web service, http://www.webservicex.net/stockquote.asmx )


The sample code using RSS Feed is as follows:


XmlTextReader rssReader;
XmlDocument rssDoc;
XmlNode nodeRss = null;
XmlNode nodeChannel = null;
XmlNode nodeItem;
rssReader = new XmlTextReader("http://www.nasdaq.com/aspxcontent/NasdaqRSS.aspx?data=quotes&symbol=MCHP");
rssDoc = new XmlDocument();
// Load the XML content into a XmlDocument
rssDoc.Load(rssReader);
XmlNodeList nodelist = rssDoc.GetElementsByTagName("description");
string description = string.Empty;
for (int i = 0; i < nodelist.Count; i++)
{
description = Convert.ToString(nodelist[i].InnerText).Trim();
}
// Loop for the tag
for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
{
// If it is the rss tag
if (rssDoc.ChildNodes[i].Name == "rss")
{
// tag found
nodeRss = rssDoc.ChildNodes[i];
}
}
// Loop for the tag
for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
{
// If it is the channel tag
if (nodeRss.ChildNodes[i].Name == "channel")
{
// tag found
nodeChannel = nodeRss.ChildNodes[i];
}
}
for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
{
// If it is the item tag, then it has children tags which we will add as items to the ListView
if (nodeChannel.ChildNodes[i].Name == "item")
{
nodeItem = nodeChannel.ChildNodes[i];
//string cdata = nodeItem.Attributes["CDATA"].Value;
string desc = nodeItem["description"].InnerXml;
string decstext = nodeItem["description"].InnerText;
string[] sep = { "\table" };
string[] str = decstext.Split(sep, StringSplitOptions.None);
string strFinal = str[0].Replace("\r\n ", "");
strFinal = strFinal.Replace("\r\n ", "");
strFinal = strFinal.Replace("MCHP", "MCHP quote");
strFinal = strFinal.Replace("% Change", "");
strFinal = strFinal.Replace("Change", "");
strFinal = strFinal.Replace("Last", "(NASDAQ Exchange - quotes delayed by 15 min) ");
strFinal = strFinal.Replace("#DDDDDD", "");
//strFinal = strFinal.Replace("red", "white");
//string strRemove = strFinal.Substring(strFinal.IndexOf("/table"), strFinal.Length);
//strFinal = strFinal.Remove(strFinal.IndexOf("/table"), strFinal.);
Literal lt = new Literal();
lt.Text = strFinal;
//this.Controls.Add(lt);
Table tb = new Table();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Controls.Add(lt);
tr.Cells.Add(tc);
tb.Rows.Add(tr);
tb.BackColor = Color.Silver;
tb.ForeColor = Color.White;
this.Controls.Add(tb);
}
}
}
}

(Note: I used NASDAQ RSS Feed (http://www.nasdaq.com/aspxcontent/NasdaqRSS.aspx?data=quotes&symbol=MCHP) to get the Microchip Stock details.


2) Deploy the web part to your Sharepoint Site.

3) Modify the SharePoint HomePage to include the above stock web part.


i) Open SharePoint Designer and open your site form "Open Site".
ii) Checkout your home page and open it.
iii) From the top menu, Select "Task Panes" and selct "Web Parts", which will open the web parts available for the site in the right side window.
iv) Select the Stockticker Web Part from the web parts list and drag and drop in the home page where eber required.
v) Add the following Javascript to refresh the Stock Ticker every 15 minutes.



vi) Check in the file and check your site home page to see the Stock details.

2 comments:

Nikhil said...

Thats awesome. This is exactly what I wanted.

Anonymous said...

Having read this I believed it was really informative.
I appreciate you spending some time and energy
to put this information together. I once again find myself spending
way too much time both reading and posting comments.
But so what, it was still worth it!

my site :: web site