none
consume a wcf rest service from a content editor web part within a page RRS feed

  • Question

  • i have created a  wcfs ervice using the VS 2013/ .NET 4.5 FRAMEWORK and  deployed onto LOCAL IIS SERVER site

    http://localhost:8089/NPDRESTService.svc and i tested it within the service by manually entering the method names and parameters and am able to see the method .

    now my concern is how can i  consume it from a html file thats residing in the site assets  library or a  page / from a content editor web part within a site page or  Page [pages library].

     


    • Edited by SaMolPP Thursday, March 2, 2017 1:29 PM
    Thursday, March 2, 2017 12:35 PM

All replies

  • Following is a sample script with custom WCF service to be used in a SharePoint page:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    
    function login() {
    	jQuery.support.cors = true;
    
    	var login_email = $("#login_email").val();
    	var login_password = $("#login_password").val();
    
    	var url = "http://localhost:8089/NPDRESTService.svc/UserLogin/"+login_email+"/" + login_password;
    	$.ajax(
    		{
    			type: "GET",
    			url: url,
    			contentType: "application/json; charset=utf-8",
    			dataType: "json",
    			success: function (data) {
    				alert("Success");
    				document.getElementById("profileName").innerHTML = data.FirstName;
    			},
    			error: function (msg, url, line) {
    				alert('error trapped in error: function(msg, url, line)');
    			}
    		});
    }
    
    </script>
    <div>
    	<input type="text" id="login_email">
    	<input type="text" id="login_password">
    	<label id="profileName"><lable>
    	<input type="button" value="Login" onclick="login();"/>
    </div>
    • Copy the script in a notepad and modify accordingly.

    • Save as html file and upload to your sharpoint site.
    • Create a webpart page and add a content editor webpart
    • Edit the webpart and put the html file link to the content link box as shown below:

    • Save the page and now you can access your WCF service from the page.

    Avijit Sur

    Thursday, March 2, 2017 1:23 PM
  •  Is it possible to pass int value or any other complex data type as a parameter to the wcf service ? 

    I tried with a sample class called Products.cs file . and i want to pass this products object as param to wcf srevice.

     public class Products

     {

            public int Id { get; set; }
            public string Name { get; set; }
            public decimal Price { get; set; }
            public int stock { get; set; }

            public Product()
            {
            }

            public Product(int id, string name, decimal price, int stock)
            {
                this.Id = id;
                this.Name = name;
                this.Price = price;
                this.stock = stock;
            }
        }

     }

    as per the  uritemlate attribute in webinvoke,  we cant pass int,  other complex typs other than string datatype.
    • Edited by SaMolPP Sunday, March 5, 2017 11:57 AM
    Sunday, March 5, 2017 11:49 AM
  • Here the is sample code:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    
    function addProduct() {
    	jQuery.support.cors = true;
    
        var product_id = $("#product_id").val();
        var product_name = $("#product_name").val();
        var product_price = $("#product_price").val();
        var product_stock =  $("#product_stock").val();
    
        var product = {
            "Id": product_id,
            "Name": product_name,
            "Price":product_price,
            "stock": product_stock
        }
    
    	var url = "http://localhost:8089/NPDRESTService.svc/AddProduct";
    	$.ajax(
            {
                type: "POST",
                url: url,
                data: JSON.stringify(product),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert("Success");
            },
            error: function (msg, url, line) {
                alert('error trapped in error: function(msg, url, line)');
            }
        });
    }
    
    </script>
    <div>
    	<input type="text" id="product_id">
    	<input type="text" id="product_name">
    	<input type="text" id="product_price">
    	<input type="text" id="product_stock">
    	<input type="button" value="Add Product" onclick="addProduct();"/>
    </div>
    Hope this will help.


    Avijit Sur

    Sunday, March 5, 2017 1:10 PM
  • i was trying to connect to sql db for fetching  few records with integrated security = true.

    but that fails.

    i have tried with both wcf service and web api . but in both cases i am unable to get records from sql db.

    Sunday, March 5, 2017 4:44 PM
  • Hi,

    Make sure your WCF service account have proper permission for your database.

    Here are some links for your reference.

    http://stackoverflow.com/questions/22328546/cant-connect-to-sql-server-from-wcf-service-using-account

    https://social.msdn.microsoft.com/Forums/en-US/a47ef2d0-31d3-4f8d-93b3-1526877d8b66/windows-authentication-between-wcf-service-sql-server?forum=wcf

    For issue related to WCF, I would recommend posting in WCF forum, so you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=wcf

    Thanks for your understanding.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 7, 2017 1:43 AM