3

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

2

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)
  • 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
1

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().

  • 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
0

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.

0

Here is how I implemented mine from JavaScript. Please change the variables to your own local variables. Only showing here the logical flow.

function submitNewComponent() {
    // Get values from the form 
    var ComponentNumber = $('#ComponentNumber').val();
    var ComponentName = $('#ComponentName').val();

    // Create a lookup field object for the project number.
    var projectNumberLookup = new SP.FieldLookupValue();
    var projLookupId = $('#projectNumber').val();
    projectNumberLookup.set_lookupId(projLookupId);

    //Create a lookup filed object for the drawing number.
    var drawingNumberLookup = new SP.FieldLookupValue();
    var drawLookupId = $('#drawingNumber').val();
    drawingNumberLookup.set_lookupId(drawLookupId);

    // Creaate a new list item information object.
    var itemCreateInfo = new SP.ListItemCreationInformation();

    // Create an object to set the fields.
    var newListItem = componentList.addItem(itemCreateInfo);

    // Set the fields 
    newListItem.set_item('ProjectNumberLookup', projectNumberLookup);
    newListItem.set_item('DrawingNumberLookup', drawingNumberLookup);
    newListItem.set_item('ComponentNumber', ComponentNumber);
    newListItem.set_item('ComponentName', ComponentName);

    // Add the new item.
    newListItem.update();

    // Call the async query
    context.load(newListItem);
    context.executeQueryAsync(addSuccess, addFail);

    function addSuccess() {
        alert('Item created: ' + newListItem.get_id());

        // Redirect to the Lists components app web.
        window.location = 'https://{hostname_placeholder}/Lists/Components/Allitems.aspx';
    }

    function addFail(sender, args) {
        alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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