转:How to submit the rows of a repeating table in InfoPath to a SharePoint list
2009-07-31 19:39
681 查看
转: http://www.bizsupportonline.net/infopath2007/how-to-submit-items-rows-repeating-table-infopath-sharepoint-list.htm
Programmatically add items from a repeating table in InfoPath to a SharePoint list by submitting a CAML update batch to the UpdateListItems method of the Lists web service that comes with Windows SharePoint Services (WSS).
How to loop through items in a repeating table in InfoPath 2007
4 Ways to programmatically add a row to a repeating table in InfoPath
In SharePoint, create a custom SharePoint list on a SharePoint site.
In InfoPath, design an InfoPath form template as shown in figure 1.
Figure 1. The InfoPath form template in design mode.
with a Main data source that resembles the following figure:
Figure 2. The Main data source of the form template.
Here the listName field node has its Default Value set equal to the GUID (globally unique identifier) of the SharePoint list you want to add the item to. To find out what this GUID is, in SharePoint, navigate to the custom list that you created in step 1, choose Settings > List Settings, and from your browser's Address bar, copy the GUID that comes after List=.
Note: GUIDs in SharePoint 2007 look something like %7BD5E6FE0D%2D362E%2D4E63%2DB12C%2D823C3BECF477%7D. You will have to replace %7B with {, %2D with -, and %7D with }. You can remove the curly brackets when using the GUID in InfoPath, so that your final modified GUID looks something like D5E6FE0D-362E-4E63-B12C-823C3BECF477.
Create an XML file with the following contents and call it CustomListCAML.xml:
In InfoPath, click on Tools > Data Connections and add a Receive data data connection to the XML document you created in the previous step. Name the data connection CustomListCAML.
While you are still in the Data Connections dialog box, add a Submit data data connection To a web service, and click Next.
Enter the URL to the Lists web service on the top-level site or subsite where the custom list is located, and click Next. For example:
uses the Lists web service of a subsite called <Site>.
Note: Each top-level site and subsite in SharePoint has its own Lists web service, so you must ensure that you've got the right one, otherwise you'll get back an error telling you that the list cannot be found for the site you specified.
Choose UpdateListItems as the web method to use, and click Next.
Specify the listName field node of the Main data source as the value for the tns:listName parameter (accept the default Include setting of Text and child elements only), and specify the Batch group node of the CustomListCAML secondary data source as the value for the tns:updates parameter. Select XML subtree, including selected element from the Include drop-down list box for submitting the Batch group node. Click Next.
Change the name of the data connection to CustomListItemsSubmit, and click Finish. Close the Data Connections dialog box.
Double-click the Submit button to open its Properties dialog box, and click Edit Form Code to add a Clicked event handler for the Submit button. Add the following C# code to the Clicked event handler InfoPath created for you:
Or add the following code if you want to use Visual Basic code instead of C# code:
Add the following private function in C# to the form's code file:
Or add the following Sub in Visual Basic to the form's code file:
You should now have a fully functional form so that when you click the Submit button after filling out the InfoPath form, all of the items in the repeating table are added to the custom list in SharePoint.
Programmatically add items from a repeating table in InfoPath to a SharePoint list by submitting a CAML update batch to the UpdateListItems method of the Lists web service that comes with Windows SharePoint Services (WSS).
Problem
You have a Repeating Table in InfoPath that contains rows of data, which you would like to submit to a SharePoint list.Solution
Programmatically add rows of data from a Repeating Table to a SharePoint list by submitting a CAML update batch to the UpdateListItems method of the Lists web service that comes with Windows SharePoint Services (WSS).Before You Begin
You should know how to do the following:How to loop through items in a repeating table in InfoPath 2007
4 Ways to programmatically add a row to a repeating table in InfoPath
Discussion
You can accomplish this functionality as follows:In SharePoint, create a custom SharePoint list on a SharePoint site.
In InfoPath, design an InfoPath form template as shown in figure 1.
Figure 1. The InfoPath form template in design mode.
with a Main data source that resembles the following figure:
Figure 2. The Main data source of the form template.
Here the listName field node has its Default Value set equal to the GUID (globally unique identifier) of the SharePoint list you want to add the item to. To find out what this GUID is, in SharePoint, navigate to the custom list that you created in step 1, choose Settings > List Settings, and from your browser's Address bar, copy the GUID that comes after List=.
Note: GUIDs in SharePoint 2007 look something like %7BD5E6FE0D%2D362E%2D4E63%2DB12C%2D823C3BECF477%7D. You will have to replace %7B with {, %2D with -, and %7D with }. You can remove the curly brackets when using the GUID in InfoPath, so that your final modified GUID looks something like D5E6FE0D-362E-4E63-B12C-823C3BECF477.
Create an XML file with the following contents and call it CustomListCAML.xml:
<?xml version="1.0" encoding="UTF-8" ?> <Batch> <Method ID="1" Cmd="New"> <Field Name="Title" /> </Method> <Method ID="2" Cmd="New"> <Field Name="Title" /> </Method> </Batch>
In InfoPath, click on Tools > Data Connections and add a Receive data data connection to the XML document you created in the previous step. Name the data connection CustomListCAML.
While you are still in the Data Connections dialog box, add a Submit data data connection To a web service, and click Next.
Enter the URL to the Lists web service on the top-level site or subsite where the custom list is located, and click Next. For example:
http://<Server>/sites/<Site>/_vti_bin/Lists.asmx
uses the Lists web service of a subsite called <Site>.
Note: Each top-level site and subsite in SharePoint has its own Lists web service, so you must ensure that you've got the right one, otherwise you'll get back an error telling you that the list cannot be found for the site you specified.
Choose UpdateListItems as the web method to use, and click Next.
Specify the listName field node of the Main data source as the value for the tns:listName parameter (accept the default Include setting of Text and child elements only), and specify the Batch group node of the CustomListCAML secondary data source as the value for the tns:updates parameter. Select XML subtree, including selected element from the Include drop-down list box for submitting the Batch group node. Click Next.
Change the name of the data connection to CustomListItemsSubmit, and click Finish. Close the Data Connections dialog box.
Double-click the Submit button to open its Properties dialog box, and click Edit Form Code to add a Clicked event handler for the Submit button. Add the following C# code to the Clicked event handler InfoPath created for you:
// Delete all Method nodes from the CAML Batch XML XPathNavigator secDSNav = DataSources["CustomListCAML"].CreateNavigator(); XPathNodeIterator iter = secDSNav.Select("/Batch/Method"); int methodNodesCount = iter.Count; XPathNavigator firstMethodNav = secDSNav.SelectSingleNode("/Batch/Method[1]", NamespaceManager); XPathNavigator lastMethodNav = secDSNav.SelectSingleNode("/Batch/Method[" + methodNodesCount.ToString() + "]", NamespaceManager); firstMethodNav.DeleteRange(lastMethodNav); // Retrieve the rows of the repeating table XPathNavigator root = MainDataSource.CreateNavigator(); XPathNodeIterator rows = root.Select( "/my:myFields/my:rows/my:row", NamespaceManager); // Loop through the rows of the repeating table // and construct the CAML Batch XML int counter = 1; while (rows.MoveNext()) { // Retrieve the title string title = rows.Current.SelectSingleNode( "my:title", NamespaceManager).Value; // Add an item to the CAML Batch XML AddMethodNode(counter, title); // Increment the counter counter++; } // Submit the rows to the Lists web service to update the custom list DataConnections["CustomListItemsSubmit"].Execute();
Or add the following code if you want to use Visual Basic code instead of C# code:
' Delete all Method nodes from the CAML Batch XML Dim secDSNav As XPathNavigator = DataSources("CustomListCAML").CreateNavigator() Dim iter As XPathNodeIterator = secDSNav.Select("/Batch/Method") Dim methodNodesCount As Integer = iter.Count Dim firstMethodNav As XPathNavigator = _ secDSNav.SelectSingleNode("/Batch/Method[1]", _ NamespaceManager) Dim lastMethodNav As XPathNavigator = _ secDSNav.SelectSingleNode("/Batch/Method[" & _ methodNodesCount.ToString() & "]", _ NamespaceManager) firstMethodNav.DeleteRange(lastMethodNav) ' Retrieve the rows of the repeating table Dim root As XPathNavigator = MainDataSource.CreateNavigator() Dim rows As XPathNodeIterator = root.Select( _ "/my:myFields/my:rows/my:row", NamespaceManager) ' Loop through the rows of the repeating table ' and construct the CAML Batch XML Dim counter As Integer = 1 While rows.MoveNext() ' Retrieve the title Dim title As String = rows.Current.SelectSingleNode( _ "my:title", NamespaceManager).Value ' Add an item to the CAML Batch XML AddMethodNode(counter, title) ' Increment the counter counter = counter + 1 End While ' Submit the rows to the Lists web service to update the custom list DataConnections("CustomListItemsSubmit").Execute()
Add the following private function in C# to the form's code file:
private void AddMethodNode(int id, string title) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.AppendFormat("<Method ID=/"{0}/" Cmd=/"New/">", id.ToString()); sb.AppendFormat("<Field Name=/"Title/">{0}</Field>", title); sb.AppendFormat("</Method>"); XmlDocument methodXML = new XmlDocument(); methodXML.LoadXml(sb.ToString()); XPathNavigator secDSNav = DataSources["CustomListCAML"].CreateNavigator(); XPathNavigator batchNav = secDSNav.SelectSingleNode("/Batch", NamespaceManager); batchNav.AppendChild(methodXML.DocumentElement.CreateNavigator()); }
Or add the following Sub in Visual Basic to the form's code file:
Public Sub AddMethodNode(ByVal id As Integer, ByVal title As String) Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder() sb.AppendFormat("<Method ID=""{0}"" Cmd=""New"">", id.ToString()) sb.AppendFormat("<Field Name=""Title"">{0}</Field>", title) sb.AppendFormat("</Method>") Dim methodXml As XmlDocument = New XmlDocument() methodXml.LoadXml(sb.ToString()) Dim secDSNav As XPathNavigator = DataSources("CustomListCAML").CreateNavigator() Dim batchNav As XPathNavigator = secDSNav.SelectSingleNode("/Batch", NamespaceManager) batchNav.AppendChild(methodXml.DocumentElement.CreateNavigator()) End Sub
You should now have a fully functional form so that when you click the Submit button after filling out the InfoPath form, all of the items in the repeating table are added to the custom list in SharePoint.
相关文章推荐
- 转:How to submit rows of a repeating table in InfoPath to a web service? 如何将重复表中的记录传给Web Service ?
- How to delete a large number of data in SharePoint for List when refreshing data?
- How to delete a large number of data in SharePoint for List when refreshing data?
- How to remove the ribbon tabs in list forms in SharePoint 2010
- How to hide my site and my document link in the right-top corner of sharepoint 2010
- Adding Custom Actions to the List Item Menu in SharePoint 2010 Using SharePoint Designer 2010[Sharepoint中添加自定义菜单]
- How to Modify the web.config file in SharePoint using SPWebConfigModification?
- 学习:How To Use Filters in SharePoint to show items in the current Calendar Month(转)
- How to get the full error stack trace of SharePoint
- How to unlock the locked tasks in Sharepoint Workflow
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.
- How-to find the SQL that using lots of temp tablespace in Oracle
- R12: How to add Microsoft Excel as Type to the Create Template List of Values in BI Publisher (Doc ID 1343225.1)
- How to Get the Frequency Table of a Categorical Variable as a Data Frame in R
- 如何访问Azure上的SharePoint 网站(How to access the SharePoint site of Azure)
- Switch view in the InfoPath form according to the whether current user is in SharePoint group admin
- How to submit an InfoPath form to a Windows SharePoint Services document library
- sql server query to get the list of column name in a table
- [转]如何删除放置库(How to Delete the Drop Off Library)in SharePoint 2010
- How to invoke the method of managed bean and render view in JSF when we are outside the lifecycle of JSF