SP2010 – Updating multiple list items

Jun 18, 2014 at 1:58 PM
I have a list that we enter information into (list1). I have a second list that we also enter information into (list2). Both lists share a field name (number). Once the (date) field is populated on (list1), I would like for code to update ALL ITEMS in list2 that have the same (number) as the current item being edited in (list1).

It looks like the SPUpdateMultipleListItems would do the trick, but I am no coder, and cannot figure out how to get the list references down.

Both lists are in the same site.

I was thinking about adding this functionality to an ASP button or something along those lines. It would be nice to integrate it directly into the SAVE button on the form itself.

Any help would be greatly appreciated!!!

(ps: hope this is the right spot Marc!)
Coordinator
Jun 18, 2014 at 2:09 PM
John:

SPUpdateMultipleListItems isn't going to do it for you because it's built to work on only one list. However, you could clone that function and make it work with a little recoding. Since you are "no coder", that may be tough for you, though.

You can certainly call the code on form submit. There's a stub function in SharePoint forms called PreSaveAction that does nothing by default. If you create your own PreSaveAction function, it will fire before the form saves. You could grab the value you want to update and have it happen there.

M.
Jun 18, 2014 at 2:11 PM
I am currently using that PreSaveAction to throw up a confirmation popup on another menu. Would there be a better suite function to do what I am looking to do?
Coordinator
Jun 18, 2014 at 2:13 PM
Edited Jun 18, 2014 at 2:13 PM
No, there's no existing function that will do exactly what you want. But first and foremost, SPServices is a toolkit. All SPUpdateMultipleListItems does is call GetListItems to find the items to update and UpdateListItems to update them. You're looking to do something pretty similar.

M.
Jun 18, 2014 at 2:23 PM
So can UpdateListItems update ANOTHER list other than the one it's in currently?

If so, then I could use GetListItems to find the items that have the ID I am looking, but the date field is blank and use UpdateListItems to populate, is that correct? (all assuming UpdateListItems can see other lists.)
Coordinator
Jun 18, 2014 at 2:31 PM
Actually, if you only want to update list2, then you can use SPUpdateMultipleListItems. The key is to pass in the right values.

It'll look something like this:
$().SPServices.SPUpdateMultipleListItems({
  listName: "list 2",
  CAMLQuery: "<Query><Where><Eq><FieldRef Name='numberFieldName'/><Value Type='Integer'>" + variableWithTheNumber + "</Value></Eq></Where></Query>",
  batchCmd: "Update",
  valuepairs: [["dateFieldName", dateFieldValue]]
});
M.
Jun 18, 2014 at 2:33 PM
Ok, great. I will see if I can get this implemented and get back with you. The only change I would need would be the CAML I guess. Not only do I need to find based upon the (number) field, but I would like for it to only find and update the ones with the (date) field that are blank.

Thanks Marc!
Jun 18, 2014 at 2:40 PM
So, does this seem about right for a presave function? I changed the field names as well to what I suspect they should be. Sorry to be a pain.
<script src="https://jqueryjs.googlecode.com/files/jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript">

function PreSaveAction() {

$().SPServices.SPUpdateMultipleListItems({
  listName: "list 2",
  CAMLQuery: "<Query><Where><Eq><FieldRef Name='numberlist2'/><Value Type='Integer'>" + numberlist1 + "</Value></Eq></Where></Query>",
  batchCmd: "Update",
  valuepairs: [["datelist2", datelist1]]
});

} </script> 
Coordinator
Jun 18, 2014 at 2:55 PM
It looks basically right. You are pointing at an ancient version of jQuery. You should use the latest, which is something like 1.11.x. Also, you're not referencing SPServices, which you can load from the CDN at cdnjs.com.

Of course, you need to set the values for numberlist1 and datelist1 before you call SPUpdateMultipleListItems.

M.
Jun 18, 2014 at 3:02 PM
Yes, I forgot to put the SPServices line in there, LoL... we have that hosted... thanks for pointing that out though...

