一起谈.NET技术,一个Silverlight的网页访问计数器

  大家知道,SharePoint 2010支持客户端的对象模型访问,主要有三种方式:

  1..net 的客户端对象模型,2.Javascript 的客户端对象模型 3.Silverlight的客户端对象模型,这里就简单实现一个Silverlight的网页计数器,来达到熟悉客户端对象模型的目的。

  其实这个简单的访问计数器主要就是统计了页面刷新了多少次,逻辑很简单,当页面被load的时候,就将次数+1,将次数和页面的地址作为一个Item存放在一个SharePoint list中。下面介绍具体的步骤:

  1、准备工作:在SharePoint 2010的某一个site下创建用来存放访问次数和页面地址的list,我们可以取名为Hit Count list。

  2、VS2010中创建Silverlight Application Project,然后添加客户端对象模型的dll引用,在SharePoint2010中,Silverlight的支持客户端对象模型的dll文件一般存放在c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin下,所以我们在项目中先Add Reference,在上面的路径下添加Microsoft.SharePoint.Client.Silverlight.dll 和 Microsoft.SharePoint.Client.Silverlight.Runtime.dll两个dll。

  3、在项目中添加一个Class,ClientOMProxy.cs作为silverlight访问SharePoint2010数据的代理类,因为Silverlight访问采用异步的方式,所以几个基本的操作方法如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Net;
using Microsoft.SharePoint.Client;


namespace ADSK.AEC.SP2010.ClientOM
{
public class ClientOMProxy:IDisposable
{
private ClientContext clientContext = null;
public ListItemCollection listItems = null;
public ClientOMProxy(string siteURL)
{
this.SiteURL = siteURL;
clientContext
= new ClientContext(this.SiteURL);
}

public void GetListItemsAsync(string listName, string viewXML, out ListItemCollection listItems, ClientRequestSucceededEventHandler successEventHandler, ClientRequestFailedEventHandler failEventHandler)
{
clientContext.Load(clientContext.Web);
List targetList
= clientContext.Web.Lists.GetByTitle(listName);
clientContext.Load(targetList);
CamlQuery camlQuery
= new CamlQuery();
camlQuery.ViewXml
= viewXML;
listItems
= targetList.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQueryAsync(successEventHandler, failEventHandler);
}

public void CreateListItemAsync(string listName, Dictionary<string, object> fieldValueDic, ClientRequestSucceededEventHandler onSuccess, ClientRequestFailedEventHandler onFail)
{
clientContext.Load(clientContext.Web);
List targetList
= clientContext.Web.Lists.GetByTitle(listName);
clientContext.Load(targetList);
ListItemCreationInformation itemCreateInfo
= new ListItemCreationInformation();
ListItem oListItem
= targetList.AddItem(itemCreateInfo);
foreach (KeyValuePair<string, object> pair in fieldValueDic)
{
oListItem[pair.Key]
= pair.Value;
}
oListItem.Update();
clientContext.Load(oListItem);
clientContext.ExecuteQueryAsync(onSuccess, onFail);
}

public void UpdateListItemAsync(string listName, ListItem item, Dictionary<string, object> fieldValueDic, ClientRequestSucceededEventHandler onSuccess, ClientRequestFailedEventHandler onFail)
{
clientContext.Load(clientContext.Web);
List targetList
= clientContext.Web.Lists.GetByTitle(listName);
clientContext.Load(targetList);
ListItem oListItem
= item;
foreach (KeyValuePair<string, object> pair in fieldValueDic)
{
oListItem[pair.Key]
= pair.Value;
}
oListItem.Update();
clientContext.Load(oListItem);
clientContext.ExecuteQueryAsync(onSuccess, onFail);
}

public void Dispose()
{
if (null != clientContext)
clientContext.Dispose();
}
}
}

  其中的ClientRequestSucceededEventHandler onSuccess, ClientRequestFailedEventHandler onFail分别为异步操作成功和失败后的回调委托,可以在调用方法的时候传出这两个委托的具体实现。由于是客户端程序,所以真正的操作都是在ExecuteQueryAsync方法调用时才开始执行的。

   4、在Main.xaml中添加页面UI:


