Sharepoint2010使用AJAx 获取 OData Service (实现客户端获取sharepoint2010的数据)---第一篇

 

我们在做Sharepoint2007时,习惯于用对项目模型来获取数据,但都是后台代码,和客户端不能很好的互动,感觉很不爽,今天我来迎来了Sharepoint2010,它带来了客户端对象模型,听名字就知道干什么的呢,但今天我们不是讲客户端对象模型,还是另一种方式获取Sharepoint数据源,那就是 OData Service 。

 

(一) 使用OData Service  的前提得必须要安装一个补丁,叫做“ADO.NET Data Services Update for .NET Framework 3.5 SP1 for Windows 7 and Windows Server 2008 R2” ,安装好后重启,然后打开“http://mySite/_vti_bin/ListData.svc ”,(mySite)是自己的站点,你可以看到自己站点上所有的列表信息,如果你要看某一个列表的数据,则打开这个地址“http://mySite/_vti_bin/ListData.svc/myListName” 。

 

(二)你要使用AJAX获取OData Service ,则需要安装AJAX控件工具包源代码(我用的是AjaxControlToolkit.Source.zip),然后把其中的几个文件按下面的方法放置:

 

  •  复制  AjaxControlToolkit.Source\SampleWebSites\AjaxClientWebSite\Scripts;  到    C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ \14\TEMPLATE\LAYOUTS\Scripts
  • 复制 AjaxControlToolkit.Source\Client\MicrosoftAjax\Templates; 到    C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ \14\TEMPLATE\LAYOUTS\Scripts

     

    (三)写代码(用VS2010 创建一个Sharepoint空项目,然后创建一个页面)

     

    代码
    <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
    <%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
    <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserStories.aspx.cs" Inherits="PreDemo.Layouts.PreDemo.UserStories" DynamicMasterPageFile="~masterurl/default.master" %>

    <asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <style type="text/css">
    .sys
    -template
    {
    display:none;
    }
    </style>
    <script src="/_layouts/Scripts/MicrosoftAjax/Start.js" type="text/javascript"></script>
    <script src= "/_layouts/Scripts/MicrosoftAjax/MicrosoftAjax.js" type="text/javascript"></script>
    <script type="text/javascript">
    Sys.require([
    Sys.components.dataView,
    Sys.components.openDataServiceProxy,
    Sys.scripts.jQuery
    ]);
    Sys.onReady(
    function () {
    var dataSource = Sys.create.openDataServiceProxy('/_vti_bin/ListData.svc');


    Sys.query(
    "#userStoriesList").dataView({
    dataProvider: dataSource,
    fetchOperation:
    "Tasks",
    feachParameters: { orderby:
    '标题' },
    autoFetch:
    "true"
    });

    });
    </script>
    </asp:Content>
    <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    Hello World
    !
    <ul id="userStoriesList" class="sys-template">
    <li>{{ 标题 }}</li>
    </ul>
    </asp:Content>
    <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    应用程序页
    </asp:Content>
    <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
    我的应用程序页
    </asp:Content>

    Tasks是我的任务列表,<li>{{ 标题 }}</li> 是绑定数据

     最后一个要注意的是sys-template类。这是一个在ASP.Net AJAX中预定义的CSS类,当完成渲染工作后,由ASP.Net AJAX将其设置为display:block。因此,我们需要为sys-template创建一个CSS类并设为display:none,这样在页面加载过程中最终用户就看不到你的模板代码了。

    (四)最后,你按F5部署到站点上,就可以看见自己的数据了。

     

    下次还要写AJAX如何运用这些数据实现很炫的效果。

     

    参考资料:http://www.endusersharepoint.com/EUSP2010/2010/05/20/client-side-ajax-applications-in-sharepoint-2010-%E2%80%93-part-4-jquery-integration-and-persistence/

     

  •  

    来源:http://www.cnblogs.com/jlydboy/articles/1807914.html

    posted @ 2010-09-15 16:13  绿森林  阅读(353)  评论(0编辑  收藏  举报