博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

SharePoint 2010中的客户端模型

Posted on 2011-01-06 20:31  生鱼片  阅读(3295)  评论(3编辑  收藏  举报

1.介绍

客户端模型是SharePoint 2010才提供的,可以更灵活的在任何客户端设备中操作SharePoint对象,在2007版本中没有客户端模型,2010中有三种客户端模型JavaScript,SilverLight,.NET Managed。下面是对比情况:

 

image      image

 

2.原理

当我们使用客户端模型访问SharePoint 2010服务器对象的时候,请求首先会被打包成XML格式的SharePoint对象模型,然后在由客户端发送到服务器端,在服务器端调用服务器端的对象模型,。然后得到反应结果以JSON

格式发给客户端对象模型,客户端模型在解析得到的结果,展现给前端,如下图:

image

3.客户端模型对象

服务器端 客户端 Javascript
Microsoft.SharePoint.SPContext Microsoft.SharePoint.Client.ClientContext SP.ClientContext
Microsoft.SharePoint.SPSite Microsoft.SharePoint.Client.Site SP.Site
Microsoft.SharePoint.SPWeb Microsoft.SharePoint.Client.Web SP.Web
Microsoft.SharePoint.SPList Microsoft.SharePoint.Client.List SP.List
Microsoft.SharePoint.SPListItem Microsoft.SharePoint.Client.ListItem SP.ListItem
Microsoft.SharePoint.SPField Microsoft.SharePoint.Client.Field SP.Field

4.一些例子代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace OM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Cursor old = this.Cursor;
            this.Cursor = Cursors.WaitCursor;

            Microsoft.SharePoint.Client.ClientContext clientContext = new Microsoft.SharePoint.Client.ClientContext("http://moss2010/sites/test");
            NetworkCredential nwc = CredentialCache.DefaultNetworkCredentials;
            clientContext.Credentials = nwc;
            Microsoft.SharePoint.Client.Web web = clientContext.Web;

            clientContext.Load(web);
            clientContext.ExecuteQuery();

            this.lblSiteTitle.Text = web.Title;

            this.Cursor = old;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Cursor old = this.Cursor;
            this.Cursor = Cursors.WaitCursor;

            Microsoft.SharePoint.Client.ClientContext clientContext = new Microsoft.SharePoint.Client.ClientContext("http://moss2010/sites/test");
            NetworkCredential nwc = CredentialCache.DefaultNetworkCredentials;
            clientContext.Credentials = nwc;
            Microsoft.SharePoint.Client.Web web = clientContext.Web;

            var query = from list
                        in clientContext.Web.Lists
                        where list.Title == "SharePoint Team Members"
                        select list;

            var result = clientContext.LoadQuery(query);
            clientContext.ExecuteQuery();

            Microsoft.SharePoint.Client.List listMember = result.SingleOrDefault<Microsoft.SharePoint.Client.List>();

            Microsoft.SharePoint.Client.CamlQuery caml = new Microsoft.SharePoint.Client.CamlQuery();
            caml.ViewXml = @"<View><Query></Query></View>";

            Microsoft.SharePoint.Client.ListItemCollection listItems = listMember.GetItems(caml);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();

            List<Employee> allDatas = new List<Employee>();
            foreach (Microsoft.SharePoint.Client.ListItem item in listItems)
            {
                allDatas.Add(new Employee(item["Title"], item["Age"], item["Sex"]));
            }
            dataGridView1.DataSource = allDatas;

            this.Cursor = old;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Microsoft.SharePoint.Client.ClientContext clientContext = new Microsoft.SharePoint.Client.ClientContext("http://moss2010/sites/test");
            NetworkCredential nwc = CredentialCache.DefaultNetworkCredentials;
            clientContext.Credentials = nwc;
            Microsoft.SharePoint.Client.Web web = clientContext.Web;

            var query = from list
                        in clientContext.Web.Lists
                        where list.Title == "SharePoint Team Members"
                        select list;

            var result = clientContext.LoadQuery(query);
            clientContext.ExecuteQuery();

            Microsoft.SharePoint.Client.List listMember = result.SingleOrDefault<Microsoft.SharePoint.Client.List>();

            Microsoft.SharePoint.Client.CamlQuery caml = new Microsoft.SharePoint.Client.CamlQuery();
            caml.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='Employee_x0020_Name' /><Value Type='Text'>" + tbxInput.Text + "</Value></Eq></Where></Query></View>";

            Microsoft.SharePoint.Client.ListItemCollection listItems = listMember.GetItems(caml);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();

            this.lblOutput.Text = listItems[0].FieldValues["Title"].ToString();

            this.Cursor = old;
        }
    }

    public class Employee
    {
        public Employee(object title, object age, object sex)
        {
            this.Age = age;
            this.Sex = sex;
            this.Title = title;
        }
        public object Title { get; set; }
        public object Age { get; set; }
        public object Sex { get; set; }
    }
}