一个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();
}
}
}

NET技术一个Silverlight的网页访问计数器,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。