In one of my earlier post I described how to develop a custom WCF service. Today I’ll cover how you can invoke the SharePoint WCF Service from jQuery. In my last post I described to develop a SOAP web service but for using WCF service from jQuery I’m going to use REST web service. For the list of service types and factories supported in SharePoint you can visit the link in MSDN. You can download source code from the link given at the end of the post.
Prepare the service to call from jQuery
Consider developing a service as described in my earlier post with the following changes:
- For using json I’ve used REST service factory ‘Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory’ as shown below. You can use SOAP factory but you then need to parse data in different way.
<%@ ServiceHost Language="C#" Debug="true"
Service="AccessSPServiceFromJQuery.MyService, $SharePoint.Project.AssemblyFullName$"
CodeBehind="MyService.svc.cs"
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0,
Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> -
Next you need to specify the return type to json in the service interface as shown below. I’ve specified both request and response type to json in WebInvoke attribute:
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<Product> SearchProduct(string productName);
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
bool Save(Product product);
}
Call Service with jQuery
The next step is to call the service with jQuery. The url of the service to be used in jQuery will be service url and method name. For example if you service url is ‘/_vti_bin/AccessSPServiceFromJQuery/MyService.svc’ and the method name you want to invoke is ‘Search’ then the full url will be ‘/_vti_bin/AccessSPServiceFromJQuery/MyService.svc/Search’. As shown in the code below, you can invoke the service Search by passing the parameter in data field of ajax call of jquery
function getProductFromService(searchText) {
try {
$.ajax({
type: "GET",
url: '/_vti_bin/AccessSPServiceFromJQuery/MyService.svc/SearchProduct',
contentType: "application/json; charset=utf-8",
data: { "productName": searchText },
dataType: 'json',
success: function (msg) {
WCFServiceGetSucceeded(msg);
},
error: WCFServiceGetFailed
});
}
catch (e) {
alert('error invoking service.get()' + e);
}
}
function WCFServiceGetSucceeded(result) {
alert('success');
}
function WCFServiceGetFailed(error) {
alert('Service Failed.');
}
Download and use code
I’ve uploaded the code for this post in my skydrive. You can download the code from the link below. To use the code please ensure you have internet connection as I’ve used jqery from Microsoft CDN. The search functionality get all products matching name. You can try to search just by typing a single character. You can debug and test the code. In save I’ve just shown you can pass value from browser to service using POST method.
Would it possible to show an example posting back to the WCF with data?
ReplyDeleteThe IMyService.Save post the product object back to the WCF service.
ReplyDeletehttp://sharepoint.stackexchange.com/questions/25222/posting-json-to-a-rest-wcf-endpoint-in-sharepoint-using-cksdev is based on your question. Do you think you would be able to reply?
ReplyDeleteFor my POST wcf endpoint the WebMessageBodyStyle needed to be WrappedRequest
ReplyDeleteEG:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
I get an "401 Unauthorized" on response to the jQuery ajax request.
ReplyDeleteI've some code with elevated priviledges, maybe it's causing the issue?
I've debugged the whole service's method and didn't throw any exception, but in the end, the client gets this Unauthorized response (as seen in Firebug).
But if I change the method to not do anything and just return an empty List (that's what the method returns) it works perfect.
Any ideas?
@Emzero, can you check sharepoint logs in 14 hive for more details on unauthorized error?
ReplyDeleteI getting the same unauthorize error.
ReplyDelete@EaglesEye, Please check in the webservice which user you are getting by using SPContext.Current.Web.CurrentUser and if the user has proper permission
ReplyDeleteSohel Rana: Once again, excellent article! I think jQuery and WCF Rest are a wonderful combination.
ReplyDeleteThanks John for the complement. Web is now becoming more like client side scripting with new sharepoint app model, single page web applications etc.
ReplyDeleteuseful article. Do you think using jQuery and WCF Service is better than Client Object Model?
ReplyDeleteClient object model is out of the box but has limitations of what you can do with it. On the other hand when you have your own wcf service you can do all your customization with jQuery and WCF. So the basic approach should be if you can do your work with Client Object Model, you should use it, either go for jQuery/WCF
ReplyDeleteHi Sohel, very nice post.
ReplyDeleteUnfortunately its not working for me.
When I try to access the service through URL, it give me an error saying "endpoint not found".
Any help would be appreciated.
Thanks
You Rock man! This works!
ReplyDelete