SharePoint 2010 implementation of a Silverlight Web Access counter

Source: Internet
Author: User
Tags foreach count object model reference silverlight

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.