Extreme newbie question--adapting an MSDN example to add items to a SharePoint list - InfoPath Dev
in

InfoPath Dev

Extreme newbie question--adapting an MSDN example to add items to a SharePoint list

Last post 07-20-2011 10:44 AM by Hilary Stoupa. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 07-19-2011 01:17 PM

    Extreme newbie question--adapting an MSDN example to add items to a SharePoint list

    Hi guys!  First post ever here on InfoPathDev.com :)  I am an "intermediate" C++/C# developer, but this is my first time ever doing anything web-related.  So I have a bunch of questions.

    My end goal is to create an InfoPath form that will let users add records to a SharePoint list.  There will be five or six fields.  Some fields will be single lines of text, others will be lookup drop-downs that refer to elements in other SharePoint lists, and I'll also have a date picker.

    I have successfully followed this MSDN InfoPath tutorial, which shows how to implement Submit functionality using CAML XML and C# code.  It works great.  But I have a ton of questions... :-P

    For example, let's say I have the following fields:

    • FirstName
    • LastName
    • Phone
    • Email
    • BranchLocation

    Let's say that last field, BranchLocation, is a dropdown that connects to another SharePoint list that I have called Branches.  I have no problem setting up that lookup, so when I click on the dropdown, I see a list of the five or six entries that I have in the Branches list.

    The problem is, the plain-vanilla C# code in the example doesn't seem to work with lookup tables.

    Here's the existing code, with another small section added to handle the BranchLocation:

     // See if any new contacts have been added.
       if (xiNewContacts.Count > 0)
       {
          while (xiNewContacts.MoveNext())
          {
             // Set the values in the Add List Item Template 
             // XML file using the values in the new row.
             xnCAML.SelectSingleNode(
                "/Batch/Method/Field[@Name='Title']", ns)
                .SetValue(xiNewContacts.Current.SelectSingleNode(
                "my:Title", ns).Value);
             xnCAML.SelectSingleNode(
                "/Batch/Method/Field[@Name='FirstName']", ns)
                .SetValue(xiNewContacts.Current.SelectSingleNode(
                "my:FirstName", ns).Value);
             xnCAML.SelectSingleNode(
                "/Batch/Method/Field[@Name='LastName']", ns)
                .SetValue(xiNewContacts.Current.SelectSingleNode(
                "my:LastName", ns).Value);
             xnCAML.SelectSingleNode(
                "/Batch/Method/Field[@Name='Phone']", ns)
                .SetValue(xiNewContacts.Current.SelectSingleNode(
                "my:Phone", ns).Value);
             xnCAML.SelectSingleNode(
                "/Batch/Method/Field[@Name='Email']", ns)
                .SetValue(xiNewContacts.Current.SelectSingleNode(
                "my:Email", ns).Value);
    
              // NEW CODE!!!!
      // NEW CODE!!!!   // NEW CODE!!!! xnCAML.SelectSingleNode( "/Batch/Method/Field[@Name='BranchLocation']", ns) .SetValue(xiNewContacts.Current.SelectSingleNode( "my:BranchLocation", ns).Value);
              // NEW CODE!!!!
      // NEW CODE!!!!   // NEW CODE!!!! // Set the values of the Changed and Delete columns to // "FALSE". xnCAML.SelectSingleNode( "/Batch/Method/Field[@Name='Changed']", ns) .SetValue("FALSE"); xnCAML.SelectSingleNode( "/Batch/Method/Field[@Name='Delete']", ns) .SetValue("FALSE"); // Set the value of Cmd attribute to "New". xnCAML.SelectSingleNode("/Batch/Method/@Cmd", ns) .SetValue("New"); // Submit the new row. wsSubmit.Execute(); } }

     

    When I try to run this, nothing happens.  When I look at the contents of xnCAML, I can see that the <BranchLocation> tag in the CAML is set to the plain text value of the selected item in the BranchLocation drop down.  Should that be an item index instead of a string?

     

    Thanks!
    --Peter

  • 07-20-2011 07:17 AM In reply to

    Re: Extreme newbie question--adapting an MSDN example to add items to a SharePoint list

    http://www.infopathdev.com/forums/p/5495/20809.aspx#20809

    I found an interesting comment by Matt Faus:  "You can't submit Lookup types to SharePoint."

    Can someone confirm whether this is still true with InfoPath 2007?  If so, how can I work around this problem?  Our system absolutely needs lookup functionality!

    Thanks!

  • 07-20-2011 09:39 AM In reply to

    Re: Extreme newbie question--adapting an MSDN example to add items to a SharePoint list

    I think you can - it is just a matter of syntax.

    You need to submit the data in the format that the lookup expects - SharePoint list item ID, semicolon, pound symbol, and display value (although it may be possible to just submit the ID, you should experiment with this). For example, 1;#mydisplay

    I'm attaching a screenshot from an owssvr.dll URL on a list with a lookup column.


    Hilary Stoupa

  • 07-20-2011 10:05 AM In reply to

    Re: Extreme newbie question--adapting an MSDN example to add items to a SharePoint list

    Hilary, That's very good to hear!

    I did some experimenting based on your instructions, and found something interesting.  When I do a hard-coded test, the index and ;# alone seems to work. For example, if I have a lookup column in my SharePoint list that's called OfficeLocation, I can add a new entry to that list simply by including something like "<Field Name="OfficeLocation">2;#</Field>" in my outgoing CAML.

    Now for my second question:

    If the user picks a lookup item from a drop-down on my InfoPath form, how do I figure out the item index for their selection?

    Thanks!

  • 07-20-2011 10:29 AM In reply to

    Re: Extreme newbie question--adapting an MSDN example to add items to a SharePoint list

    Well, the drop down is using a ShP list for its values, right? So you could use the ShP ID for the value and whatever column you want for the display. Then you have the ID number in your main data source.

    Hilary Stoupa

  • 07-20-2011 10:36 AM In reply to

    Re: Extreme newbie question--adapting an MSDN example to add items to a SharePoint list

    Aaah, I see now.  I originally had set the drop-down properties to use the "Title" for both the Value and the Display Name.  I corrected it so now the Value is the ID and the Display Name is the title.

    Works like a charm!  THANK YOU!!!

  • 07-20-2011 10:44 AM In reply to

    Re: Extreme newbie question--adapting an MSDN example to add items to a SharePoint list

    You are welcome, glad you got it squared away!

    Hilary Stoupa

Page 1 of 1 (7 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.