Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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&amp; 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.

share|improve this question
    
What code behind file have you put that C# code in? Is this in a custom page layout you've created? What class did you inherit in the C# codebehind file? –  James Love Dec 26 '11 at 19:06
    
James, yes the C# code is in the code behind of a custom page layout. The class inherits from PublishingLayoutPage. Thanks –  George Durzi Dec 26 '11 at 19:07
    
I should also point out that absolutely nowhere else in the current implementation of the SharePoint object model uses this model at all, as I believe this is incompatible with the way the Publishing Framework works, so is probably not a supported customisation. –  James Love Dec 26 '11 at 19:08
1  
Think you can keep the jQuery UI Sortable bit in the Editor Part, but you're gonna need a postback (to sync the web part properties to the personalisation store) to 'update' the visual web part UI. –  James Love Dec 26 '11 at 19:15
1  
I have used web methods on an custom application page in SharePoint and that worked perfectly. Haven't tried out on a page layout. –  justforkix09 Dec 28 '11 at 17:08
show 2 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.