SOLUTION: Populate Infopath form data to a Sharepoint List (i.e. Task list)

J

jupiter41

After days and days of search, test, of suffering and crying, of tears,
of great moment of hope and despair, I finally managed to find the
solution to populate data from an Infopath form to a Sharepoint list
(task list, for instance).

First of all: the example you find under /_vti_bin/Lists.asmx is really
really bad made. It's absolutly unclear. You don't need to specify an
XSL schema, and the XML file asked is the same "Batch file" you create
when you use UpdateListsItems using VB.NET. The lack of documentation
about this is really a SHAME!! Now, lady and gentlemen, there's the
final code:

function CTRL54_5::OnClick(eventObj)
{
var objField1;
objField1=XDocument.DOM.selectSingleNode("//my:myFields/my:Objet").text;
XDocument.UI.Alert(objField1);

var liste =
XDocument.DOM.selectNodes("//my:myFields/my:group15/my:Activities");

//The XML doc contains the same batch type as the VB.NET way of using
UpdateListItems
var xmlDoc = "";

//The soap request is adapted from the example in /_vti_bin/Lists.asmx
var soapRequest = "";

//Using a xmlHttpRequest allow to don't bother about the precise
formatting of the request, letting Infopath care about that.
var xmlHttpRequest;

//Here's our batch xml file. Very simple. Just add "Field" parameter
to fill more fields (here's just an example)
xmlDoc += "<Batch>";
xmlDoc += "<Method ID='1' Cmd='New'>";
xmlDoc += "<Field Name='Title'>Test</Field>";
xmlDoc += "</Method>";
xmlDoc += "</Batch>";

//Prepare SOAP request. Notice that no schema is specified. The
headers will come in the xmlHttpRequest object.
soapRequest += "<?xml version='1.0' encoding='utf-8'?>";
soapRequest += "<soap:Envelope
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
soapRequest += " <soap:Body>";
soapRequest += " <UpdateListItems
xmlns='http://schemas.microsoft.com/sharepoint/soap/'>";
soapRequest += " <listName>test</listName>"; //Change here the
name of the list to update
soapRequest += " <updates>" + xmlDoc + "</updates>"; //Just
specify your Batch xml variable created before
soapRequest += " </UpdateListItems>";
soapRequest += " </soap:Body>";
soapRequest += "</soap:Envelope>";


//objXMLHttp. Just a nice try&catch to deal with eventual problems.
try
{
var xmlHttpRequest = new ActiveXObject("MSXML2.XMLHTTP.5.0");
}
catch(ex)
{
XDocument.UI.Alert("Could not create MSXML2.XMLHTTP.5.0 object.\r\n"
+ ex.number + " - " + ex.description);
return false;
}


//Send datas to Sharepoint. Here we're specifying headers.
Content-Length is not mandatory.
xmlHttpRequest.open("POST",
"http://yoursharepointserver/_vti_bin/Lists.asmx", false); //Just
specify here the complete address of the list service
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml;
charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length",1);
xmlHttpRequest.setRequestHeader("SOAPAction",
"http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
xmlHttpRequest.send(soapRequest);

}
 
M

Miguel

Hello Jupiter,

After studying your code for a couple of hours I gave it a try with
vbscript, I'm a vb guy! Unfortunately, it just does not work!

This is the error I get:
Internal Server Error
Server was unable to read request. --> This is an unexpected token. The
expected token is 'EndElement'. Line 1, position 456.

After numerous attemps, there seem's to have an error around "charset=utf-8"
also.

Here's my code. The list I want to update, is based on the SharePoint
Contacts list template. My list is called "Clients". I want to add a new item
to my list.

Sub AddListItem()
Dim sChamp1, sXML, sSoapRequest
Dim xmlHttpRequest

sChamp1 = ""
sXML = "<Batch>" & _
"<Method ID=""1"" Cmd=""New"">" & _
"<Field Name=""Last_x0020_Name"">Brunet</Field>" & _
"</Method>" & _
"</Batch>"

sSoapRequest = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<soap:Envelope
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""" & _
" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""" & _
" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & _
" <soap:Body>" & _
" <UpdateListItems
xmlns=""http://schemas.microsoft.com/sharepoint/soap/"">" & _
" <listName>Clients</listName>" & _
" <updates>" & sXML & "</updates>" & _
" </UpdateListItems>" & _
" </soap:Body>"
'XDocument.UI.Alert("Message = " & sSoapRequest)
'Stop
On Error Resume Next
Set xmlHttpRequest = CreateObject("MSXML2.XMLHTTP.5.0")
xmlHttpRequest.open "POST",
"http://sps.k2mega.local/sites/sales/InfoPathSPS/_vti_bin/lists.asmx", False
xmlHttpRequest.setRequestHeader "Content-Type", "text/xml"
xmlHttpRequest.setRequestHeader "charset", "utf-8"
xmlHttpRequest.setRequestHeader "Content-Length", 1
xmlHttpRequest.setRequestHeader "SOAPAction",
"http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"
'Stop
xmlHttpRequest.send sSoapRequest

If xmlHttpRequest.status <> 0 Then
XDocument.UI.Alert("Erreur = " & Cstr(xmlHttpRequest.status) & vbcrlf & _
"Description = " & xmlHttpRequest.statusText & vbcrlf & _
"Texte = " & xmlHttpRequest.responseText)
End if
End Sub

Hope you can help me or point me in the right direction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top