Friday, April 13, 2012 7:02 AM
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.
Friday, April 13, 2012 10:07 AM
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.
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).
Friday, April 13, 2012 8:49 PM
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
DmitryLightning 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
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