码迷,mamicode.com
首页 > 其他好文 > 详细

Sharepoint

时间:2014-05-15 10:48:16      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:datagridview   style   blog   class   code   java   

1.介绍

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

 

bubuko.com,布布扣      bubuko.com,布布扣

 

2.原理

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

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

bubuko.com,布布扣

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.一些例子代码

bubuko.com,布布扣
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; }
    }
}
bubuko.com,布布扣

 

bubuko.com,布布扣
ClientContext clientContext = new ClientContext("http://servername/");
clientContext.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(clientContext_ExecutingWebRequest);
Web site = clientContext.Web;
clientContext.Load(site);
clientContext.ExecuteQuery();
static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
{
    e.WebRequestExecutor.WebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}
bubuko.com,布布扣

 

Uri uri = new Uri("http://tempuri.org/");
ICredentials credentials = CredentialCache.DefaultCredentials;
NetworkCredential credential = credentials.GetCredential(uri, "Basic");

 

Sharepoint,布布扣,bubuko.com

Sharepoint

标签:datagridview   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/MarchThree/p/3728872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!