Another option is to create a SharePoint application page. Here is your code slightly modified to act as an application page (uses sections of the contents of the SharePoint main page, and AJAX points to the application page:
<%@ 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 () { </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>
Then change your service side to inherit from LayoutsPageBase instead of 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);
}
Hope this solution works in your situation, if not, and you need to call the web service from the web part, you will need to create a web service outside of your web part. This is well explained at http://dbremes.wordpress.com/2011/01/03/ajax-style-web-parts-in-sharepoint-the-easy-way/
Steve
source share