Sandeep's Blog: This blog is no longer updated, please visit http://sandeepnakarmi.com.np for latest posts

December 23, 2009

Programatically Browser-Enable InfoPath form template in SharePoint

Filed under: InfoPath, Sharepoint 2007 — Tags: , , — sandeep nakarmi @ 3:33 PM
A BrowserEnabled InfoPath form template allows you to access and modify the item directly from the web browser like  Internet Explorer. Inorder to publish an InfoPath Form template that is browser enabled, the InfoPath template must be configured to be browser compatible along with the sharepoint server.  

In InfoPath, make sure you set the following parameters to make the template browser compatible:-        

  • When creating a new template, in Design a From Template wizard, check the “Enable browser-compatible features only”.
  • In Toosl->Form Options->Compatibility, make sure the checkbox that states “Design a form template that can be opened in a browser or InfoPath” is checked. 

With that, if you try to publish the template to SharePoint using InfoPath publish feature , and if you have not configured sharepoint to browser-enable the form templates, you will get a warning message in the publishing wizard:-

This form template is browser -compatible, but it cannot be browser-enabled in the selected site.       

Reason:
          This may be caused by one of the followings:
         

 

  • The Server is not running InfoPath Forms Services.
  • The necessary features are not available on the site collection.
  • The policy setting on the server does not allow to browser-enable form templates.    

Solution:     
You need to make sure that Sharepoint has been configured to browser-enable the form templates. Make sure that :            

  • InfoPath Forms Services or Forms Server 2007 has been installed on the server.
    In SharePoint Central Admin, verify that InfoPath Forms Services group is present under Application Management.
  • Form Services feature on the site collection is activated.
  • The policy in Central Admin allows you to browser enable the form templates.
    In Configure InfoPath Forms Services setting under Application Management,  allow users to browser enable form templates.
     

With these settings configured, publish the form template again which should work fine.       

From a sharepoint developer’s point of view, if you are interested in knowing how does InfoPath manage to publish these templates and browser-enabled it in sharepoint,  here’s a way to see what’s happening behind the scene.  

I use Fiddler, a web debugging tool, to monitor this. Just set the fiddler to capture data and start the process to publish InfoPath Form template to sharepoint, and you can see the web methods that InfoPath invokes during this process. Here are a couple snapshots with key operations during publishing of a browser compatible form template .             

Is Forms Service Enabled

Invoke WebService method to browser enable form template

Operations exposed by FormsServices.asmx WebService

 The implementation is in the assembly “Microsoft.Office.InfoPath.Server.dll” located in “C:\Program Files\Microsoft Office Servers\12.0\Bin” folder. You can use a reflector to look at the implementation of the operations.          

Implementation of web service operations

 

  So, along with FormsServices web service, you can programatically browser-enable the form template in sharepoint using this assembly in your the project, provided it runs in the sharepoint machine and meets the requirements stated above.       

October 7, 2009

Embedding SqlServer Compact in application. Performance benchmarks and best practices

Filed under: SqlServer — Tags: , — sandeep nakarmi @ 5:20 PM

Thinking of implementing SQL Server Compact in your application. !!!

Have a look at the performance benchmarks and best practices tips presented in PDC 2008 in the video below. It will help a lot.

http://channel9.msdn.com/pdc2008/PC40/   

:-) Happy coding.

September 23, 2009

Create a simple WCF Service and Client application.

Filed under: WCF — Tags: — sandeep nakarmi @ 8:04 PM
In this post, I will demonstrate how to create a simple WCF client and service console application. This post is intended for beginners who can follow the following steps to get a basic WCF Service up and running.

Minimum requirement: .Net framework 3.0.

Download SourceCode

For this, we will create three projects.

  • A service class Library: This library will contain the Service Contract, Operation Contracts and Data Contracts that will be shared by both WCF service and client applications.
  • WCF service application: A console application that will host the service.
  • WCF client application:  A console application that will call a remote wcf service method and display the response from the service.

Lets start by creating the service class library.

1) Create a class library application and name it WCFServiceLibrary.

2) Delete the default Class1.cs file and add new interface file named “IService.cs”.

3) Add reference to the Syste.ServiceModel in the project and then add the corresponding using directives.  Then make the following change in the IService interface.       

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace WCFServiceLibrary
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        int GetProduct(int x, int y);
    }
}

Here we add a definition for method GetProduct which will return the product of two numbers passed in as parameter.

4) Add another cs file named “Service.cs”. Implement the “IService.cs” interface and its method. 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WCFServiceLibrary
{
    class Service : IService
    {
        #region IService Members
        public int GetProduct(int x, int y)
        {
            return x * y;
        }
        #endregion
    }
}

