Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another option is to create a SharePoint Application Page. Here is your code slightly modified to work as an Application Page (uses SharePoint Master Page content sections, and the AJAX points to the application page:</p> <pre><code>&lt;%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %&gt; &lt;%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt; &lt;%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt; &lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationPageWS.aspx.cs" Inherits="WebMethod.Layouts.WebMethod.ApplicationPageWS" DynamicMasterPageFile="~masterurl/default.master" %&gt; &lt;asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server"&gt; &lt;SharePoint:ScriptLink ID="ScriptLink1" runat="server" Localizable="false" Name="js/jquery-1.8.2.js"&gt;&lt;/SharePoint:ScriptLink&gt; </code></pre> <p></p> <pre><code>&lt;asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server"&gt; &lt;script type="text/javascript"&gt; $(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); } }); }); }); &lt;/script&gt; &lt;div id="Result"&gt;Click here for the time.&lt;/div&gt; &lt;asp:Button ID="btnClick" runat="server" Text="Button" /&gt; &lt;/asp:Content&gt; &lt;asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server"&gt; Application Page &lt;/asp:Content&gt; &lt;asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" &gt; My Application Page &lt;/asp:Content&gt; </code></pre> <p>Next change your service-side to inherit from LayoutsPageBase instead of WebPart:</p> <pre><code>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(); } } </code></pre> <p>}</p> <p>Hope this solution work in your situation, if not, and you need to call a web service from a web part, you will need to create the web service outside of your Web Part. This is nicely explained at <a href="http://dbremes.wordpress.com/2011/01/03/ajax-style-web-parts-in-sharepoint-the-easy-way/" rel="nofollow">http://dbremes.wordpress.com/2011/01/03/ajax-style-web-parts-in-sharepoint-the-easy-way/</a></p> <p>Steve </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload