SPGetQueryString & GetListItems

Apr 4, 2012 at 2:38 PM
Edited Apr 4, 2012 at 3:08 PM

I am wanting to populate several single line text fields in a NewForm based on the data in another list in same site. Using the SPGetQueryString is the easy part but I am not sure of how I can populate the fields after I do a GetListItems using the ID from the previous form. Any examples I can look at?

 I am currently using Alexander's solution but feel that anyone can bypass the query string (copy & paste) and populate the form with anything they feel like, by using your solution all I need to pass is the ID value.

 

This is what I have thus far:

 

$(document).ready(function() {
 var RequestIDVal = $().SPServices.SPGetQueryString();
 var RequestID = queryStringVals["RequestIDVal"];

 $().SPServices({
  operation: "GetListItems",
  async: false,
  listName: "Other List",
  CAMLViewFields: "<ViewFields>" +
  "<FieldRef Name='field1' />" +
  "<FieldRef Name='field2' />" +
  "<FieldRef Name='field3' />" +
  "</ViewFields>",
  CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>" + RequestID + "</value></Eq></Where></Query>", 

 completefunc: function (xData, Status) {
   $(xData.responseXML).SPFilterNode("z:row").each(function() {
    });
  }
 });
});

 

Not sure what to place in the completefunc to populate the fields...

Thanks.

Apr 4, 2012 at 6:22 PM

This is done...you may delete.

Coordinator
Apr 4, 2012 at 6:28 PM

Bob:

I always love it when someone answers their own question before anyone can help them. If you wouldn't mind posting what you ended up with, it may help others in the future.

Thanks,
M.

Apr 5, 2012 at 2:20 AM

Marc...my pleasure. I also use Alexander Bautz's spjs-utility.js so this is part of the solution. Hope this helps someone...

 

<script type="text/javascript">

fields = init_fields_v2();

 var IDVal = $().SPServices.SPGetQueryString();
 var ID = IDVal["ID"];
 //alert(ID);

 $().SPServices({
  operation: "GetListItems",
  async: false,
  listName: "Some List",
  CAMLViewFields: "<ViewFields>" +
  "<FieldRef Name='Title' />" +
  "<FieldRef Name='Type' />" +
  "<FieldRef Name='Request' />" +
  "<FieldRef Name='Assign' />" +
  "</ViewFields>",
  CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>" + ID + "</Value></Eq></Where></Query>",
  completefunc: function (xData, Status) {
   //alert("Sending the xml request to the server was: " + Status);
   //alert("Sharepoint processing of the message returned: " + xData.responseText);
   $(xData.responseXML).SPFilterNode("z:row").each(function() {
    var Title = $(this).attr("ows_Title");
    //alert(Title);
    var Request = $(this).attr("ows_Request");
    //alert(Request);
    var Assign = $(this).attr("ows_Assign");
    //alert(Assign);
    var Type = $(this).attr("ows_Type");
    //alert(Type);
    setFieldValue('Title', Title);
    setFieldValue('Requester', Request);
    setFieldValue('Creator', Assign);
    setFieldValue('Type', Type);
   });
  }
 });
</script>

May 19, 2012 at 5:43 AM

Hi Bob_Mac

I'm trying to achieve a very similar result to the one you have created above, ie pass the value of one field from list A into the NewForm for list B. I've adapted your code above to my needs but it's not working. I wonder if you can help me.

Code being used is:

fields = init_fields_v2();
  $(document).ready(function()
  {
 var accountID = getParameterByName("AccountID");
 //alert("AccountID is: " + accountID);
 $().SPServices
 ({
   operation: "GetListItems",
   async: false,
   listName: "Accounts",
   CAMLViewFields: "<ViewFields>" +
   "<FieldRef Name='Title' />" +
   "</ViewFields>",
   CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>" + accountID + "</Value></Eq></Where></Query>",
   completefunc: function (xData, Status)
  {
  //alert("Sending the xml request to the server was: " + Status);
  //alert("Sharepoint processing of the message returned: " + xData.responseText);
  $(xData.responseXML).SPFilterNode("z:row").each(function()
   {
   var Title = $(this).attr("ows_Title");
   alert("Account Name returned is: " + Title);
   setFieldValue('Company', Title);
   });
  }
 });
});

The last alert returns the value I'm trying to copy in but the field fails to update.

Hope you can shed some light on this.

TIA

MartyNZ