Thursday, February 01, 2007

StockQuotes - A replacement stock quote web part for Sharepoint 2003

Update for Sharepoint 2007 users: Thomas Burke Holland has released a version of this web part for Sharepoint 2007. Get it here.

As most Sharepoint 2003 administrators know, Microsoft will discontinue support for their MSNBC Web Parts on February 6, 2007 (KB 929197). Since I was unable to find a suitable replacement for the MSNBC Stock Quote web part, I decided to program my own using the stock data provided by Yahoo!.

It is important to note that this web part is provided by me and I have nothing to do with Yahoo!. All of the references to Yahoo! are meant to give them all the credit for the data.

Here is a screenshot of the default web part:



In this next screenshot, you can see the settings window. Here is where your users can identify the stocks they wish to track. These stocks are a Sharepoint Personal setting, so they can be different for each user.



As you can see from the settings, there is an option to display the company name in addition to the stock symbol. Here's a screenshot of what that looks like:



Finally, if you select the display charts option in the settings, you will see charts of both the intra-day and weekly results for the stocks you've chosen. Screenshot:




To install this web part:

1. Locate the directory where sharepoint portal server is installed (i.e. C:\Inetpub\wwwroot). You should see a web.config file in this location.

2. Edit the web.config file:
2.1. In the safecontrols section, add the following line (note that this is case sensitive!):


<SafeControl Assembly="StockQuotes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=24291b4f7bc336a4" Namespace="StockQuotes" TypeName="*" Safe="True" />

2.2 Change the trust level to "Full." You may skip this step if you are installing the web part to the GAC (not recommended for security reasons).

<trust level="Full" originurl="">

2.3 Save the web.config file and run iisreset at a command prompt.

2.4 In your sharepoint root directory (typically C:\Inetpub\wwwroot), create a directory called "bin" if it doesn't already exist. Copy the StockQuotes.dll file into this new directory (C:\Inetpub\wwwroot\bin). Be sure that the permissions on the bin directory and the file itself are the same as the wwwroot directory, otherwise you may see an "Access denied" error message.

Alternatively, you may install the web part to the Global Assembly Cache by dragging the StockQuotes.dll file into C:\Windows\Assembly. You must restart the server after doing this before the web part will work properly. If you do this step, you can leave the trust level at "WSS_Minimal." Please note that the best practice is to install web parts in the bin directory instead of into the GAC. Besides being better for security, it allows you to replace web parts without having to restart the whole server.

3. Import the StockQuotes.dwp file to load the web part on the page.


***Please note that the stock symbols are currently stored as a sharepoint Personal property. Some web part zones do not permit personalization (for example, the web part zones on the home page of the portal). Therefore if you load this web part in one of these restricted zones, your users will not be able to customize the settings (although you can specify the settings under Shared View). You will notice that the "Change Symbols" link is hidden when the web part is loaded in a restricted zone.

If you have a single-portal web server, you can consider editing the template file to change a web part zone so that it can be personalized by the users. To do this, edit:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\TEMPLATE\1033\SPS\default.aspx
Look for the web part zones at the bottom and change: AllowPersonalization="true"


The services provided by Yahoo may be changed at any time. I am not associated with Yahoo in any way and I have no idea as to the legality of your use of this code with respect to downloading data from the Yahoo.com website. Please refer to Yahoo for any applicable copyright or usage information.

My web part (StockQuotes.dll) and source code are licensed under the Creative Commons Attribution 2.5 License.
To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco,
California, 94105, USA.


How To Get It

Download (zip file containing .DLL file and source code) - Last Updated 2/2/2008


Notes
  • I have tested on a fresh install of Sharepoint Portal Server 2003 on both portal and wss pages. The web part has not been tested with Sharepoint 2007.
  • Your web server must be able to contact the Yahoo Finance web servers. Although the charts are downloaded directly by the users, the content of the stock quotes is downloaded by the server before the web part is rendered. The file downloaded by the server is extremely small (it is a comma delimited file showing on the data on the screen). Currently this data is not cached due to the variety of different searches your users may perform. Therefore, this small data file is downloaded from Yahoo each time the web part is loaded by each user. If you have a high-traffic web site, you will want to consider how this will impact the performance of your server.

Troubleshooting

Assembly stockquotes.dll security permission grant set is incompatible between appdomains.
Check the trust level setting in your web.config (typically c:\inetpub\wwwroot\web.config) and be sure that it is set to Full.

Error Access is denied: 'StockQuotes'.
Check the permissions on the StockQuotes.DLL file in the bin directory (typically c:\inetpub\wwwroot\bin). To adjust these permissions, set both the bin directory and the dll file to inherit the permissions of the parent folder (on the file properties Security tab, click Advanced and click the checkbox to Allow inheritable permissions from the parent to propagate to this object...).

