I'm trying to define a WebMethod in one of my Publishing page layouts.
This WebMethod is intended to be called when a user takes a specific action in an Editor Part in a Visual Web Part, however, you can't have a WebMethod in a Server Control or User Control. This web part will only exist on this specific page layout, so I figured the best way would be to implement the WebMethod in the page layout itself. I would then implement a static method in the Editor Part which I would call from the WebMethod.
I implemented this concept successfully in a sample asp.net web application then set out to do it in SharePoint. That's when the fun began :)
Set EnablePageMethods on ScriptManager
I started by setting the EnablePageMethods property of the ScriptManager in my master page to true.
Define WebMethod in Page Layout
In the page layout, I define the WebMethod as follows:
[WebMethod]
public static void MyWebMethod(string someParams)
{
MyEditorPart.MyWebMethod(someParams);
}
In the EditorPart, MyWebMethod is implemented as a simple static method:
public static void MyWebMethod(string someParams)
{
// do something
}
I understand that being a static method in the editor part, I won't have access to any of the instance data for it. I'm postponing dealing with that issue until I get the web method working...
Register Scripts in EditorPanel
In the OnInit event of MyEditorPanel, I register the script:
if (!this.Page.ClientScript.IsClientScriptBlockRegistered("myScript"))
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"myScript",
"the contents of my JavaScript script"
true);
Background info on the script: The script runs on the stop event of a jQueryUI Sortable list. It's intended to pass the ordered list (formatted) back to the web method.
Testing
I'm able to successfully trigger the script by taking the appropriate action in the MyEditorPanel server control.
I see the request in Fiddler as follows but with a 500 error:
http://localhost/Site/Sub-Site/Pages/default.aspx/MyWebMethod
Here's the error:
Unknown web method MyWebMethod.<br>Parameter name: methodName
[ArgumentException: Unknown web method MyWebMethod.
Parameter name: methodName]
System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +841944
System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +217
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171
When I implemented this in asp.net, I could immediately hit the break point in the page. However, in SharePoint, the WebMethod needs to be defined somewhere else. As far as I can tell, that's the only difference between the two implementations.
How have other people approached this?
Thank you
EDIT
I've abandoned this approach. the necessary plumbing just doesn't seem to be there to use a WebMethod defined in a page layout or master page to communicate back to the web part or editor part.