As you know, there are three main ways that SharePoint 2010 supports client-side object model access
1..net Client object model, 2.Javascript client object Model 3.Silverlight client object model, here is a simple implementation of Silverlight Web counter, to achieve familiarity with the client object model.
In fact, this simple access counter is mainly to count the number of pages refreshed, logic is very simple, when the page is load, the number of times +1, the number of times and the address of the page as an item stored in a SharePoint list. The following are some specific steps:
1 Preparation: Create a list of access times and page addresses under a site in SharePoint 2010, which we can name as hit Count list.
2 VS2010 to create Silverlight application Project, and then add a DLL reference to the client object model, in SharePoint2010, Silverlight's supporting client object model DLL file is typically stored in C:\ Program Files\Common files\microsoft Shared\Web Server Extensions\14 \template\layouts\clientbin, so we add first in the project Reference, add Microsoft.SharePoint.Client.Silverlight.dll to the path above and Microsoft.SharePoint.Client.Silverlight.Runtime.dll two DLLs.
3 Add a Class,clientomproxy.cs to the project as a proxy class for Silverlight access to SharePoint2010 data, because Silverlight access takes the form of asynchrony, so several basic methods of operation are as follows
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 override void Getlistitemsasync (String listname, String viewxml, out ListItemCollection ListItems, clientrequests Ucceededeventhandler 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 ();
}
}
}
The Clientrequestsucceededeventhandler onsuccess, Clientrequestfailedeventhandler Onfail are both successful and failed callback delegates for the asynchronous operation, The concrete implementation of these two delegates can be heard when the method is invoked. Because it is a client program, the real operation starts when the Executequeryasync method call is performed.