Sharepoint 2010 Visual Webparts and WCF


Hardy har har har,

There is a really nice feature with Sharepoint 2010 that allows you to create webparts in a very intuitive way with Visual studio 2010. You can create a project called Visual Web Part:

Now this project is pretty much a ASP.NET user control with some extra classes and meta data to work with Sharepoint. What can you put into visual webparts? Well the answer is great, anything you can put in a ASP.NET user control, plus the added advantage of using sharepoint assemblies. Believe me coming from sharepoint 2003 this is the Sh@T! At my day job we are currently working on upgrade from 2003 to 2010.

Now we have the POWWWWWERRRRR! How can we use web services with visual webparts? This is what I did.
Right click on your project and click “Add Service reference”:

After you add it you will now have “Service References” folder and you should see you service reference in this folder. Now you can reference the service in your code and use all its Objects and methods. Wow Fritz, This is how it is suppose to work!?

The unexpected part is when we go to run and debug the project or deploy the project, The service reference will not work. You will get errors! Why are you getting errors. Because the app.config file that it creates in your project which contains the service reference info, is not deployed with the project. If you open app.config you will see something similar to this:

<system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IPosReporting" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://misoffsvrweb1dv/POSReporting/PosReportingService.svc"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IPosReporting"
                contract="POSReporting.IPosReporting" name="WSHttpBinding_IPosReporting">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>

So how do you get this information to be used by sharepoint. What I did is copied and paste this info into the sharepoint sites web.config.

Open the sharepoints web.config file then look for the closing xml tag:

</system.webserver>

right after that paste everything from app.config starting and ending on the system.ServiceModel tag:
<system.serviceModel>everything in this tag</system.serviceModel>

Save the web.config file, make sure you have admin rights to do this. Run you visual web part project and it should deploy and run!!!

Thats it, thats it ……


3 Comments on “Sharepoint 2010 Visual Webparts and WCF”

  1. viara says:

    this is great, thanks for posting!!

  2. spokhrel says:

    I did exactly same mentioned above, but i got some errors as follows. Please help me.

    Server Error in ‘/’ Application.
    Runtime Error
    Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

    Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a “web.config” configuration file located in the root directory of the current web application. This tag should then have its “mode” attribute set to “Off”.

    Notes: The current error page you are seeing can be replaced by a custom error page by modifying the “defaultRedirect” attribute of the application’s configuration tag to point to a custom error page URL.

    • ontehfritz says:

      Hey,
      You need to enable the error messages remotely or try executing the web page locally on the server to get the correct error details.
      Until then you can only guess what is happening. It is possible there is communication issue between the sharepoint server and the WCF service you are trying to connect to.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.