In the world of web 2.0 we need to make our application (in our case SharePoint) more collaborative, interactive and responsive.
In the world of web 2.0 we need to make our application (in our case
SharePoint) more collaborative, interactive and responsive. That’s why I
created a nice example that uses the Long Polling pattern to get us the
real-time web applications experience like Gmail or Facebook has.
In our example we'll create a simple Like button that updates our
custom list number filed and increases it every time we click our
button. Below we'll create a label div that shows in real-time the
number of likes. (At the end of my post you can download the sample
project)
Here are the steps:
1) Create an empty SharePoint project.
2) Add our SharePoint a wcf service.
We have two choices, to create a mapped folder to ISAPI folder in hive 14 and add svc service
or you can use my preferred way and just
download CKS development extension for VS 2010 that takes care the annoying part of adding the wcf files for us.
After you downloaded the CKS extension you need to add to our SharePoint project wcf service(CKSDev), call it WCFLikeService
As described in
MSDN you have three configuration choices:
We’ll choose REST Service and change the factory in our svc file to MultipleBaseAddressWebServiceHostFactory
In our IWCFLikeService.cs file add the following lines:
2 | [WebInvoke(Method = "GET" , ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/getLikes" )] |
6 | [WebInvoke(Method = "POST" , ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/addLikes" )] |
In our WCFLikeService.svc.cs file add the following lines:
01 | public string GetLikes() |
03 | SPList list = SPContext.Current.Web.Lists.TryGetList( "likeList" ); |
06 | SPListItem item = list.Items[ 0 ]; |
07 | return item[ "likes" ].ToString(); |
10 | public bool AddLikes() |
14 | SPWeb web = SPContext.Current.Web; |
15 | web.AllowUnsafeUpdates = true; |
16 | SPList list = web.Lists.TryGetList( "likeList" ); |
19 | SPListItem item = list.Items[ 0 ]; |
20 | int likeNum = int.Parse(item[ "likes" ].ToString()); |
22 | item[ "likes" ] = likeNum; |
24 | web.AllowUnsafeUpdates = false; |
3) Add script folder to a mapped Layouts folder and add jquey js and create a new js file called likeHelper
4) Create a LikeWebPart
Register the two js files.
01 | protected override void OnInit(EventArgs e) |
03 | ClientScriptManager csm = Page.ClientScript; |
04 | if (!csm.IsClientScriptIncludeRegistered( "jQuery-min" )) |
07 | csm.RegisterClientScriptInclude( this .GetType(), |
08 | "jQuery-min" , "/_layouts/SharePointProject/Scripts/jquery-1.7.2.min.js" ); |
11 | if (!csm.IsClientScriptIncludeRegistered( "likejs" )) |
14 | csm.RegisterClientScriptInclude( this .GetType(), "likejs" , |
15 | "/_layouts/SharePointProject/Scripts/likeHelper.js" ); |
In on render method add div with ‘likeBtn’ id and another div with ‘like’ id
01 | protected override void Render(HtmlTextWriter writer) |
04 | writer.Write("<div id=\"likeBtn\" style=\" width : 50px ; hight: 20px ; |
05 | background-color : blue ; color : white ; margin-top : 5px ; font-size : 12px ; font-family : |
07 | writer.Write( "<br/>" ); |
08 | writer.Write( "<div id='like' style='font-size:40px'></div>" ); |
5) In our likeHelper.js we will add 3 main functions.
- document ready function
02 | $(document).ready( function () { |
03 | likeLabel = $( "#like" ); |
05 | var likeBtn = $( "#likeBtn" ); |
06 | $(likeBtn).click( function () { |
- updateLike function that will update our number field in our list using wcf service and jquery POST request
4 | SharePointProject/WCFLikeService.svc/addLikes"); |
- waitForMsg function that will use the Long Polling technique
01 | function waitForMsg() { |
05 | SharePointProject/WCFLikeService.svc/getLikes", success: function (data) { |
07 | $(likeLabel).html(data); |
09 | }, dataType: "json" , complete: poll, timeout: 30000 |
*Don’t forget to change the service host url.
*Create a custom list in your web called “likeList” and add a number field called “likes”.
Congratulations, you finished
.
Every time you click on the like button you will see the number of likes increased.
No comments:
Post a Comment