SharePoint Resource

The latest insights from the SharePoint Experts

Author Archive

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 | 3 Comments »

Change local site language format (locale) in Sharepoint C#

Posted by niknovotronix on September 22, 2011

Had to change the regional settings on a SharePoint site programmatically to get correct date/time format in lists and calendars. Since the whole site is dynamically created with a site definition, this had had to be done with a feature which was activated upon site creation.
  1. Create a feature
  2. Create an event receiver class to the feature
  3. Code:
   

4. Set the local country code of your country instead of “en-GB”

Posted in Uncategorised | Leave a Comment »

The workbook you requested cannot be opened in the browser

Posted by niknovotronix on September 1, 2011

This error is not unusual when configuring excel services and trying to display an excel document inside the browser. OK, so what to do then?

  1. First step would be to check the SharePoint log and the event log for errors
  2. Check that the account running the excel service application has “db_owner” rights to the SharePoint content database. (http://blogs.msdn.com/b/jjameson/archive/2010/05/04/the-workbook-cannot-be-opened-error-with-sharepoint-server-2010-and-tfs-2010.aspx)
  3. If you get this error in the event log “The data culture ‘X’ was requested, and it is not fully supported because its language pack is not installed. Some features will not be available.”, install the language pack for that specific country and run the configuration wizard. (http://www.kowalski.ms/2008/07/04/excel-services-was-unable-to-load-the-workbook-that-you-requested/)
  4. See an “System.InvalidOperationException: Could not retrieve a valid Windows identity.” error in the ULS log? Make sure “Claims to Windows Token Service is started in Windows Services
  5. Also, check if Claims to Windows Token Service is started in central administration (Services on server)
If you have more steps to add to this post, leave a comment.

Posted in Uncategorised | 1 Comment »

Timer job: No changes after deployment

Posted by niknovotronix on August 31, 2011

Ever re-deployed a timer job in SharePoint without any changes being made (even after GAC deployment and “iisreset”)?

Remember to restart the “SharePoint 2010 Timer” Service as well after deployment for changes to be made immediately.

Posted in SharePoint 2010, SharePoint Deployment, SharePoint Development | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 73 other followers