When you say I need to set the values first, do you mean that if the fields are blank on the form, it would error out? Because this form will be edited numerous times before the date field will be populated on list1, so this code would run each time they save, but it would be a blank field. Is that an issue?
Coordinator
Jun 18, 2014 at 3:19 PM
I meant that you aren't setting the value for those variables, so they will be undefined.

M.
Jun 18, 2014 at 3:31 PM
So add something like this:

var numberlist1 = list2.get_item('number')
var datelist1 = list2.get_item('date')
Jun 18, 2014 at 3:38 PM
Edited Jun 18, 2014 at 3:43 PM
or would it be better to do this:
var numberlist1 = document.getElementById('ctl00_m_g_7d009b38_8224_430d_990e_5bf3900af57f_ctl00_CustomFieldIterator1_ctl00_ctl00_ctl00_ctl04_ctl00_Txtbx').parentElement
Obviously the ElementID would be different.
Coordinator
Jun 18, 2014 at 4:26 PM
It's something like the latter, as you're grabbing the value from the DOM, not an item that exists in the list.

M.
Jun 18, 2014 at 4:57 PM
Ok thanks Marc. I'm going to take a stab at this. I appreciate your patience.
Jun 18, 2014 at 5:38 PM
Edited Jun 18, 2014 at 5:45 PM
Ok, this is what I have, let me know what you think. I wasn't sure if I used the internal names on the correct fields or not:
<script language="javascript" type="text/javascript" src=" /sites/hp/JS/jquery-min.js"></script>
<script language="javascript" type="text/javascript" src=" /sites/hp/JS/jquery.SPServices-2014.01.min.js"></script>
<script language="javascript" type="text/javascript">


function PreSaveAction() {

var PCRNumberMain = document.getElementById('ctl00_m_g_7be96a6f_b09b_4ca7_acbf_624572c9b345_ctl00_CustomFieldIterator1_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField').parentElement;
var ReleaseDateMain = document.getElementById('ctl00_m_g_7be96a6f_b09b_4ca7_acbf_624572c9b345_ctl00_CustomFieldIterator1_ctl20_ctl00_ctl00_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDate').parentElement;

$().SPServices.SPUpdateMultipleListItems({
  listName: "{11D7C2F8-8BCD-4EB8-A85C-10978D39BFED}",
  CAMLQuery: "<Query><Where><Eq><FieldRef Name='PCR_x0020_Number'/><Value Type='Integer'>" + PCRNumberMain + "</Value></Eq></Where></Query>",
  batchCmd: "Update",
  valuepairs: [["Release Date", ReleaseDateMain]],
  debug: true
});

} </script> 
Coordinator
Jun 18, 2014 at 5:47 PM
Is it working?

I'd avoid using the big, long ,hairy selectors. Those ids will change from one environment to another.

Maybe:
var PCRNumberMain = $("input[title='PCRNumberMain']");
You'll need to inspect the DOM for the right values, of course. I'm pretty sure you're already doing that since you have the ids.

M.
Jun 18, 2014 at 7:32 PM
Ok, I will give that a shot. I haven't had a chance to implement as of yet. Got tied up in some meetings. Thanks again. I will let you know tomorrow how it goes.
Jun 18, 2014 at 8:00 PM
Ok, so this is what I have currently. When I hit save, it does nothing. Doesn't even save the document. I have entered this into a CEWP. Is that an incorrect spot for a PRESAVE action maybe?

Also, I wanted to clarify the field names I have below:

The 2 titles for the variables are the INTERNAL names.
The fieldref name NewColumn1 is the internal name.
The Release Date on the valuepairs is the actual title.

