SharePoint Calling Web Services

These days the overall development model for SharePoint has been changed so much. The core SharePoint developers who have been working since 2007 have to make major updates to their core knowledge related to SharePoint client side object model & the usage of jQuery enough to keep their expertise up-to-date with the new set of tools and techniques that we have. There are many ways of using jQuery in SharePoint and calling web services. We will try to look at how smartly we can do this. We will use the Visual Studio to complete all of these steps.

For sample and illustration purposes, we should keep everything in one solution instead of having multiple projects and maintain all of these. One solution will be created, one WSP only and we will deploy it in one go. This will make things easier. We will look at the following steps:

  • Create a simple SharePoint project using a blank template so that it has nothing and we can add to it later on with what we need.
  • Add a new item of type WCF service to the solution. This will add all relevant files to the solution, namely it has the interface file starting from ‘I’, it contains the implementation file & the config file.
  • Do some implementation in the web service by providing some methods that return something just to test it out. Also add the reference which is almost mandatory that is Microsoft.SharePoint.Client.ServerRuntime & System.ServiceModel.Web.
  • Next we will need to add the mapped Layouts folder to our project, we will add the .svc file to this folder. This will deploy our .svc file to this folder once the solution is deployed.
  • Once that is done, the next step is to write some jQuery code that will call the web service method and invokes it. This should be done in .js file & should also deploy to the Layouts folder as we are deploying this solution as a farm feature. If we have developed this solution as a web application level then we do not need to deploy it to Layouts and can use any other place too. The sample code should look like this:
    Function CallWSMethod(lURL)
    {
    $.ajax({
    Type: “Get”,
    url: lURL
    contentType: “application/json; charset=utf-8”,
    dataType: “json”,
    processdata: true,
    success: function (lResult)
    {
    WSCallSuccedded(lResult);
    },
    error: function (lResult)
    {
    WSCallFailed(lResult);
    }
    });
    }

     

  • In order to test this piece of code now, we can add a web part and invoke this method to test it out. Simpler way is to add a web part and use some code to invoke the above method by providing the URL we need. The overall solution looks like this:
SharePoint Calling Web Services - Overall Solution

SharePoint Calling Web Services – Overall Solution

By completing this, we have simply added a farm feature, JS file, web service & web part in same solution. Upon deploying the solution all relevant resources will be deployed. As soon as we will visit the page, we can see the desired results as an output.

Data Visualization With Share Point And SQL Server

Usman Akram on SharePoint Calling Web Services

Usman Akram on SharePoint Calling Web Services

About the Author: Usman Akram is an experienced IT Professional with over 10 years experience in Microsoft technologies especially SharePoint. Usman has delivered several large and complex projects including integration of SharePoint with BizTalk and with Dynamics CRM. Other then SharePoint Usman has also managed a BigData analytic project (HDInsight/Hadoop).

Share this on...

Rate this Post:

Share: