Updating List via CAML: Can't create new list item if target is a lookup field? - InfoPath Dev
in

InfoPath Dev

Updating List via CAML: Can't create new list item if target is a lookup field?

Last post 11-23-2012 09:35 AM by Sfarrell. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 11-23-2012 07:54 AM

    Updating List via CAML: Can't create new list item if target is a lookup field?

    Hi All, Still pretty novice at the whole InfoPath/SharePoint thing so, please, be gentle with me. ;)

    Following numerous guides through the web I've managed to take a repeating table in an InfoPath form and have it submitted to an SharePoint list through CAML successfully. At least in part.

    My InfoPath repeating table has three columns: Project ID (Integer), Date (Date), Capacity (Integer)

    My Sharepoint list has the same columns with the addition of the default "Title" column for which I have no need (made non-visible).

    If I only use the following line in my code a new record is created as expected and passes the correct value.
    sb.AppendFormat("{0}", ProjectID)

    However if I use the following line then nothing happens. No errors, no new list item. The same occurs when including the "Date" and "Capacity" variables.
    sb.AppendFormat("{0}", ProjectID)

    "Project_Title" and "Capacity" are both lookup types while the "Date" column is, as one might assume, a date type column.

    I'm sure this is a simple thing to fix but, given my newness, I'm just not seeing it.

    Can anyone point me in the right direction?

    Let me know what additional information you might need.
    Many Thanks in advance!
    Filed under:
  • 11-23-2012 08:57 AM In reply to

    Re: Updating List via CAML: Can't create new list item if target is a lookup field?

    Hi - the two lines of code in your post look the same to me - I don't quite understand your question.

    Can you post your code full code? For lookup columns, you need to submit the list item id for the looked up item. For dates, the format can be a little finicky - at the very least, you need to be using yyyy-MM-dd format.

    As a side note - qRules can submit to SharePoint lists - and would keep you from having to write your own code.

    Hilary Stoupa

  • 11-23-2012 09:08 AM In reply to

    Re: Updating List via CAML: Can't create new list item if target is a lookup field?

    Oops, looks like I pasted the same line twice!
    That should have been:

    <sb.AppendFormat("<Field Name=''Title''>{0}</Field>", ProjectID>

    and

    <sb.AppendFormat("<Field Name=''Project_Title''>{0}</Field>", ProjectID>

    Sorry for the confusion!


    Here's the code I'm using.

    Public Sub AddMethodNode(ByVal id As Integer, ByVal ProjectID As Integer, ByVal pdate As Date, ByVal pcapacity As Integer)
    Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
    sb.AppendFormat("", id.ToString())
    sb.AppendFormat("{0}", ProjectID)
    'sb.AppendFormat("{0}", pdate)
    'sb.AppendFormat("{0}", pcapacity)
    sb.AppendFormat("")

    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

    Public Sub CTRL47_28_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
    ' Delete all Method nodes from the CAML Batch XML
    Dim ns As XmlNamespaceManager = Me.NamespaceManager
    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:Projected_Repeating_Table/my:Table", 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 ProjectID As String = rows.Current.SelectSingleNode( _
    "/my:myFields/my:Projected_Repeating_Table/my:Table/my:ProjectID", NamespaceManager).Value
    Dim pdate As String = rows.Current.SelectSingleNode( _
    "/my:myFields/my:Projected_Repeating_Table/my:Table/my:Date", NamespaceManager).Value
    Dim pCapacity As String = rows.Current.SelectSingleNode( _
    "/my:myFields/my:Projected_Repeating_Table/my:Table/my:Capacity", NamespaceManager).Value

    ' Add an item to the CAML Batch XML
    AddMethodNode(counter, ProjectID, pdate, pCapacity)

    ' Increment the counter
    counter = counter + 1
    End While

    ' Submit the rows to the Lists web service to update the custom list
    DataConnections("CustomListItemsSubmit").Execute()
    End Sub
  • 11-23-2012 09:26 AM In reply to

    Re: Updating List via CAML: Can't create new list item if target is a lookup field?

    So, you aren't getting any errors when the code runs, just if you include the date and the capacity, your items aren't created.

    Run in debug, and see what batchNav looks like, so you know what the CAML is you are going to submit. Your web service submit may be returning errors that you aren't seeing - you can pass some additional XPathNavigators to the execute method that you can then look at in debug after execution so that you can see if errors are being returned either in output or errors. Don't bother trying to create one for input, just output and errors - then your execute would look like .Execute(null, outputNav, errorsNav) (using the variables in the code sample from MSDN link).

    Hilary Stoupa

  • 11-23-2012 09:33 AM In reply to

    Re: Updating List via CAML: Can't create new list item if target is a lookup field?

    Oh and here's the xml file:

    <?xml version="1.0" encoding="UTF-8" ?>
    <Batch OnError="Continue">
    <Method ID="1" Cmd="New">
    <Field Name="Title"></Field>
    <Field Name="Project_Title"></Field>
    <Field Name="Date"></Field>
    <Field Name="Capacity_Requested"></Field>
    </Method>
    </Batch>
  • 11-23-2012 09:35 AM In reply to

    Re: Updating List via CAML: Can't create new list item if target is a lookup field?

    Sounds great Hilary. I'll give that a shot.
    One way or another I'll be back shortly!
Page 1 of 1 (6 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.