10.5. Using FrontPage RPCThe FrontPage Remote Procedure Call (RPC) methods provide yet another way to change SharePoint sites and get content remotely. The RPC methods are similar to the URL commands discussed earlier, except RPC uses HTTP POST rather than GET to send requests .
Use RPC methods when you want to compose your changes or queries in Collaborative Markup Language (CAML) rather than through web service methods. CAML is a declarative approach to programming SharePoint using XML rather than a procedural programming language such as Visual Basic .NET. There are several advantages to this approach:
Table 10-8 lists the methods available through RPC. The URL commands are also available through RPC but aren't included in this table since they are already listed in Table 10-6. Table 10-8. SharePoint RPC methods
10.5.1. Preparing a Page for RPCBecause RPC methods can change content, the post must include user information so SharePoint can authenticate and authorize the method. That means you must take a few special steps to execute RPC commands on a web page:
When creating a page that includes RPC commands, it's easiest if you start with a web part page generated by SharePoint, since those pages include the @ Register directive for the SharePoint web controls. For example, I created RPCDemo.aspx in the TestPages document library and edited it using FrontPage to create a platform for testing RPC commands. Then, I added the following script to the HEAD element at the beginning of the page: <!-- This directive registers the FormDigest WebControl --> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <html dir="ltr"> <HEAD> <!-- Add this script to include user info in the CASML --> <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> </HEAD> Next , I added the following form after the closing form tag generated by SharePoint: <!-- SharePoint web part zones omitted here --> <\form> <!-- Form used to post RPC commands to SharePoint --> <form class="ms-formbody" method="post" action="http://wombat1/_vti_bin/owssvr.dll" onsubmit="InsertSecurityValidation(this);" target="result" > <!-- This control provides user information --> <SharePoint:FormDigest runat="server"/> Enter CAML containing RPC methods and click Run to see the result: <input type="hidden" name="Cmd" value="DisplayPost" /> <!-- Source for CAML --> <textarea name="PostBody" style="width=100%;height=200"> </textarea> <br> <input type="submit" value="Run" /> <input type="reset" value="Clear" /> <br> Result: <!-- Target for results --> <IFRAME name="result" src="::blank.htm" style="width=100%;height=200"/> </form> Figure 10-8 shows the resulting web part page. You can enter CAML in the textbox, click Run, and see the results in the IFRAME as shown in the figure. animal 10-8. Using RPCDemo.aspx to test RPC methodsI created RPCDemo.aspx as a test bed for composing RPC method calls. If the CAML is incorrect, an error displays in the result. You can cut and paste samples from the SharePoint SDK into the textbox, modify the parameters, and run the commands to see how they work. Once you're satisfied with the results, you can hard-code the CAML onto a new web page using a hidden input control as shown here: <!-- Embedded RPC --> <form class="ms-formbody" method="post" action="http://wombat1/_vti_bin/owssvr.dll" onsubmit="InsertSecurityValidation(this);" target="_blank" > <!-- This control provides user information --> <SharePoint:FormDigest runat="server"/> Click Run to see the result in a pop-up window. <input type="hidden" name="Cmd" value="DisplayPost" /> <!-- Source for CAML --> <input type="hidden" name="PostBody" value=" <Method ID='0,DisplayPost'> <SetList Scope='Request'>Lists</SetList> <SetVar Name='View'>EnumLists</SetVar> <SetVar Name='Cmd'>DisplayPost</SetVar> <SetVar Name='XMLDATA'>TRUE</SetVar> </Method>" /> <br> <input type="submit" value="Run" /> </form> Don't forget to add the InsertSecurityValidation script to the new page's header. This sample displays the results in a new pop-up window ( target="_blank ") rather than on the page in an inline frame. 10.5.2. Common RPC TasksThe following sections make up a quick tour of some common tasks you might want to perform using RPC. In each of these samples, you'll need to replace the GUIDs provided for lists and views with values from your own sites. 10.5.2.1 Identifying listsRPC methods identify the list they act on by the list's GUID. You can get the GUIDs of all the lists in a site through RPC by calling the DisplayPost method with the View parameter set to EnumLists as shown here: <Method ID="0,DisplayPost"> <SetList Scope="Request">Lists</SetList> <SetVar Name="View">EnumLists</SetVar> <SetVar Name="Cmd">DisplayPost</SetVar> <SetVar Name="XMLDATA">TRUE</SetVar> </Method> Once you find the GUID of the list you want, you specify that value in the SetList element. For example, the following method exports the Announcements list: <Method ID="0,ExportList"> <SetList Scope="Request">{41D7D046-1E0A-4FB2-A096-C063D210D552}</SetList> <SetVar Name="Cmd">ExportList</SetVar> </Method> 10.5.2.2 Combining multiple methodsUse the Batch element to combine two or more RPC methods in a single post. For example, the following CAML adds two items to the Announcements list: <ows:Batch Version="6.0.2.5608" OnError="Return"> <Method ID="Anouncement1"> <SetList>{41D7D046-1E0A-4FB2-A096-C063D210D552}</SetList> <SetVar Name="ID">New</SetVar> <SetVar Name="Cmd">Save</SetVar> <SetVar Name="urn:schemas-microsoft-com:office:office#Title">Annoucment Title1</SetVar> <SetVar Name="urn:schemas-microsoft-com:office:office#Body">Annoucement text</SetVar> <SetVar Name="urn:schemas-microsoft-com:office:office#Expires">2005-09-14T00:00:00Z</SetVar> </Method> <Method ID="Announcment2"> <SetList>{41D7D046-1E0A-4FB2-A096-C063D210D552}</SetList> <SetVar Name="ID">New</SetVar> <SetVar Name="Cmd">Save</SetVar> <SetVar Name="urn:schemas-microsoft-com:office:office#Title">Annoucement Title2</SetVar> <SetVar Name="urn:schemas-microsoft-com:office:office#Body">Announcment text</SetVar> <SetVar Name="urn:schemas-microsoft-com:office:office#Expires">2005-12-18T00:00:00Z</SetVar> </Method> </ows:Batch> The Batch element's OnError attribute determines what happens if one of the methods fails. The Return setting causes the methods to stop as soon as an error occurs; Continue causes SharePoint to skip the methods with errors and continue with the next method. 10.5.2.3 Querying listsUse the Display method to perform a query on a list and return only a specific set of fields. For example, the following query returns three fields from a list using the filter criteria introduced=2000 (note that field names are case-sensitive): <Method ID="0,Display"> <SetList Scope="Request">{5E6561D4-7048-45AB-BFA1-2D1991BAF3B1}</SetList> <SetVar Name="Cmd">Display</SetVar> <SetVar Name="XMLDATA">False</SetVar> <SetVar Name="Query">name Title introduced</SetVar> <SetVar Name="FilterField1">introduced</SetVar> <SetVar Name="FilterValue1">2000</SetVar> </Method> For queries that require filtering or sorting, you can also specify a view that defines those criteria. For example, the following query returns items from the list specified by one of the list views: <Method ID="0,Display"> <SetList Scope="Request">{5E6561D4-7048-45AB-BFA1-2D1991BAF3B1}</SetList> <SetVar Name="Cmd">Display</SetVar> <SetVar Name="XMLDATA">False</SetVar> <SetVar Name="View">{21E03766-D0CF-4942-B2C0-DF4E3706DD50}</SetVar> </Method> 10.5.2.4 Creating listsUse the NewList method to create a new list or document library on a site. The ListTemplate parameter determines what type of list is created, as shown in Table 10-9. Table 10-9. ListTemplate settings
For example, the following method creates a new document library: <Method ID="0,NewList"> <SetVar Name="Cmd">NewList</SetVar> <SetVar Name="ListTemplate">101</SetVar> <SetVar Name="Title">New Document Library</SetVar> </Method> 10.5.2.5 Creating pagesTo add a new web page to the document library created in the preceding section, get the library's GUID; then use the NewWebPage method: <Method ID="0,NewWebPage"> <SetList Scope="Request">{28F54F4C-BA98-43E9-A60F-8C81E5365560}</SetList> <SetVar Name="Cmd">NewWebPage</SetVar> <SetVar Name="ID">New</SetVar> <SetVar Name="Type">WebPartPage</SetVar> <SetVar Name="WebPartPageTemplate">3</SetVar> <SetVar Name="Overwrite">true</SetVar> <SetVar Name="Title">TempPage</SetVar> </Method> The WebPartPageTemplate parameter determines the layout of the new page, as shown in Table 10-10. Table 10-10. WebPartPageTemplate settings
10.5.2.6 Deleting itemsThe following code deletes the web page created in the preceding section: <Method ID="0,Delete"> <SetList Scope="Request">28F54F4C-BA98-43E9-A60F-8C81E5365560</SetList> <SetVar Name="Cmd">Delete</SetVar> <SetVar Name="ID">1</SetVar> <SetVar Name="owsfileref">http://wombat1/New Document Library/TempPage.aspx</SetVar> </Method> 10.5.2.7 Deleting listsUse this code to delete the document library created earlier: <Method ID="0,DeleteList"> <SetList Scope="Request">28F54F4C-BA98-43E9-A60F-8C81E5365560</SetList> <SetVar Name="Cmd">DeleteList</SetVar> </Method> 10.5.3. Resources
|