Tuesday, November 24, 2009

Calling WCF service from jquery

Over the weekend I was working on a piece of code wherein I was supposed to call a WCF service from jquery (i.e. exposing WCF as REST based service).
I found it really interesting and thought of sharing the details (unfortunately, I could not find a way to attach the sample solution to blogspot).
In this example, I call up a WCF service from jquery and show the result (Products) on a page with pagination support.  To format the result, I use jtemplates.

Here are the steps:
1. First, created an asp.net ajax project and, like all WCF service, create an interface which will be the service contract. Let call this IProductService.


    [ServiceContract(Namespace = "urn:SampleApps/Services")]
    interface IProductService
    {
        [OperationContract]
        [WebGet]
        ProductResult GetProducts(int StartIndex, int PageSize);
    }


The important things to note out here are the namespace in ServiceContract attribute and the [WebGet] attribute.
The first one is used to group our service contracts into namespaces like we do for a C# file and the second one exposes allows it to receive GET messages (which is required for REST calls)


2. Implement this service contract as we do a normal service contract.


    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ProductService : IProductService
    {
        #region IProductService Members
        public ProductResult GetProducts(int StartIndex, int PageSize)
        {
            return ProductFactory.GetProducts(StartIndex, PageSize);
        }

        #endregion
    }


In the above code ProductFactory.GetProducts returns us the products to be displayed on a page. We can have any logic out here to return the products.

3. Create an ASPX page and add references to jquery.js file and optionally to jquery-jtemplates.js


    <script type="text/javascript" src="/JS/jquery-1.3.2.js"></script>
    <script type="text/javascript" src="/JS/jquery-jtemplates.js"></script>


4. Add a script manager tag and refer the service as below on the aspx page

    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="~/ProductService.svc" />
        </Services>
    </asp:ScriptManager>


5. Now the interesting stuff, the below JavaScript code calls up the service and gets the result

    <script type="text/javascript">
        var PageCount = 0;

        function GetProducts(StartIndex, PageSize) {
            var proxy = new SampleApps.Services.IProductService();
            proxy.GetProducts(StartIndex, PageSize, ProductsReturnedEventHandler, ProductsFailedEventHandler, null);
        }

        function ProductsReturnedEventHandler(value) {
            PageCount = Math.ceil(value.TotalRecords / value.PageSize);
            $('#divSearchResults').setTemplate($('#templateContainer').html());
            $('#divSearchResults').processTemplate(value);

            var paging = "<table align='center'><tr>";
            for (var pgN = 1; pgN <= PageCount; pgN++) {
                paging += "<td><a href='#' onClick='ChangePage(this)' id='pagingHref_" + pgN + "'>" + pgN + "</td>";
            }
            paging += "</tr></table>";
            $('#divPaging').html(paging);                 
        }

        function ChangePage(obj) {
            var pgN = $(obj).text();
            var pageSize = 10;
            var startIndex = (pgN - 1)* pageSize;
            GetProducts(startIndex, pageSize);
        }
        
        function ProductsFailedEventHandler(value) {
        }
    </script>

One good feature is that we get full intelligence support in JavaScript for service call as soon as we register the service in script manger :)