5) Now since we have the service library with a method exposed, lets create a service application now.

6) Create a console application named “WCFService” and add reference to the WCFServiceLibrary project. Also add reference to System.ServiceModel. Add the following using directive.

using System.ServiceModel
using WCFServiceLibrary;

7) Lets now create a configuration file for the WCF Service. This configuration file will define the Endpoint binding, endpoint address and behaviors for the service. Add a new app.config file to the WCFService project.

8) Open the WCF Service Configuration Editor, Tools -> WCF Service Configuration Editor. Then select the app.config file created in above step. You can now follow the wizard to create the binding for the service.

8.a) Since we are creating a config file for service, select the “Create a New Service” link.

8.b) In Service Type step,  browse to the WCFServiceLibrary.dll and select the type “WCFServiceLibrary.Service”. 

         8_b

        8_b_2

8.c) Click Next, and you will see the Contract for the service. 

        8_c

8.d) Click next; here you need to select the mode you want to use for binding, Http, Tcp, Named Pipes etc. Select Tcp and click next. Now set the endpoint address like below: 

        8_d

       8_d_2

 Click Next and finish the wizard.

8.e) Then go to “Endpoints” node and give the name for configuration as TcpBinding. Then save the project.

       8_e

8. f) Select root Services node and then create a binding configuration by selecting “(Default )Click to create”. Supply the name of the binding as NetTcpBinding. With rest set to default, save the config file.

9) Now add the following lines of code to the program.cs file of WCFService project.                                         


namespace WCFService
 {
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
               ServiceHost host = new ServiceHost(typeof(WCFServiceLibrary.Service));
                host.Open();
                Console.WriteLine(string.Format("The service is running....."));
            }
            catch (Exception exc)
            {
                Console.WriteLine(string.Format("Error: {0}",exc.Message));
            }
            Console.ReadLine();         
        }
    }
 }

                                                                                                                                                                                                          When you run the service you should see a console open up with message “The service is running…..”  

10) Now since the service is running, lets create a client to access the remote service. Create a console application named “WCFClient” and add the reference to the System.ServiceModel, WCFServiceLibrary project. Then add the following using directives in the program.cs file.


using System.ServiceModel;
using System.ServiceModel.Channels;
using WCFServiceLibrary;

11) Now let’s create the app.config file for the client. Again we are going to use the “WCF Service Configuration Editor”. The catch here is that, now we can just specify the app.config file of the service application and it will automatically generate the settings for the client according to the service. So follow the steps below to get this done.

  • Add a new app.config file to the WCFClient application.
  • Open the WCF Service Configuraiton Editor and select to open this app.config file.
  • Select the Client node in the left tree and on the right side you will see a link label Create a New Client. Click it.
  • Select the “From service config” option and browse to the app.config file of the WCFService application which we created at the end of step 8.
  • Proceed by selecting next, and provide “ClientTcpBinding” as name when you are prompted for the client configuration name. Proceed to finish and then save the config file.

         Remove the <identity> section if exists. The configuration file should look like this.


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
      <bindings>
          <netTcpBinding>
              <binding name="NetTcpBinding" />
          </netTcpBinding>
      </bindings>
      <client>
    <endpoint address="net.tcp://localhost:8080/Service" binding="netTcpBinding"
   bindingConfiguration="NetTcpBinding" contract="WCFServiceLibrary.IService"
         name="ClientTcpBinding">             
    </endpoint>
     </client>
  </system.serviceModel>
</configuration>

12) Now add the following codes of line to the program.cs file of WCFClient project.

namespace WCFClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
ChannelFactory<WCFServiceLibrary.IService> factory = new ChannelFactory<IService>("ClientTcpBinding");
                factory.Open();
                IService remoteService = factory.CreateChannel();
                int x=4;
                int y=4;
                int retVal = remoteService.GetProduct(x, y);

                Console.WriteLine(string.Format("The product of {0} and {1} is {2} ",x,y,retVal));
            }
            catch (Exception exc)
            {
                Console.WriteLine(string.Format("Error:{0}",exc.Message));
            }
            Console.ReadLine();
        }
    }
}

13) Now run the WCFService application first. Right Click WCFService->Debug->Start New Instance.

        13

14) Run the WCFClient project with WCFClient->Debug->Start New Instance. You should see the following output.

        14

So here we learned how to create a WCF Service, host it in console application, program the client to access this remote service exposed method. We also learned how to use the WCF Service Configuration Editor tool which saved us from manually editing the configuration file and saved our time. 

Happy Coding :-)

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.