Single List Item

Jul 30, 2013 at 2:46 PM
I have a situation where I would like to manipulate items in the Display list item Modal window, specifically adding a Google Map depending on the record. The trickier part of this is that the address for my Google map is based on another "Locations" list that I have created, to which is a lookup. I would love to be able to retrieve the address from the Lookup, i.e. "Business Office". From then I can pipe the address into the Google MAP API that I have already integrated using a cool jquery plugin called "gomap". Anyways, the goa of this discussion is how we can return one record from a List that is specific to an ID or some other relational field using SPServices. I know how to retrieve ALL list items using using GetListItems and an each() function, however the trick is to getting a specific one.
Jul 30, 2013 at 7:37 PM
fslassiter,

Just to confirm - from your posting, are we to assume that you already have the ID, and need the information behind it? If this is the case, could you not just do a match against that value, and then assign the resulting information to variables, ready for use elsewhere in your code?
Jul 30, 2013 at 7:53 PM
Edited Jul 30, 2013 at 8:00 PM
alibby,

I would be getting the ID from the Item that I clicked on, for example, clicking from a List title. Anyways I am halfway there or closer. I completely spaced on this, I do not know how I could have completely forgotten about the CAMLQuery that you can add to GetListItems! While I am embarrassed, I am glad I found a closer path to a solution. The trick now is finding a way to assignt he variable a "clicked" ID. Check the example below:

FYI, I had to use the jQuery slice() function because when referencing a lookup, it gives me 3 character in font of my lookup result: 3;#. I am not sure if there is a cleaner way around this.
$().SPServices({
        operation: 'GetListItems',
        async: false,
        debug: true,
        listName: 'IT Assets',
        CAMLViewFields: '<ViewFields><FieldRef Name="Location" /></ViewFields>',
        CAMLQuery: '<Query><Where><Eq><FieldRef Name="ID"/><Value Type="Counter">1</Value></Eq></Where></Query>',
        completefunc: function(xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function() {
                alert($(this).attr("ows_Location").slice(3));
                alert($(this).attr("ows_ID"));
            });
        }
    });
Jul 30, 2013 at 10:02 PM
I was able to find a way to get the List Item ID of the list item that I had clickedbyusing a function Marc created called SPGetLastItemId. Thanks Marc!
var listID = $().SPServices.SPGetLastItemId({   
        listName: "IT Assets",
    });
Jul 30, 2013 at 10:03 PM
Hehe - I know what you mean about spacing out; there's no need to feel embarrassed, as I am sure most of us have been there at some point in our careers!

In terms of getting rid of the "3;#" - I'm puzzled by something: are you using a People and Groups field to store your Location value, by any chance? Those characters usually appear for this reason; it seems odd that you're using this type of field, given this is a "Location" column? Or is it a case that "Location" actually stores the name of a person at that location, perhaps? ;-)

That all said - I'm not sure, but when you say you need to assign a variable to a "clicked" ID, do you mean something like this:
        $(xData.responseXML).SPFilterNode("z:row").each(function() {
            owsLocation = $(this).attr("ows_Location").slice(3);
            owsID = $(this).attr("ows_ID");
        });
by any chance? You will need to set up two global vars, one called owsLocation, and the other owsID, for this to work correctly.
Coordinator
Jul 31, 2013 at 10:11 PM
fslassiter:

SPGetLastItemId retrieves the last item created by a specific user, so if that is what you want you can use it the way your say you are. Otherwise, simply pass an ID in as part of the CAMLQuery for GetListItems.

M.
Aug 1, 2013 at 3:10 PM
Thanks for the reply Marc. The issue is that I am trying to get the ID. So, let me elaborate, the user opens the list, clicks on the Title of the list item, then the view items details modal window opens. The display is where I will be placing the google map. I have hardcoded address in there now, however it is super slick, it shows the Item details then a nice satellite view of the location below so the user will know where the Asset is located. The jQUery I am using is embedded in the list item (display.aspx) of my list. To enter the ID into the CAML query, which I have implemented, (a hardcoded ID is in there now for testing), I need to be able to grab the ID that the user clicked on. Maybe I am just overlooking something completely simple, however I could not find an easy way to get the ID of the item of the modal window opened. I have scoured the window with Firebug and cannot seem to find a solid ID, class, or attribute to hook to with my jQuery (typical of Sharepooint). From there I will then work on the next step, getting the lookups worked out. Maybe there has been a function in SPServices already for this to "capture the ID". I will keep looking:) Agian, love SPServices, have done some other very cool stuff with it so far!
Aug 1, 2013 at 6:58 PM
So, the way that I was able to pass in the ID for the CAMLQuery was to change my library settings to not use dialogs, so that I could grab the query string value of the item record, using Marcs $().SPServices.SPGetQueryString(), thanks Marc!

