Consume WCF service in Visual Web Part
-
Friday, April 13, 2012 7:02 AM
Hello,
We developed custom WCF services which access SharePoint data using BDC object model.
These WCF services are deployed on SharePoint server in _layout folder.Now we want to develop Visual Web Parts and consume these WCF services. WebParts to be deployed on same site where WCF services are deployed.
1. I created new visual web part and added Service Reference. It added app.config file. But when I deployed it i get below error [ The remote server returned an error: (401) Unauthorized. ]
2. Then i added WCF as Web Reference [ Advanced option in Service Reference ] Here it not added app.config file instead WCF service is included as library. It throw same error [ The request failed with HTTP status 401: Unauthorized. ]
Is there any way to consume WCF service in Visual Web Part and SharePoint pages?
Thank you in advance.
Shailendra Shelar
All Replies
-
Friday, April 13, 2012 10:07 AM
Hi,
Are you able to use this WCF service in WCF Test client?
Dmitry
Lightning
Tools Check out our SharePoint tools and web parts |
Lightning Tools Blog -
Friday, April 13, 2012 11:24 AM
Yes, I able to.
We consumed these services in another Sliverlight Applicaiton also and we deployed the Silverlight XAP to SharePoint site. We able to run it using Silverlight Webpart.
Shailendra Shelar
-
Friday, April 13, 2012 12:49 PMI am wondering about where your app settings ended up. They need to go to the web.config file of the SharePoint web app hosting the web part. It is not a problem to call WCF services within web parts, I have done this here http://sharepointdragons.com/2011/10/07/parallel-programming-in-sharepoint-2010-the-back-to-the-future-pattern/ (granted, a normal web part, not a visual one, but things'll be the same). You're allowed to do WCF calls in sandbox solutions as well (so that shouldn't be the problem either).
Kind regards,
Margriet BruggemanLois & Clark IT Services
web site: http://www.loisandclark.eu
blog: http://www.sharepointdragons.com
-
Friday, April 13, 2012 8:49 PM
Hi Shailendra,
Try also take a look at these forum discussions:
- Adding a service reference to a WCF service hosted in SharePoint 2010 gets a (401 ) Unauthorized exception.
- Custom WCF sevices error
Dmitry
Lightning
Tools Check out our SharePoint tools and web parts |
Lightning Tools Blog -
Saturday, April 14, 2012 2:38 AMHope you are changing the Web Config changes to The web application web config.Also, as you ave deployed the WCF to the _Layout, make sure, you provide permission to the "USERS" at the folder level. Hope this will help...
Ashish Ranjan (Please click "Marked As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you)
-
Saturday, April 14, 2012 1:14 PM
Hi Shailendra,
I hope you did not use the "localhost" url in the service reference or web reference.
You can use NetWorkCredential class to authenticate the incoming request for your WCF service.
Why don't you use the WCF Factory method which allows the dynamic endpoint.You find this link will be useful for creating the WCF to be hosted in SP environment.[http://intelliview.blogspot.in/2012/04/wcf-hosting-in-sharepoint-environment.html].You don't want to use the "app.config" or placing the <servicemodel> tag inside your site's config file.sharePoint will manage this in run time.
You can also use the selfhosting option in visual webpart using endpointAddress class
Murugesa Pandian.,MCTS|App.Devleopment|Configure
- Edited by Murugesa Pandian Saturday, April 14, 2012 1:18 PM
- Marked As Answer by Rock Wang– MSFT Friday, April 20, 2012 7:59 AM