Am I using the right fields? Either way, wouldn't the DEBUG kick in? I am getting nothing at all. The jquery-min is 1.11 version, not the old one I was referencing in the prior code.
<![CDATA[<script language="javascript" type="text/javascript" src=" /sites/hp/JS/jquery-min.js"></script>
<script language="javascript" type="text/javascript" src=" /sites/hp/JS/jquery.SPServices-2014.01.min.js"></script>
<script language="javascript" type="text/javascript">


function PreSaveAction() {

var PCRNumberMain = $("input[title='Change_x0020_Request_x0020_Numbe']");

var ReleaseDateMain = $("input[title='Actual_x0020_Release_x0020_Date']");

$().SPServices.SPUpdateMultipleListItems({
  listName: "{11D7C2F8-8BCD-4EB8-A85C-10978D39BFED}",
  CAMLQuery: "<Query><Where><Eq><FieldRef Name='NewColumn1'/><Value Type='Text'>" + PCRNumberMain + "</Value></Eq></Where></Query>",
  batchCmd: "Update",
  valuepairs: [["Release Date", ReleaseDateMain]],
  debug: true
});

} </script> 
]]>
Jun 19, 2014 at 2:06 PM
Edited Jun 19, 2014 at 2:22 PM
Ok, so I changed the single quotes to double quotes on the var title fields, and now when I click save it actually saves. Nothing is being updated though.... if I had a column title incorrect somewhere, wouldn't the debug alert me?

Ok, just wanted to update on most recent steps. Changed field names from internal to titles and still no good... swapped around different fields with internal and single to double quotes and back and forth just trying different combinations. Still no good. I checked the items that are SUPPOSE to be updated, and they still haven't been touched... meaning the last modified is still yesterday's date.... so it's not hitting it at all for some reason....

This is the most recent code that actually allows me to save the form:
<script language="javascript" type="text/javascript" src=" /sites/hp/JS/jquery-min.js"></script>
<script language="javascript" type="text/javascript" src=" /sites/hp/JS/jquery.SPServices-2014.01.min.js"></script>
<script language="javascript" type="text/javascript">


function PreSaveAction() {

var PCRNumberMain = $("input[title="PCR Number"]");

var ReleaseDateMain = $("input[title="Actual Release Date"]");

$().SPServices.SPUpdateMultipleListItems({
  listName: "{11D7C2F8-8BCD-4EB8-A85C-10978D39BFED}",
  CAMLQuery: "<Query><Where><Eq><FieldRef Name='NewColumn1'/><Value Type='Text'>" + PCRNumberMain + "</Value></Eq></Where></Query>",
  batchCmd: "Update",
  valuepairs: [["Release Date", ReleaseDateMain]],
  debug: true
});

}
 </script> 
Coordinator
Jun 19, 2014 at 2:21 PM
You're really going to have to do some debugging here. You're writing code, so you need to step through it and see what's happening.

debug: true isn't going to do much for you in this case. Even if the variables aren't set, they will end up undefined, which will pass a value to the update. You should look at the network traffic in Firebug or Fiddler to see if the calls are in fact working.

M.
Jun 19, 2014 at 2:57 PM
Not sure if this helps, but it appears that the vars aren't getting populated??
<FieldRef Name="NewColumn1"></FieldRef>
<Value Type="Text">[object Object]</Value>
Maybe I'm not looking in the right place on Firebug, never used it before.
Coordinator
Jun 19, 2014 at 3:09 PM
It's populated, but your selector is returning a JavaScript object.

It should be:
var PCRNumberMain = $("input[title="PCR Number"]").val();
M.
Jun 19, 2014 at 4:29 PM
Now it shows this error:

SyntaxError: missing ) after argument list
var PCRNumberMain = $("input[title="PCR Number"]").val();

Has an error pointing to PCR Number under the error.
Coordinator
Jun 19, 2014 at 4:44 PM
Sorry; my line above wasn't right. I can help up to a point, but ultimately, you need to debug this.
var PCRNumberMain = $("input[title='PCR Number']").val();
M.
Jun 19, 2014 at 5:03 PM
Ok, thanks.
Jun 19, 2014 at 5:58 PM
Ok, just wanted to post the final code that worked for me:
<script language="javascript" type="text/javascript" src=" /sites/hp/JS/jquery-min.js"></script>
<script language="javascript" type="text/javascript" src=" /sites/hp/JS/jquery.SPServices-2014.01.min.js"></script>
<script language="javascript" type="text/javascript">