<UserControl xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" x:Class="ADSK.AEC.SP2010.Portal.HitCount.MainPage"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable
="d"
d:DesignHeight
="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Left">
<Border CornerRadius="5" BorderThickness="2" BorderBrush="#B8BABD" Width="80" Height="40">
<StackPanel >
<TextBlock x:Name="txtHitName" FontFamily="Verdana" FontSize="10" Padding="2" Text="Page Hits" HorizontalAlignment="Center" VerticalAlignment="Center" FontStretch="ExtraExpanded">
</TextBlock>
<TextBox x:Name="txtHitCount" Text="" Foreground="Blue" FontSize="10" Padding="0" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" FontStretch="UltraCondensed" BorderThickness="0"></TextBox>
</StackPanel>
</Border>
</Grid>
</UserControl>

  用一个textbox来显示访问次数,在main.xaml.cs中添加实现逻辑:


public partial class MainPage : UserControl
{
private ADSK.AEC.SP2010.ClientOM.ClientOMProxy clientProxy = null;
private ListItemCollection targetHitCount = null;
public MainPage(string siteUrl)
{
InitializeComponent();
string currentPageURL = HtmlPage.Document.DocumentUri.ToString();
string viewXML = @"<View><ViewFields><FieldRef Name='HitCount'/></ViewFields><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + currentPageURL + "</Value></Where></Eq></Query></View>";
clientProxy
= new ClientOM.ClientOMProxy(siteUrl);
clientProxy.successEventHandler
+= new ClientRequestSucceededEventHandler(OnRequestSucceeded);
clientProxy.failEventHandler
+= new ClientRequestFailedEventHandler(OnRequestFailed);
clientProxy.GetListItemsAsync(
"Hit Count", viewXML, out targetHitCount);
}

private void BindData()
{
if (targetHitCount != null && targetHitCount.Count != 0)
{
long hitCount = Convert.ToInt64(targetHitCount[0]["HitCount"].ToString());
txtHitCount.Text
= (hitCount + 1).ToString();
lock (targetHitCount[0])
{
Dictionary
<string, object> fieldValueDic = new Dictionary<string, object>();
fieldValueDic[
"HitCount"] = hitCount+1;
clientProxy.UpdateListItemAsync(
"Hit Count", targetHitCount[0], fieldValueDic, null, null);
}
}
else
{
txtHitCount.Text
= "1";
Dictionary
<string, object> fieldValueDic = new Dictionary<string, object>();
fieldValueDic[
"Title"] = HtmlPage.Document.DocumentUri.ToString();
fieldValueDic[
"HitCount"] = 1;
clientProxy.CreateListItemAsync(
"Hit Count", fieldValueDic, null, null);
}
}

private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(BindData);
clientProxy.Dispose();
}

private void OnRequestFailed(Object sender, ClientRequestFailedEventArgs args)
{
clientProxy.Dispose();
}

  逻辑比较简单,这里就不详细解释了。在App.xaml.cs中的Startup事件中添加读入参数的方法:


private void Application_Startup(object sender, StartupEventArgs e)
{
string siteUrl = e.InitParams["SiteUrl"];
siteUrl
= System.Windows.Browser.HttpUtility.UrlDecode(siteUrl);
this.RootVisual = new MainPage(siteUrl);
}

  OK,build一下,先在客户端调试一下,这里注意了,如果你机器安装过360的浏览器的话,那么有可能调试的时候无法进入断点,保险的方法还是将IE设置为默认浏览器,然后调试。

  5、将生成的.xap文件上传到SharePoint2010的一个Document library中,在任意一个你想统计访问次数的页面上Add web part,选择Silverlight web part,在url中添加xap上传的路径,然后在Other Settings中添加外部传入参数,形式为SiteUrl = hit count list 所属site的url。

  完成以后,可以看见页面上已经有了访问次数统计了:

一起谈.NET技术,一个Silverlight的网页访问计数器

  这里只是简单的创建了一个页面次数访问的统计器,关键还是熟悉了SharePoint2010下Silverlight的客户端访问模型,欢迎大家讨论。

上一篇:带你读《6G需求与愿景》第三章6G 设计思路与愿景3.2 6G 业务愿景 (二)


下一篇:Markdown基础语法