4

我在 Sharepoint 中有一个自定义 Web 部件,并且正在尝试将一些 ajax 应用到 Web 部件。

相同的代码适用于 .net Web 应用程序,但不适用于 Sharepoint Web 部件。

.net web 应用程序的代码如下所示(aspx 页面和后面的代码),它运行良好。:

ASPX 文件

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  // Add the page method call as an onclick handler for the div.
    $("#Result").click(function () {
        var loc = window.location.href;
    $.ajax({
      type: "POST",
      url: loc + "/GetMessage",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Replace the div's content with the page method's return.
        $("#Result").text(msg.d);
      }
    });
  });
});

   </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div id="Result">Click here for the time.</div>
    <asp:Button ID="btnClick" runat="server" Text="Button" />
</asp:Content>

.CS 文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace WebApplication3
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetMessage()
        {
            return "Codebehind method call...";
        }
        [WebMethod]
        public static string GetDate()
        {
            return DateTime.Now.ToString();
        }
    }
}

当我在不进行回发的情况下单击它时,文本“Click here for the time”会更改为“Code behind method call”。我可以进入代码,这会GetMessage()在 .net Web 应用程序中调用代码中的方法。

但是,这在 sharepoint 2010 中的 webpart 上不起作用。有人有什么想法吗?

4

3 回答 3

1

另一种选择是创建一个 SharePoint 应用程序页面。这是您的代码稍作修改以用作应用程序页面(使用 SharePoint 母版页内容部分,并且 AJAX 指向应用程序页面:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationPageWS.aspx.cs" Inherits="WebMethod.Layouts.WebMethod.ApplicationPageWS" DynamicMasterPageFile="~masterurl/default.master" %>


<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<SharePoint:ScriptLink ID="ScriptLink1" runat="server" Localizable="false" Name="js/jquery-1.8.2.js"></SharePoint:ScriptLink>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/javascript">
    $(document).ready(function () {
        // Add the page method call as an onclick handler for the div.
        $("#Result").click(function () {

            $.ajax({
                type: "POST",
                url: "**/_layouts/WebMethod/ApplicationPageWS.aspx/GetMessage**",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#Result").text(msg.d);
                },
                error: function (msg) {
                    alert(msg.responseText);
                }
            });
        });
    });
</script>

<div id="Result">Click here for the time.</div>
<asp:Button ID="btnClick" runat="server" Text="Button" />

</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
     Application Page
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
    My Application Page
 </asp:Content>

接下来将您的服务端更改为从 LayoutsPageBase 而不是 WebPart 继承:

using System;
using System.Web.UI;
using System.Web.Services;
using Microsoft.SharePoint.WebControls;

namespace WebMethod.Layouts.WebMethod
{
    public partial class ApplicationPageWS : LayoutsPageBase
    {
        protected override void OnInit(EventArgs e)
        {
             base.OnInit(e);
             //InitializeControl();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string GetMessage()
    {
        return "Codebehind method call...";
    }

    [WebMethod]
    public static string GetDate()
    {
        return DateTime.Now.ToString();
    }
}

}

希望此解决方案适用于您的情况,如果不是,并且您需要从 Web 部件调用 Web 服务,则需要在 Web 部件之外创建 Web 服务。这在http://dbremes.wordpress.com/2011/01/03/ajax-style-web-parts-in-sharepoint-the-easy-way/中有很好的解释

史蒂夫

于 2012-11-20T06:21:21.577 回答
0

首先确保所有需要的 js 文件都已正确部署和加载。在 html 源代码中查找 url 并将它们复制到新选项卡中,看看它们是否正确加载。

还尝试将所有 JavaScript 文件放在服务器上(所以没有 'https://ajax.googleapis.com/...' 网址)。我曾经遇到过跨域脚本权限的问题。

加载的不同 JavaScript 库也可能存在冲突。对于 SharePoint 上下文中的 jQuery,我通常使用“noConflict”选项。

var $j = jQuery.noConflict(); $j(document).ready(function() { .....

于 2012-11-19T13:48:58.883 回答
-2

我只想说你必须使用 POST 你不能使用 GET 发现很难。

于 2014-02-13T03:07:02.223 回答