Just for anyone else that may want to read, I implemented it similar to the way Marc has in his documentation. Notice that I use qsID in the CAMLQuery as the ID. I would love to be able to continue to use the dialog, however that is more of a user preference there may be equal users who prefer the page over the dialog so I should not be predjudice. This is my custom project anyways, so they can live with the page for now.
    var qsVals = $().SPServices.SPGetQueryString();
    var qsID = qsVals["ID"];
        
    // pass qsID into the CAMLquery below
        
    $().SPServices({
        operation: 'GetListItems',
        async: false,
        //debug: true,
        listName: 'IT Assets',
        CAMLViewFields: '<ViewFields><FieldRef Name="Location" /></ViewFields>',
        CAMLQuery: '<Query><Where><Eq><FieldRef Name="ID"/><Value Type="Counter">' + qsID + '</Value></Eq></Where></Query>',
        completefunc: function(xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function() {
                alert($(this).attr("ows_Location").slice(3));
                alert($(this).attr("ows_ID"));
            });
        }
    });
    
    
});

Aug 2, 2013 at 9:39 PM
So, finally got it to work! Need a few more customizations to the pop up in the map, however thanks for the help guys, what a great way to end a Friday! SPServices made my weekend again, also Tiger shooting a 61 today was nice as well.......
// JS file that powers the IT Asset Maps pages

$(document).ready(function() {
    
    var qsVals = $().SPServices.SPGetQueryString();
    var qsID = qsVals["ID"];
        
    // pass qsID into the CAMLquery below
    var location = '';
    $().SPServices({
        operation: 'GetListItems',
        async: false,
        //debug: true,
        listName: 'IT Assets',
        CAMLViewFields: '<ViewFields><FieldRef Name="Location" /></ViewFields>',
        CAMLQuery: '<Query><Where><Eq><FieldRef Name="ID"/><Value Type="Counter">' + qsID + '</Value></Eq></Where></Query>',
        completefunc: function(xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function() {
                //alert($(this).attr("ows_Location").slice(3));
                var location = $(this).attr("ows_Location").slice(3);
                //alert(location);
                getLocation(location);
            });
        }
    });
    function getLocation(loc) {
        $().SPServices({
            operation: 'GetListItems',
            async: false,
            debug: true,
            listName: 'Location',
            //webURL: 'http://dfhsp.com/sites/DFH/ISDept/support/Lists/Location/',
            CAMLViewFields: '<ViewFields><FieldRef Name="Address" /><FieldRef Name="City" /><FieldRef Name="State" /></ViewFields>',
            CAMLQuery: '<Query><Where><Eq><FieldRef Name="Title"/><Value Type="Text">' + loc + '</Value></Eq></Where></Query>',
            completefunc: function(xData, Status) {
                $(xData.responseXML).SPFilterNode("z:row").each(function() {
                    var address = $(this).attr("ows_Address");
                    var city = $(this).attr("ows_City");
                    var state = $(this).attr("ows_State").slice(4);
                    //alert($(this).attr("ows_ID"));
                    var fullAdd = address + ',' + city + ',' + state;
                    $('.ms-formtable').after('<div id="assetMap"></div>');
    
                    $('#assetMap').goMap({
                        address: fullAdd, 
                        zoom : 15
                        
                    });
                    $.goMap.createMarker({
                        address: fullAdd,
                                title : loc,
                                html : {
                                    content : loc,
                                    popup : true
                                }
                    });             
                });
            }
            
        });
        
        
    }
});


Coordinator
Aug 5, 2013 at 11:29 AM
Glad you got it. IMO, the always-on dialogs for SharePoint 2010 forms are excessive. They often make the user experience worse (with longer forms, for instance), not better. Turning them off based on the form structure is always something to consider.

M.