Web Part Error: This page has encountered a critical error. Contact your system administrator if this problem persists. (on existing Sharepoint web parts)
This can occur if the safecontrols line is not exactly correct. In my instance miscapitalizing Safe="True" as safe="True" caused the built-in web parts to malfunction (note that the "S" must be capitalized).

A Web Part or Web Form Control on this Web Part Page cannot be displayed or imported because it is not registered on this site as safe. (when adding the web part to a page or viewing a page with the web part already added)
This can be caused by 3 issues: 1. The SafeControls entry in the web.config file is missing or incorrect, 2. Sharepoint cannot find or access the StockQuotes.DLL (is it installed in the bin directory? if installing in the GAC, did you reboot?), or 3. The DWP file you are importing contains incorrect information about the Assembly and Type info for the web part.

Finally
If you use my web part, please send add a comment here.

36 comments:

Parag said...

Good stuff!

Are you planning on writing/testing one for 2007?

Thanks!

Parag said...

Never mind. I got it to work with 2007 with some customizations.

debh106 said...

I was very excited to find both the stock quote and weather web parts on your blog, as my employer is looking for a solution to the MSNBC web parts. I do have a quick question for you and actually this applied to both items: We have a single instance of SPS2003 running on two servers; can individual users at my company personalize the stocks and weather? I was a little unsure what you meant by "single-portal web server". Thanks for your help!

Scott said...

@debh106

The reference to a single-portal web server was only a caution about editing the master sharepoint templates. For example, if you hosted a dozen sharepoint sites on a server for a dozen different companies, you wouldn't want to edit the master templates because the change would affect every portal hosted on that server. It doesn't sound like it would be a problem for you.

BTW, the alternative to editing the master template would be to modify the web part zone using Frontpage to enable Personalization. Just be aware of the ghosting issues when editing any Sharepoint page with Frontpage.

John said...

Scott,

Thanks for the great web part! The webpart works great on my WSS sites but will not work on any SPS sites for some reason. I know you said that you tested it on portal sites but do you have any ideas on how I can troubleshoot?

Thanks,
John

Scott said...

FYI, I've updated the directions to fix a problem with the SafeControls line. If you have previously had trouble getting this web part to work properly, this may have been your problem.

Jyotirmayee said...

Hi, i am able to import the webpart in WSS v3.0 but i see this error.

System.NullReferenceException: Object reference not set to an instance of an object. at StockQuotes.StockQuotes.RenderWebPart(HtmlTextWriter output)

Nathan said...

I get the same error...
System.NullReferenceException: Object reference not set to an instance of an object. at StockQuotes.StockQuotes.RenderWebPart(HtmlTextWriter output)

Any thoughts as to where this comes from?

Craig said...

Great! Exactly what I need however, I get the error "Unable to contact the yahoo finance web site. Timeout waiting for the Yahoo Finance web site to resond." Is there someting I am missing right under my nose? The yahoo finance site link works quickly and perfectly in the browser, but not within the web part.

Scott said...

@Craig

That message usually means that the web server is unable to access the Yahoo web site. The Sharepoint web server downloads the data file directly from the Yahoo website (the client only downloads the chart images from the yahoo web site). Check with your firewall people to make sure that your server can access the Internet. If you use a proxy configuration, you'll need to modify the web.config file to accommodate that (I'm not sure of the steps).

Jeff said...

I've also had the error as some above: "System.NullReferenceException: Object reference not set to an instance of an object. at StockQuotes.StockQuotes.RenderWebPart"

Any idea on why I would be getting this error?

John said...

I too am having the same issue with 2007. "System.NullReferenceException: Object reference not set to an instance of an object. at StockQuotes.StockQuotes.RenderWebPart"

parag stated that he was able to get it to work with some customizations.

srividya said...

hi
even i am getting this error, scott, can you please tell me how to resolve this error.
System.NullReferenceException: Object reference not set to an instance of an object. at StockQuotes.StockQuotes.RenderWebPart(HtmlTextWriter output)

Scott said...

As the title implies, this web part was written and tested for Sharepoint 2003, so I'm not surprised that it does not work in Sharepoint 2007 (aka MOSS 2007 aka WSS 3.0).

At some point I will test and update the web part so that it is compatible with MOSS 2007.

Bei said...

To use this for WSS 3.0/MOSS 2007, change the following lines in the StockQuote.vb and recomplie:

Around line 118 Comment out:
'Dim webPartZone As WebPartZone = Me.Page.FindControl(Me.ZoneID)

Immediately after that line, add:
Dim baseWebPart As WebPart = CType(Me, WebPart)
Dim webPartZone As WebPartZone = CType(baseWebPart.Zone, WebPartZone)

Deepu said...

Great!!!!!!!!!!

Can you please proved me the same things for Weather Webpart for MOSS. Please its very urgent.

