SharePoint Resource

The latest insights from the SharePoint Experts

Archive for October, 2011

Creating a WCF web service and calling JSON objects from JQUERY – SharePoint 2010 C#

Posted by niknovotronix on October 13, 2011

WCF or ASMX?

First off, why create a WCF web service in SharePoint instead of the old ASMX web service? Did some reading on this and would recommend these blog posts on the subject:

http://dotnetarchitecthouston.com/post/WCF-versus-ASMX-services.aspx

http://www.bishoylabib.com/2009/08/comparing-asmx-and-wcf.html

Creating a WCF web service in Visual Studio 2010

1. Start off by creating a new SharePoint project in visual studio

2. Call it WCFService (or name of your choice)
3. Create a new “Mapped folder” by right clicking on the project and select “Add”
4. Point to the ISAPI folder (Usually: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI)
5. Create a folder called WCFService (or name of your choice)
6. Create a folder called “WCFObjects” (or name of your choice)
7. Add a class called “Staff” in the newly created folder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WCFService
{
public class Staff
{
public string name { get; set; }
public string country { get; set; }
}
}

8. Create an Interface class called “IService.cs” with following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
namespace WCFService
{
[ServiceContract(Namespace = "WCFService.Service")]
public interface IService
{
[WebInvoke(UriTemplate = "/returnStaff", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
List<Staff> returnStaff(string staffName, string staffCountry);
}
}

9. Now we need to create the /returnStaff method defined in the interface
10. Create a class called “Service.cs” with following code in it:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace WCFService
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service:IService
{
public List returnStaff(string staffName, string staffCountry)
{
//create staff list
List staffList = new List();
Staff staff = new Staff();
staff.name = staffName;
staff.country = staffCountry;
//add staff to list
staffList.Add(staff);
//return list
return staffList;
}
}
}

11. Now that we have the logic for the web service, the actual Service.svc can be created

12. Add a “Service.svc” file in the ISAPI\WCFService folder with following code in it:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$"%>
<%@ServiceHost Debug="true" Language="C#" CodeBehind="Service.cs" Service="WCFService.Service, WCFService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=KEY TOKEN"
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

13. Put your token id from the assembly instead of “KEY TOKEN”

14. Your solution should now look like this:

15. Deploy your solution

16. Try to go to “http://yourservername/_vti_bin/WCFService/Service.svc to check if your service is working

17. You will get “Endpoint not found” as a return message from your service

Calling web service from client side JQUERY (ASPX) in a Web Part

1. Create a new SharePoint project called “WebParts”

2.  Create a new visual web part called “StaffViewer” (http://weblogs.asp.net/sreejukg/archive/2011/03/26/create-a-visual-web-part-using-visual-studio-2010.aspx)

3. Add following code to the StaffViewerUserControl.ascx to create input boxes and tables to view data:
<table id="tblInput">
<tr>
<td>
Name:
</td>
<td>
<input type="text" id="name" />
</td>
</tr>
<tr>
<td>
Country:
</td>
<td>
<input type="text" id="country" />
</td>
<td>
<button id="btnCallService" type="button" onclick="getStaff()">
Call web service</button>
</td>
</tr>
</table>
<table id="tblStaff">
</table>

4. Add a JQUERY function called “getStaff” which calls the WCF web service

function getStaff() {
//input parameters
var name = $('#name').val();
var country = $('#country').val();
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
//url to web service
url: "/_vti_bin/WCFService/Service.svc/returnStaff",
//input parameters (JSON)
data: JSON.stringify({ staffName: name, staffCountry: country }),
success: function (data) {
var staff = data;
//iterate through data
$.each(staff, function (index, person) {
//view data
$('#tblStaff').append('
Name: ' + person.name + '| Country: ' + person.country + '
');
});
}
});
};

5. Note the  ”JSON.stringify({ staffName: name, staffCountry: country })” which passes through parameters as in JSON format

6. Add a .click event to the button

$(function () {
$("btnCallService").click(function () {
getStaff();
});
});

7. Your solution should now look like this:

8. Deploy the solution

9. Activate the “Web Part” feature

10. Add the Web Part to your page

11. Type a name and country, then click “Call web service”

12. The service is now called, JSON array returned to user control and the result is displayed on the page

Source code:

Download source code

Posted in C#, jQuery, SharePoint 2010, WCF, Web Service | Leave a Comment »

What is InfoPath and its benefits ?

Posted by tomshaw94 on October 6, 2011

Before I start this blog I would just like to say that I am a newcomer to the world of Microsoft Office InfoPath (however Novotronix have extensive experience with the application) and therefore this may not be as technical as you would like. However, through my own limited experiences with InfoPath I am aiming in this blog to enlighten people on its many uses.

 

In the simplest terms InfoPath is an application used for designing, dispensing, filling and submitting forms. There are two programs on the Microsoft Office package for InfoPath; InfoPath designer and InfoPath Filler. InfoPath designer is where all the magic of designing and developing the form happens. Here you can design your own template forms or use the existing templates. InfoPath Filler is therefore then used to fill in client based forms. However, if InfoPath forms services are being used then the form can be completed in the browser.

 

Microsoft InfoPath can be very useful for a business when dealing with its form document management and filling forms out. Using InfoPath on a SharePoint Site you replace all of your paper based forms. So now if employees or clients need to fill out a form they can go onto the SharePoint site, go into the forms folder and choose the form they need to fill out. Also once the form is filled out it again goes into a separate list with all the other forms (each type of form could have its own list) to be kept for reference at a later date. This would help form management as all the forms would be in one place on the SharePoint site, instead of thousands of paper based forms all over the place in different filing cabinets and offices.

 

There are many benefits on offer to a business if they have InfoPath installed and are using it. Firstly using InfoPath can save a business money in paper, and also storing the paper documents. Furthermore when using InfoPath it gives the business the ability to pull data from other systems, which can reduce costs and improves functionality/accuracy. A big benefit of having InfoPath is its ease of use. Fewer mistakes are made from having to re-key in information.

 

My benefits of using InfoPath:

 

1) In my opinion one of the biggest benefits of using InfoPath is its ease of use when designing and constructing forms. In the making of this blog I played around with InfoPath Designer and had a go at designing some of my own forms. I found it easy going thanks to the ribbon interface, which increases the efficiency and speed of making forms. In InfoPath Designer the forms you create are completely unique with options to have a company logo and company colours on the forms.

 

2) As I mentioned before I am no developer and because of this I worried that when using InfoPath Designer I would struggle as I came to some of the more complex aspects of the form. However, I was able to create a sophisticated form without writing code. I was able to use out-of-the-box rules, improves rules management and prebuilt layout sections to quickly create a form. Also I could add validation, formatting, or use actions on the form.

 

3) One aspect of InfoPath that I think is significant is you can create your forms based on SharePoint lists. The data in your SharePoint lists can automatically generate a form with all the SharePoint list fields, reducing time to create a form.

 

4) In addition to this I also found that when publishing it only took a single click. As I finished designing my form one of my worries was that publishing it would be a long and complicated process. However, I found that all it took was one click. This furthermore expressed the point of ease of use to me.

 

5) A further benefit of using InfoPath is the use of InfoPath Filler. This can either be used via the browser or using the software version. I think having InfoPath Filler, uniquely designed to fill out the forms you have just created, is a very useful feature and again increases its ease of use. With InfoPath Filler the way you fill in forms becomes cleaner and much simpler.

Posted in Uncategorised | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 45 other followers