Monday, March 7, 2011

Submitting data to a SharePoint 2010 List using InfoPath - CAML

After moving around with some heavy tasks in SharePoint I wanted to Submit data to a SharePoint list through a InfoPath web form which has a Submit button in it. Did some googling and saw a way of submitting data through SharePoint in built web services. Thought of trying it and started to work on it. But after some time trust me, what i felt was this way is not hard, neither so easy.

So I thought of giving you all a step by step guide to learn how to do this without much frustration.
For this task, you will need SharePoint 2010 and Microsoft Office InfoPath Designer 2010 installed in your machine. You will need to have good understanding about CAML(Collaborative Application Markup Language), knowledge of the methods of the SharePoint Lists web service and that would be all and you are ready to go.

First of all CAML is an XML based markup language used with the family of Microsoft SharePoint technologies. Unlike plain XML, CAML contains specific groups of tags to both define and display (render) data. To get more information visit,
http://en.wikipedia.org/wiki/Collaborative_Application_Markup_Language

To get more information about the SharePoint Lists web service, in your IE navigate to "http://servername/_vti_bin/lists.asmx?WSDL" and you will be able to see all the web methods associated with the Lists web service. For our purpose we will be using UpdateListItems web method.

Now let's start. I am assuming you as a beginner, so I am adding a lot of step by step screen shots to ease your work.

1. Create a Custom List in a SharePoint Site. I have named it as Employee. Now create some columns as per your need. Note that there will be a default column called 'Title' and lets not change it and keep it that way to store EmployeeName, I have created EmployeeAddress, EmployeeNo, EmployeeEmail as columns which are Single text. Note that when Column names does not contain any spaces and later I will describe the reason to put them in that way.

2. Open Microsoft Office InfoPath Designer 2010 and select a Blank Form and click Design Form. Then do as follows.

Form Options

Security and Trust

Full Trust

Programming

Design

Design

Design

Renaming the button


Add fields






3. In SharePoint site under Employee list, in ribbon under List Tools select List and then select List Settings. In that page click Advanced Settings and select Allow management of content types to Yes.

List Settings



4.  Again go to List Settings click Metadata navigation settings and copy the list GUID.

Copy list GUID

5. Go back to InfoPath form right click on listID and paste GUID under value field.




6. Open Notepad and type,

<?xml version="1.0" encoding="UTF-8" >?
<Batch>
<Method ID="1" Cmd="New">
     <Field Name='Title' />
     <Field Name="EmployeeAddress" />
     <Field Name="EmployeeNo" />
     <Field Name="EmployeeEmail" />
</Method>
</Batch>

Save the file as an xml file and name it Submit.xml.

7. Now lets create Data Connections. Go to InfoPath form and click Manage Data Connections. First we need to create a Receive Data connection. I am naming it as 'Submit'.


Receive Data

XML Document

Browse for the created Submit.xml file


Naming the connection as 'Submit'

8. Now we need to create a Submit Data Connection. Again in Manage Data Connections click Add.

Add

Submit Data

To a Web Service

http://servername/_vti_bin/lists.asmx?WSDL

UpdateListItems

listname

listID

Include : Text and child elements only

Updates

Secondary


Batch

Include : XML Subtree, including selected element


Web Service Submit


Edit Form Code

9. Click Edit Form Code and btnSubmit_Clicked event will be created. In that block, type the following code.

try
{
    XPathNavigator root = MainDataSource.CreateNavigator();

    // Retrieve the values for the separation list item
    string listID = root.SelectSingleNode("/my:myFields/my:listID", NamespaceManager).Value;
    string eNameDS = root.SelectSingleNode("/my:myFields/my:EmployeeName", NamespaceManager).Value;
    string eAddressDS = root.SelectSingleNode("/my:myFields/my:EmployeeAddress", NamespaceManager).Value;
    string eNoDS = root.SelectSingleNode("/my:myFields/my:EmployeeNo", NamespaceManager).Value;
    string eEmailDS = root.SelectSingleNode("/my:myFields/my:EmployeeEmail", NamespaceManager).Value;

    if (listID == null)
    {
    
    }
    else
    {
        //This is CAML xml file. it contains batch and method nodes
        XPathNavigator batch = DataSources["Submit"].CreateNavigator();

        batch.SelectSingleNode("/Batch/Method/Field[@Name='Title']", NamespaceManager).SetValue(eNameDS);
        batch.SelectSingleNode("/Batch/Method/Field[@Name='EmployeeAddress']", NamespaceManager).SetValue(eAddressDS);
        batch.SelectSingleNode("/Batch/Method/Field[@Name='EmployeeNo']", NamespaceManager).SetValue(eNoDS);
        batch.SelectSingleNode("/Batch/Method/Field[@Name='EmployeeEmail']", NamespaceManager).SetValue(eEmailDS);

        DataConnections["Web Service Submit"].Execute();
}
catch (Exception ex)
{
    ex.Message.ToString();
}

10. And finally thats all. Preview the form and try submitting the data. After that publish the form and upload the form template into SharePoint and start using.

There are few important things to remember carefully. 
  • There is a significant difference between Display name and the Field name. When we are doing the coding we are using the value under the Field name. If you can remember, I have mentioned to keep the default 'Title' column as it is, because if we rename the 'Title' column it will just change the display name. The Field name will be remain as 'Title'.
  • No of updating list fields should be the same as no xml field names.
  • When I am creating columns in SharePoint list, if you can remember I have included no spaces between words. Thats because it will add characters into Field names. So when we are referring to Field names from looking at Display names, the Field name is actually differs from the Display name.
  • List fieldname should be same as xml fieldname.
  • Make sure when creating the Submit Data connection 'Web Service Submit' to select following things,
    • listName - listID : Include : Text and Child elements only
    • updates - Batch : Include : XML subtree, including selected element

Appreciate your feedback. 

Happy Coding.

Regards,
Jaliya