Sitecore – Web Service

I don’t know whether I should hold my post for another day or I keep posting everything. But well, I am in mood to post things, so here it is.

Somehow, I develop a web that using javascript to run a serverside method. Yes, sometimes you need that. I figure it out how to use that and I got the answer: web service.

Sitecore has provide its developer with a web service to access some functions. You can find it on /sitecore/shell/Webservice/Service.asmx Well they provide some useful functions I told you and you might need that. But those functions are not always needed (I guess). For me, (I don’t know about others) I use web service to access data that my application provide not the data that CMS provide for my application. For example, sitecore is providing me the list of users, I really don’t need to use web service (at least until now) to access that. My application provide a list of products and I need to access them from anywhere. Oh, and by the way, if you use the web service that Sitecore provide you will be logged in as sitecore/* user.

So, I create a custom web service in my application. Just say that the web service provide me a function that returns a list of string. I have to call it using javascript. How do I do that? Here’s the step.

  1. Add [System.Web.Script.Services.ScriptService] tag before your web service class definition. So it will become like the code below.

    public class ClassName : System.Web.Services.WebService
  2. You need to register your service in your web control. Using the code below, you can register the web service as a script on a control.

    <asp:ScriptManager ID="_scriptManager" runat="server">
        <asp:ServiceReference Path="/path/to/your/web/service.asmx" />
  3. Then you may call it using javascript using the code below.


    Params is all parameters needed by your web service method. OnSuccess is a function that will process the result if there is no error. OnFail is a function that will process the result if there is any error. And I haven’t used userContext. 😀

  4. the function to process the result will be like

    function FunctionName(result) {
        /* body */

Simple right? Just four steps to use it. You can do most of anything. And if you want to use the web service from the server side C# code, it becomes even simpler. You only need to add reference then use it as you like.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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