none
Why can't I set multi-choice values in SharePoint list using WCF REST interface?

    Dotaz

  • I'm having very specific trouble using the SharePoint 2010 REST interfaces with a multi-choice column (e.g. a set of checkboxes). Single choice columns work fine (e.g. radio buttons or dropdown, and even yes/no columns all work ok.

    - I have a list 'Flight' on SharePoint with a multi-choice column 'Equipment'

    - in C#, .NET 4.0, I add a Service Reference to the site's RESTful service

    - in the Object Browser in VC# Express, I see the Flight object

    - there is also a FlightEquipmentValue type and a DataServiceCollection<FlightEquipmentValue> Equipment property to set.

    I'm able to instantiate a new context for the service, create a new item, set its other properties. Then I can call FlightEquipmentValue.CreateFlightEquipmentValue() to create a new 'holder' for a checkbox value and add one of my choices to the list of equipment (e.g. "Beverage Cart").

    When I inspect the Flight object in the debugger after adding the equipment, everything looks great. When I save the object with SaveChanges(), the new list item is created in SharePoint.

    Except that the Equipment field is blank, nothing is ever set.

    After trying everything I could think of to set the value (e.g. AttachLink()), .... I sniffed the data going across the wire to the SharePoint service with Fiddler.

    The rest of the Flight object is "serialized" there but not the Equipment field. Nothing at all.

    Help needed, thanks.

           static void Main(string[] args)
            {
                Uri uri = new Uri(args[0]);
                Console.WriteLine("Forwarding: to " + uri);
                HomeDataContext context =
                    new HomeDataContext(uri);
                context.Credentials = new System.Net.NetworkCredential(args[1], args[2]);
                FlightsItem flight = new FlightsItem();
                flight.FlightNumber = "79";
                flight.Pilot = "Bill";
                flight.Copilot = "Susan";
                flight.Aircraft = FlightsAircraftValue.CreateFlightsAircraftValue("Boeing 747");
                flight.AircraftValue = "Boing 737";
                FlightsEquipmentValue equip = FlightsEquipmentValue.CreateFlightsEquipmentValue("Beverage Cart");
                flight.Equipment.Add(equip);
                context.AddToFlights(flight);
                context.SaveChanges();
            }

    Here's what went over the wire:

        <?xml version="1.0" encoding="utf-8" standalone="yes"?>
        <entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
           xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
           xmlns="http://www.w3.org/2005/Atom">
          <category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" 
           term="Microsoft.SharePoint.DataService.FlightsItem" />
          <title type="text">79</title>
          <updated>0001-01-01T00:00:00Z</updated>
          <author>
            <name />
          </author>
          <id />
          <content type="application/xml">
            <m:properties>
              <d:AircraftValue m:null="true" />
              <d:ContentType m:null="true" />
              <d:ContentTypeID m:null="true" />
              <d:Copilot>Susan</d:Copilot>
              <d:Created m:type="Edm.DateTime" m:null="true" />
              <d:CreatedById m:type="Edm.Int32" m:null="true" />
              <d:FlightNumber>79</d:FlightNumber>
              <d:Id m:type="Edm.Int32">0</d:Id>
              <d:Modified m:type="Edm.DateTime" m:null="true" />
              <d:ModifiedById m:type="Edm.Int32" m:null="true" />
              <d:Owshiddenversion m:type="Edm.Int32" m:null="true" />
              <d:Path m:null="true" />
              <d:Pilot>Bill</d:Pilot>
              <d:Version m:null="true" />
            </m:properties>
           </content>
         </entry>

    14. června 2012 8:43

Všechny reakce

  • Hi Per,

    Thanks for your post!

    I am trying to involve some one familiar with this topic to do some further research.

    Thanks,
    Simon


    Simon Huang

    TechNet Community Support

    18. června 2012 8:11
  • I’m having the same problem. The DataServiceCollection in the Object Model is populated but the REST-Post doesn’t contain any items.


    Sharepoint Hosting und Beratung www.sharepoint-foundation-hosting.ch

    19. června 2012 15:12
  • Simon,

    I found a solution but I'd still be curious to hear from an expert.

    Benjamin,

    This works... 

    // The new objects
    FlightsEquipmentValue equip = FlightsEquipmentValue.CreateFlightsEquipmentValue("Beverage Cart");
              
    // Make the context start tracking these objects. Note the special name!!!
    context.AttachTo("FlightsEquipment", equip);
              
    // Track the parent
    context.AddToFlights(flight);
              
    // Then add to the choices
    flight.Equipment.Add(equip);
              
    // Now make sure that links are added. Without this, they aren't added!
    context.AddLink(flight, "Equipment", equip);
    context.SaveChanges();

    I'm not sure I can explain the 'why' of it but the two important points: the order above and the "FlightsEquipment" label the 'equip' object gets attached with.

    I'd been thinking that I needed to attach 'equip' to the entity 'Equipment' where it gets used but that's not the case.

    Hope that helps.

    Before you get too far along the question I faced after fixing this one: I still can't find a way to set a fill-in value on multi-choice fields.

    Since the new fill-in value doesn't exist in the list, and the Value type for the multi-choice field can't be expanded programmatically, I can't get it to accept any new values. Any ideas?

    Regards, Per

    20. června 2012 3:29
  • Hello Per,

    Can you post the code you are using to attempt a fill-in value, and the results when you attempt to run it (e.g. field is set to blank value or you get an exception)?

    Thank you.


    David Wilborn MSFT

    2. července 2012 15:28