VISIT THE NEW JOE SHAREPOINT BLOG AT WWW.JOESHAREPOINT.COM!

Tuesday, February 26, 2008

CAML with Multi-Value Lookup Fields

When using the WSS 3.0 Web Services to access your lists, you might have a list that contains a Multi-Value Lookup Field; that is to say, a Lookup Field that has the "Allow Multiple Values" option checked. These types of fields are a bit shaky in SharePoint, although some quirks have been addressed in the recent Service Pack 1 upgrade. A regular CAML query to insert a new item would look something like this:

<Method ID="1" Cmd="New">
   <Field Name="FirstName">John</Field>
   <Field Name="LastName">Doe</Field>
   <Field Name="Email\">johndoe@johndoe.com</Field>
</Method>


So now you want a field where the user can select multiple items, and these items are populated from another list. Let's say there is a section on a form that allows a user to sign-up for multiple mailing lists. On the back-end these would be managed in their own list, "Mailing Topic", and would be the source for our lookup column. We will call the lookup column "My Subscriptions". Here is the contents of our "Mailing Topic" list:
ID  Topic Name
1    New Hires
2    Promotions
3    Leaving


We will submit the same CAML, but with the new field added and all three options included:

<Method ID="1" Cmd="New">
   <Field Name="FirstName">John</Field>
   <Field Name="LastName">Doe</Field>
   <Field Name="Email\">johndoe@johndoe.com</Field>
   <Field Name="MySubscriptions\">
      1;#New Hires;#2;#Promotions;#3;#Leaving
   </Field>
</Method>


The format is: ID#;Lookup Field Value. Separate additional values by ";#". If you look in the list with the lookup column after a successful submission, you will see all the items, but minus the #;ID's and delineated with a comma instead.

4 comments:

Anonymous said...

This is WRONG
ID#;Lookup Field Value

This is RIGHT

ID;#Lookup Field Value

Anonymous said...

I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

Sorry for offtopic

Anonymous said...

[url=http://blog.mester.com/]buy viagra soft tabs[/url]

Anonymous said...

scottish singles [url=http://loveepicentre.com/]talk to singles knoxville tn[/url] dating services comparisons http://loveepicentre.com/ black man dating white woman