Partager via


Posting Windows SharePoint Services RPC Methods

You can use URL protocol in combination with Collaborative Application Markup Language (CAML) and Windows SharePoint Services Remote Procedure Call (RPC) protocol to post requests to a front-end server running Microsoft Windows SharePoint Services. 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.

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

The following programming task shows how to create a form for posting requests.

  • Create an ASPX page and add a page directive like 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 Windows SharePoint Services deployment from the default.aspx file in the Local_Drive:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\TEMPLATE\LCID(1033 in English)\STS folder, or from information provided for the Microsoft.SharePoint assembly at Local_Drive:\WINDOWS|WINNT\assembly in Windows Explorer.

  • 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>
    
  • 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  />
      <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.

  • 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>
    

The Batch element must contain a Version attribute that specifies the version of Windows SharePoint Services 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.