My Email Id is:- kdk1983@gmail.com

Deepu said...

hi,

I had your's old weather webpart but i need some other one in which i can show weather for whole week days.

Thanks in advance.

Isha kapoor said...

Hi,

I did the changes needed for for it to run in sharepoint server 2007.
When i deploy it i see the lyout yahoo fiance thing but no values ......
can u help

stan said...

Scott,

We have been using SPS2003 and your awesome web part for a while now but some time ago, no values seem to show up. I tried reloading, updating to the one you fixed recently. But nothing seems to work. Does it still work with SPS2003?

Thanks!

Scott said...

I've created a new blog entry explaining the issue and recompiled the DLL with the fix.

Empty Stock Quotes Problem Resolved

Deepu said...

Hi scott,

I done all the upadation for use this web part in MOSS as you explianed. I am getting this error now.
"Unable to contact the yahoo finance web site. Timeout waiting for the Yahoo Finance web site to resond."

i read blog in which you told that check the proxy setting but all the setting are here ok, and yahoo site is running fine. So please tell me what exactly i have to to do get the stock data.
please.

Thanks in advace.
Kuldeep Kadyan
kdk1983@gmail.com

Scott said...

That error message occurs because the web part was not able to access the Yahoo web site. This can happen for several different reasons (firewall, DNS problems, etc). To test it, from your Sharepoint Server, use Internet Explorer to browse to this URL:

http://finance.yahoo.com/d/quotes.csv?s=CSCO+JNJ+MSFT&f=snl1c1v

If that works, then you'll need to debug the code in Visual Studio to find the answer. Set a breakpoint on line 290 and step through the code to see where it bombs out.

I haven't tested the code in MOSS 2007 yet, so I can only confirm that it works in Sharepoint Portal Server 2003 and Windows Sharepoint Services 2003.

Deepu said...

Thanks Scott,

Its working fine now that wass only firewall problem...

thanks again.

just give me one suggestion if i want to access another site for stocks not spedifically yahoo what i have to change????

Kuldeep Kadyan

Scott said...

I'm afraid that this web part only works with Yahoo. The truth is that my web part does very little as it depends on Yahoo for all the data and charts. If you wanted to use another web site's data, you would have to rewrite much of the web part.

Deepu said...

Ok,
Thanks Scott.

DotNetGuts said...

I Got Same Error any Solution?????

FYI: Testing on MOSS 2007 and deployed in GAC, also added entry in web.config file

System.NullReferenceException: Object reference not set to an instance of an object. at StockQuotes.StockQuotes.RenderWebPart(HtmlTextWriter output)

Linton said...

can anybody run us through how you do the following

"change the following lines in the StockQuote.vb and recomplie:"

Around line 118 Comment out:
'Dim webPartZone As WebPartZone = Me.Page.FindControl(Me.ZoneID)

Immediately after that line, add:
Dim baseWebPart As WebPart = CType(Me, WebPart)
Dim webPartZone As WebPartZone = CType(baseWebPart.Zone, WebPartZone)

Deepu said...

i will send you the Snapshot of this thing just send me your EmailID.

My ID is:- kdk1983@gmail.com

Mahendran said...

Will this support wss3.0

Mahendran said...

Anybody have C# code in StockQuotes webpart

Zane Hasouris said...

Thanks for the great webpart.

As far as Moss 2007? Is anyone else having trouble getting the CustomPropertyToolPart working?

In other words can you edit the stock ticker symbols in 2007?

Scott said...

At least in Sharepoint 2003, you have to be sure that the web part is located in a zone that allows personalization. For example, the code in the template for the web part zone:

<WebPartPages:WebPartZone runat="server" AllowPersonalization="true" FrameType="TitleBarOnly" ID="TopLeftZone" Title="Top Left Zone (Restricted)" Orientation="Vertical"/>

Thomas Burke Holland said...

Scott

I used your code base and ported it to 2007. It works GREAT. I did have to make some changes as far as some of the properties went as well as when calls were made.

I would like your permission to post a blog on it. I could not find a way to contact you directly via your blog.

Please contact me at burkeholland@gmail.com

TC said...

I am getting the System.NullReferenceException: Object reference not set to an instance of an object. at StockQuotes.StockQuotes.RenderWebPart(HtmlTextWriter output)
error as well in WSS 3.0. Can anyone help and provide a fix. I don't have the capabilities to compile a dll here. Thanks

Scott said...

TC: Try downloading the version that Thomas Holland compiled for MOSS 2007:

Stock Quotes Web Part for Sharepoint 2007 (WSS 3.0)

Shyamala Devi said...

Hi,I have installed dll and modified the web.config file.After loading the web part i am getting this error...
System.NullReferenceException: Object reference not set to an instance of an object. at StockQuotes.StockQuotes.RenderWebPart(HtmlTextWriter output)
Can anyone help?