Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a list MyUsers that has a lookup field deptRef that references the Title in my departments list. Fine and is easy for my users to update the MyUsers list and select the relevant department.

Now, I want to do this via JavaScript and CSOM

   var itemCreateInfo = new SP.ListItemCreationInformation();

   // test department 
   var deptName = 'Professional Services' ;  // an existing title field in Departments

   var userItem = UserList.addItem(itemCreateInfo);
   userItem.set_item('Title', 'Daniel');

   // create our lookup field
   var deptItemLookupField = new SP.FieldLookupValue();
   deptItemLookupField.set_lookupId(deptName);

   userItem.set_item('DeptRefTitle', deptItemLookupField);
   userupdate();

   context.executeQueryAsync(function () {
      alert('I did it!');

   }, onFailedCallback) 

Unfortunately the I can't simply use the string title from the departments list as

The request failed: Message: Input string was not in a correct format.

Any ideas?

Thanks

Daniel

share|improve this question

3 Answers 3

The following code should work:

   var itemCreateInfo = new SP.ListItemCreationInformation();

   // test department     
   //var deptName = 'Professional Services' ;  // an existing title field in Departments

   var deptID = 1; //Where 1 is the Item ID of the ListItem with Title "Professional Services" 

   var userItem = UserList.addItem(itemCreateInfo);
   userItem.set_item('Title', 'Daniel');

   // create our lookup field 
   var deptItemLookupField = new SP.FieldLookupValue();
   deptItemLookupField.set_lookupId(deptID);

   userItem.set_item('DeptRefTitle', deptItemLookupField);
   userItem.update();

   context.executeQueryAsync(function () {
      alert('I did it!');

   }, onFailedCallback)
share|improve this answer
    
Hi I have changed DepRefTitle to just DeptRef and as suggested by many on here I have lookup the ID of my departments. I have commended out the DeptTitle in the view in the schema of my list def xml - this I notice cause an exception if ever I change the view. Oh well the CAML and csom appears to work so I can pick out the other fields as a result of the lookup. Thanks to all who helped –  westerdaled Mar 22 '13 at 14:26

The easiest way to do this is to simply write:

userItem.set_item('DeptRefTitle', deptId);

...where deptId the numeric ID of the parent department list item.

Your approach will also work, but it has a bug. You are using set_lookupId() to set the parent item's title. To do this you'd need to use set_lookupValue().

share|improve this answer
    
Hi thanks.. Unfortunately I am getting .set_lookupValue is not a function. –  westerdaled Mar 19 '13 at 15:50
    
Hi thanks.. Unfortunately I am getting .set_lookupValue is not a function. I am trying deptItemLookupField = new SP.FieldLookupValue(); lookFieldValue = deptItemLookupField.get_lookupValue(deptName). Unfortunately I create my item but without the department name –  westerdaled Mar 19 '13 at 15:57
    
ah. Yes, it looks like you need to use the numeric ID when setting a lookup, because SharePoint does not see titles as unique. –  Derek Gusoff Mar 19 '13 at 16:04
    
Thanks for your help. Hmmmm this a depressing as the title is way more intuitive to me, let alone my users. Oh well I will refactor and strive for future success.... –  westerdaled Mar 19 '13 at 16:16

It seems to me that you should be creating the lookup value on the list you are looking up and then setting the id of the lookup on your current list should automatically bring over the title.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.