function PreSaveAction() {

var PCRNumberMain = $("input[title='PCR Number']").val();

var ReleaseDateMain = $("input[title='Actual Release Date']").val();

$().SPServices.SPUpdateMultipleListItems({
  listName: "{11D7C2F8-8BCD-4EB8-A85C-10978D39BFED}",
  CAMLQuery: "<Query><Where><Eq><FieldRef Name='NewColumn1'/><Value Type='Text'>" + PCRNumberMain + "</Value></Eq></Where></Query>",
  batchCmd: "Update",
  valuepairs: [['Release_x0020_Date', ReleaseDateMain]],
});
return true;
} </script> 
Thanks for all the help Marc. Now I just have to figure out the CAML part to tell it to ONLY update where the Release Date is Blank.
Coordinator
Jun 19, 2014 at 6:14 PM
You're welcome. That CAML will be something like:
CAMLQuery: "<Query><Where><And><Eq><FieldRef Name='NewColumn1'/><Value Type='Text'>" + PCRNumberMain + "</Value></Eq>><IsNull><FieldRef Name='ReleaseDate'/></IsNull></And></Where></Query>",
M.
Jun 19, 2014 at 6:40 PM
Worked like a charm. Thanks again Marc!
Jun 19, 2014 at 6:41 PM
Oh, I DID have to change the RELEASE DATE field on the destination list to a SINGLE LINE OF TEXT field... it wasn't liking the date format... something about CAML I believe from the research I did... do you know of any way around this?
Coordinator
Jun 19, 2014 at 6:51 PM
You should be able to filter on a Date/Time column.

M.
Jun 19, 2014 at 8:13 PM
No, the filter worked fine... but when it tried to UPDATE the list item, it returned an error about the DATE/TIME column not being correct format or something... no big deal... just thought maybe there was a quick fix for something like that. I just made it a text field, and will put validation on it for manual entry.

Thanks again Marc.
Jun 19, 2014 at 11:45 PM
Date fields need to be updated with a date in ISO format. Example: 2014-06-19.
I think Marc has a utility that does some converting for you. Read the docs. to find it.


--
Paul T.

-- Sent from Mobile

Coordinator
Jun 20, 2014 at 1:12 PM
Yup, the function is SPConvertDateToISO.

M.
Jun 20, 2014 at 1:25 PM
Awesome, worked great. Changed one line, and added another, see below.
var ReleaseDateMainNotFormatted = $("input[title='Actual Release Date']").val();

var ReleaseDateMain = $().SPServices.SPConvertDateToISO(ReleaseDateMainNotFormatted);
Thanks guys!
Jun 20, 2014 at 1:42 PM
Obviously, that only returns the CURRENT date... which is what I just HAPPENED to be testing with, LoL.... so I'm redoing it now... LoL
Jun 20, 2014 at 1:56 PM
Ok, this is what I have, but it's giving me an error:
var ReleaseDateMainNotFormatted = $("input[title='Actual Release Date']").val();

var ReleaseDateMain = $().SPServices.SPConvertDateToISO({
  dateToConvert: $(ReleaseDateMainNotFormatted).val()
});
The error it gives is: "Error: Syntax error, unrecognized expression: 6/16/2014"

So, it's getting the right date now, just not the right format? I'm guessing maybe after the .val(), in the parenthesis I need to format it to match the destination maybe? Desintation column is a DATE ONLY field.

Or am I way off?
Jun 20, 2014 at 3:41 PM
Got it. Removed original VAR and replaced both lines with:
var ReleaseDateMain = $().SPServices.SPConvertDateToISO({
  dateToConvert: new Date ($("input[title='Actual Release Date']").val())
});