Search code examples
sharepointjspweb-parts

Converting a JSP to a SharePoint webpart


We have a large number of Java based servlets/portlets running in a BEA portal that we want to convert into SharePoint 2007 webparts. Many of the portlets use user preferences but the implementations are split between preferences being handled by the portlet directly and stored in a separate database from the portal. Others are using the BEA WebLogic API for user preferences.

Three questions:

  1. Has anyone gotten a Java Servlet/JSP (compiled against JRE 1.4.2 and running on Tomcat 4.1) to run as a SharePoint 2007 webpart?
  2. How large of an effort was it in general (as in, was it measured in days/weeks/months)?
  3. Would it be easier to rewrite the portlet as native webparts at least as far as user preferences are concerned?

Solution

  • Here is what I've done for a single portlet, a stock quote panel.

    We have a gadget that displays stock quotes. We have an account with Tickertech for them to provide us with quote information. There are user preferences which allow people to add the gadget to a private page and then select stocks of interest to them as individuals. You also can select which columns to display. This is accomplished through JavaScript. The selected stock symbols are sent along with a token which identifies the request as coming from a valid customer.

    The simplest approach was to use a web content control and just paste in the JavaScript. This works but leaves no way for a user to change the stock symbols or other preferences involving Tickertech.

    The next step was to create a custom Webpart. We are using the WSPBuilder add-on to Visual Studio. The consulting firm that is helping us with the project reccomended it and I'm very glad they did, reduces the integration cycle to a tolerable level.

    In the webpart we have a property that contains the script.

    public class MarketSummaryWP : Microsoft.SharePoint.WebPartPages.WebPart
    {     
        string m_scriptBlockPre = "<script language='javascript'> \n"+ // the beginning of the JavaScipt block 
    

    In the CreateChildControls() override, I just added it as a literal.

    this.Controls.Add(new LiteralControl(this.Script));  
    

    Next I changed the script to be private and created another property to hold the stock symbol list. Notice out the Script property does the concatenation inside the getter.

        //Script Property
        [WebBrowsable(false),
        WebDisplayName("Script"),
        WebDescription("The JavaScript to insert in the page.")]
        public string Script
        {
        get { return m_scriptBlockPre + m_stockSymbolsList + m_scriptBlockPost; }
        //set { ; }
        }
    
        //Stock Symbol list Property
        [Personalizable(PersonalizationScope.User), WebBrowsable(true),
        WebDisplayName("Stock Symbols"),
        WebDescription("The stock symbols to retrieve quotes for, seperated by commas.")]
        public string StockSymbols
        {
            get { return m_stockSymbolsList; }
            set { m_stockSymbolsList = value; }
        }
    
    
        string m_stockSymbolsList = "GE,CAT,$DJI,AMR,JNJ,";
    
        string m_scriptBlockPost = " *other JavaScript code* </script> \n"+
    

    This gets me a webpart that can be added to any page because it is in the webpart gallery. To add a copy of the webpart built using the static html webpart, you'd need to get the JavaScipt block from an existing instance probably using 'view source', navigate to the target page, add a new instance of the static HTML webpart, and modify it to include the JavaScipt block; each time. This way the users only have to select it from a list of webparts and they can have customized stock quotes preferences.