How to: Post SharePoint Foundation RPC Methods

Applies to: SharePoint Foundation 2010

You can use URL Protocol in combination with Collaborative Application Markup Language (CAML) and SharePoint Foundation Remote Procedure Call Protocol (RPC) to post requests to a front-end Web server that is running Microsoft SharePoint Foundation 2010. Use the CAML Method element to post a single request, or a Batch element containing multiple Method elements to post multiple requests, through the post body of a form in an .ASPX page. The following programming task shows how to create a form for posting requests.

To create a form for posting requests

  1. Create an .aspx page and add a page directive, such as the following, that registers the Microsoft.SharePoint.WebControls namespace for the server control.

    <%@ Register Tagprefix="SharePoint" 
      Namespace="Microsoft.SharePoint.WebControls" 
      Assembly="Microsoft.SharePoint, Version=11.0.0.0, 
      Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    

    Note

    You can obtain the PublicKeyToken value for the current SharePoint Foundation 2010 deployment from the default.aspx file in the path Local_Drive:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\SiteTemplates\sts, or from information provided for the Microsoft.SharePoint assembly at Local_Drive:\%WINDOWS%\assembly in Windows Explorer.

  2. Add the following script block to automatically insert the FormDigest control into each Method element when the form is submitted.

    <script type="text/javascript" language="JavaScript">
    function InsertSecurityValidation(oForm)
    {
        var sFormDigest = '<SetVar Name="__REQUESTDIGEST">' + 
          oForm.elements["__REQUESTDIGEST"].value + "</SetVar>\n";
        var oPostBody = oForm.elements["PostBody"];
        var rePattern = /<\/Method>/g;
        oPostBody.value = oPostBody.value.replace(rePattern, 
            sFormDigest + "</Method>");
    }
    </script>
    
  3. Create a form in the .aspx page such as the following, where the action attribute contains the URL command that is posted to the server.

    <form method="post" 
      action="http://Server_Name/sites/Site_Name/_vti_bin/owssvr.dll" 
      onsubmit="InsertSecurityValidation(this);">
      <SharePoint:FormDigest runat="server" />
      <input type="hidden" name="Cmd" value="DisplayPost" />
      <textarea rows="18" name="PostBody" cols="72"></textarea>
      <input type="submit" value="Submit" />
      <input type="reset" value="Reset" />
    </form>
    

    The form uses the DisplayPost method to request that the server render whatever CAML is contained within the post body.

  4. Open the form in the browser and insert a block of code like the following into the post body, which uses the Batch element to post multiple RPC methods for adding two announcements to an announcements list:

    <ows:Batch Version="6.0.2.5608" OnError="Return">
      <Method ID="A1">
        <SetList>List_GUID</SetList>
        <SetVar Name="ID">New</SetVar>
        <SetVar Name="Cmd">Save</SetVar>
        <SetVar Name="urn:schemas-microsoft-com:office:office#Title">
            New Program Manager</SetVar>
        <SetVar Name="urn:schemas-microsoft-com:office:office#Body">
            Congratulations to Jane for her promotion!</SetVar>
        <SetVar Name="urn:schemas-microsoft-com:office:office#Expires">
            2001-09-14T00:00:00Z</SetVar>
      </Method>
      <Method ID="A2">
        <SetList>List_GUID</SetList>
        <SetVar Name="ID">New</SetVar>
        <SetVar Name="Cmd">Save</SetVar>
        <SetVar Name="urn:schemas-microsoft-com:office:office#Title">
            Sales rise by 10%</SetVar>
        <SetVar Name="urn:schemas-microsoft-com:office:office#Body">The
            accounting department has released its quarterly report. 
            Check it out!</SetVar>
        <SetVar Name="urn:schemas-microsoft-com:office:office#Expires">
            2001-12-18T00:00:00Z</SetVar>
      </Method>
    </ows:Batch>
    

Compiling the Code

The Batch element must contain a Version attribute that specifies the version of SharePoint Foundation that the server is running. Each SetList node must specify the GUID of the list.

When the form is submitted, this sample adds two records to an announcements list.

Security

If security validation is enabled on the server, which is true by default in SharePoint Foundation, then a FormDigest server control must be added within the form. For information about this control and security validation, see Security Validation and Making Posts to Update Data. Each Method element used in a post must have a form digest specified; otherwise, the post will not pass security validation.