6. On the page we have a template section where the results are binded.

    <div> 
    <input type="button" id="btnClick" name="btnClick" value="Click Here" onclick="GetProducts(0, 10);" />
   <div id="divSearchResults" style="font-family: Verdana; font-size: xx-small;">
    </div>    
    <div id="templateContainer" style="display:none;">
       <table style="font-family: Verdana; font-size: xx-small; width:60%">
            <thead style="background-color:Blue; color:White; font-size:larger; font-weight:bold">
                <tr>
                    <td>ID</td>
                    <td>Name</td>
                    <td>Price</td>
                </tr>
            </thead>
            {#foreach $T.Products as post}
            <tr>      
                <td>
                    {$T.post.ProductID}
                </td>
                <td>
                    {$T.post.ProductName}
                </td>
                <td>
                    {$T.post.ProductPrice}
                </td>
            </tr>
            <tr>
                <td colspan="3"><hr /></td>
            </tr>
            {#/for}
        </table>    
    </div>
    <div id="divPaging" />    
    </div>



7. Images:

a. Solution Structure


b. Final result

Friday, July 31, 2009

SharePoint (MOSS 2007) Requirements

For the last few weeks I have been learning Microsoft Office SharePoint Server (MOSS 2007). We even had a week long external training conducted at our organization.
Since this technology is vast, I thought it would be worth sharing the hardware and softwares/ tools required to start learning SharePoint (and of course this list is exhaustive).

Hardware requirements:
-------------------------
If you peek through the list of software required for MOSS 2007, it would be clear that a high end machine is required even to try our hands at.

At the minimum we require the below hardware.
1. Dual Core Processor (3GHZ or above)
2. 1GB RAM (the more the merrier, My PC has 3GB RAM but it still lacks the punch)
3. At least 18GB HDD space to install all the below softwares

Software Requirements.
--------------------------
1. Microsoft Windows Server 2003 (standard or enterprise) with SP1 or higher
2. IIS 6.0 (comes with the OS)
3. SMTP server, if mailing required from SharePoint.
4. SQL Server 2005 enterprise edition with SP2 or above
5. Office 2007 professional edition with SP2 (if excel services features are required)
6. Visual Studio 2008 Professional edition or above with SP1
7. .NET Framework 3.5 (installs with VS 2008)
8. Microsoft Office SharePoint Server (MOSS) 2007 (either standard or enterprise edition) with SP2
Note: Trial version of MOSS 2007 can be downloaded from Microsoft site for learning purposes.
9. Office Server SDK 1.5
10. Visual Studio Extensions for Windows SharePoint Services (VSeWSS) 1.3
11. SharePoint Designer 2007 with SP2 (Thankfully this is now free :))
12. SharePoint reporting services integration add-in (required only for reporting services)

Wow...so, the list is impressive enough to scare away anyone.
Thankfully, Microsoft had provided a way out for people who would like to learn MOSS without spending a fortune to purchase all the sotwares.
A 30 days trial virtual PC image (VHD) can be downloaded free of cost from the below web site of MS:
http://www.microsoft.com/downloads/details.aspx?FamilyID=67f93dcb-ada8-4db5-a47b-df17e14b2c74


This only requires Virtual PC 2007, which again can be downloaded free of cost and can be installed even on Win XP machines.
Once Virtual PC is install the VHD image can be hosted and we can start learning MOSS 2007 even from within Win XP machine.

Sunday, July 26, 2009

Relishing Windows 7

Few days back, I downloaded and installed the evaluation copy of Windows 7.

I had read a lot of good reviews about Windows 7 on blogs claiming that it is much more stable that its predecessor (Vista) and thought of giving it a try.

The evaluation copy (build 7100) is freely available from Microsoft site. I downloaded the 64 bit version (3.05 GB iso image), which is the ultimate edition and takes around 8GB hard disk space.
My PC has a "AMD Athlon 64 x 2 dual core 5200+ 2.7GHZ" processor with 2GB RAM.

The only one drawback that I found of Windows 7 is that there is no direct upgrade support from Windows XP. So, I had to backup my data and go for a fresh install.

The installation was smooth. I expected it to take an hour, but, it installed in cool 20 min.
The changes that Microsoft did appears right from the installation screen which unlike the dull blue screen of Windows XP has full graphics UI.
Even the boot-up is much faster than Windows XP. I liked the way Windows logo comes up the most. It's like 4 light beams coming together in a fusion.

I never tried Vista but have seen people using it. I found Windows 7 to be much faster and reliable even when this is just an evaluation version.
I would highly suggest to give it a try.

Saturday, June 6, 2009

Good News

What a lovely day!! No..No, I don't mean the weather. It is still scorching hot outside and on a normal day like this I won't prefer to even peek outside. But, today I had reasons to do that.

I appeared for MCTS 70-536 (Application Development Foundation) exam and was startled by my score. Can you believe this, I got 982 out of 1000.
Frankly speaking, I have never scored so well in any exam in my entire lifetime :)
I had previously cleared 70-528. So, it now makes me a MCTS (Microsoft certified Technology Specialist) on ASP.NET 2.0 (C#)....applause!!!