SPDisplayRelatedInfo Question

Dec 20, 2011 at 6:07 PM

Hey Marc,

I have a list called counterpartes that has company names and a list called contacts that has contact info with a lookup column to counterparties. I have a document libary called Info that has lookups to both counterparties and contacts.

I added a contact called John Doe to two different counterparties.

In My Document Libraray if I select John Doe, it shows be Boih John Doe records. Is their a way to retirn only the one selected, maybe selecting by ID?

Coordinator
Dec 20, 2011 at 7:29 PM

Russ:

I can't reallly follow your example, but if you have two items which match, then two items will be displayed. One of the options for the function is relatedListColumn, which determines which items match.

M.

Dec 20, 2011 at 11:36 PM

Sorry, I didn't explain that very clearly. If I have a contacts list with two John Doe's ( one from comapny A, one from company B) I would want spdisplayrelatedinfo to display info for the person I selected(John Doe at company A), not all persons with that name. I want it to do  do the lookup into the correspnding list using the ID of the item selected in the dropdowm. NOT the text.

I modified the spdisplayrelatedinfo to do it with an additional parameter called matchonid.  It seems to be working. I'll post the code when I'm back at work tomorrow.

Do you understand the issue I am trying to address?.

 

 

Dec 20, 2011 at 11:40 PM

Hey again, I see that spdisplayrelatedinfo can be tied to a textbox as well . My usage case is when I'm using it in a dropdown list and i want to display related info for the specific person selected in the dropdown.

Coordinator
Dec 21, 2011 at 12:53 AM
SPDisplayRelatedInfo only works with dropdowns. In the function, I assume that the values in your dropdown correspond to values in a column in another list. Person or Group columns are a little different. Are you not using the People Picker?

M.
Dec 21, 2011 at 1:15 AM

I'm not using peoplepicker beacause the 'contacts' are external users... its a custom list.  I wanted SPDisplayRelatedInfo to get the info from the custom list using the id of the item the user selected, not the text value of the item the user selected.

If a user selected item # 17, i want to show him info related to item  #17, not info for all items that have the same Title as item #17.

Dec 21, 2011 at 3:57 PM

I  modified  SPDisplayRelatedInfo to get the related info using the selected Items ID rather than its description. I added a parameter called matchonid that does this.

 function showRelated(opt) {

        var columnSelectSelected = null;

        // Find the column's select (dropdown)
        var columnSelect = new dropdownCtl(opt.columnName);

        // Get the current column selection(s)
        switch (columnSelect.Type) {
            case "S":
                if (opt.matchonid)
                    columnSelectSelected = columnSelect.Obj.find("option:selected").value();
                else
                    columnSelectSelected = columnSelect.Obj.find("option:selected").text();
                break;
            case "C":

                if (opt.matchonid)
                // get the value from opthiod
                    columnSelectSelected = $("input[id='" + columnSelect.Obj.attr("optHid") + "']")[0].value;
                else
                    columnSelectSelected = columnSelect.Obj.attr("value");
                     // Check to see if at least opt.numChars have been typed (if specified)
                if (opt.numChars > 0 && columnSelectSelected.length < opt.numChars) return;
                break;
            case "M":
                break;
            default:
                break;
        }

        // If the selection hasn't changed, then there's nothing to do right now.  This is useful to reduce
        // the number of Web Service calls when the parentSelect.Type = "C", as there are multiple propertychanges
        // which don't require any action.
        if (columnSelect.Obj.attr("showRelatedSelected") == columnSelectSelected) return;
        columnSelect.Obj.attr("showRelatedSelected", columnSelectSelected);
        var divId = genContainerId("SPDisplayRelatedInfo", opt.columnName);
        $("#" + divId).remove();
        columnSelect.Obj.parent().append("<div id=" + divId + "></div>");


        // Only get the requested columns
        var relatedColumnsXML = [];

        // Get information about the related list and its columns
        $().SPServices({
            operation: "GetList",
            async: false,
            webURL: opt.relatedWebURL,
            listName: opt.relatedList,
            completefunc: function(xData, Status) {
                // If debug is on, notify about an error
                $(xData.responseXML).find("faultcode").each(function() {
                    if (opt.debug) errBox("SPServices.SPDisplayRelatedInfo",
						"relatedList: " + opt.relatedList,
						"List not found");
                    return;
                });
                // Output each row
                $(xData.responseXML).find("Fields").each(function() {
                    $(xData.responseXML).find("Field").each(function() {
                        for (i = 0; i < opt.relatedColumns.length; i++) {
                            // If this is one of the columns we want to display, save the XML node
                            if ($(this).attr("Name") == opt.relatedColumns[i]) relatedColumnsXML[i] = $(this);
                        }
                    });
                });
            }
        });

        // Get the list items which match the current selection
        var camlQuery = "<Query><Where>";
        if (opt.CAMLQuery.length > 0) camlQuery += "<And>";
        if (opt.matchonid)
            camlQuery += "<Eq><FieldRef Name='ID'/><Value Type='Lookup'>" + columnSelectSelected + "</Value></" + opt.matchType + ">";
        else
        camlQuery += "<" + opt.matchType + "><FieldRef Name='" + opt.relatedListColumn + "'/><Value Type='Text'>" + escapeColumnValue(columnSelectSelected) + "</Value></" + opt.matchType + ">";
        if (opt.CAMLQuery.length > 0) camlQuery += opt.CAMLQuery + "</And>";
        camlQuery += "</Where></Query>";

        var viewFields = " ";
        for (i = 0; i < opt.relatedColumns.length; i++) {
            viewFields += "<FieldRef Name='" + opt.relatedColumns[i] + "' />";
        }
        $().SPServices({
            operation: "GetListItems",
            async: false,
            webURL: opt.relatedWebURL,
            listName: opt.relatedList,
            // Filter based on the column's currently selected value
            CAMLQuery: camlQuery,
            CAMLViewFields: "<ViewFields>" + viewFields + "</ViewFields>",
            // Override the default view rowlimit and get all appropriate rows
            CAMLRowLimit: 0,
            completefunc: function(xData, Status) {
                $(xData.responseXML).find("faultcode").each(function() {
                    if (opt.debug) errBox("SPServices.SPDisplayRelatedInfo",
						"relatedListColumn: " + opt.relatedListColumn,
						"Column not found in relatedList " + opt.relatedList);
                    return;
                });

                var outString;
                // Output each row
                switch (opt.displayFormat) {
                    // Only implementing the table format in the first iteration (v0.2.9) 
                    case "table":
                        outString = "<table>";
                        outString += "<tr>";
                        for (i = 0; i < opt.relatedColumns.length; i++) {
                            if (relatedColumnsXML[i] == undefined && opt.debug) {
                                errBox("SPServices.SPDisplayRelatedInfo",
									"columnName: " + opt.relatedColumns[i],
									"Column not found in relatedList");
                                return;
                            }
                            outString += "<th class='" + opt.headerCSSClass + "'>" + relatedColumnsXML[i].attr("DisplayName") + "</th>";
                        }
                        outString += "</tr>";
                        // Add an option for each child item
                        $(xData.responseXML).find("[nodeName=z:row]").each(function() {
                            outString += "<tr>";
                            for (i = 0; i < opt.relatedColumns.length; i++) {
                                outString += "<td class='" + opt.rowCSSClass + "'>" + showColumn(relatedColumnsXML[i], $(this).attr("ows_" + opt.relatedColumns[i]), opt) + "</td>";
                            }
                            outString += "</tr>";
                        });
                        outString += "</table>";
                        break;
                    // list format implemented in v0.5.0. Still table-based, but vertical orientation. 
                    case "list":
                        outString = "<table>";
                        for (i = 0; i < opt.relatedColumns.length; i++) {
                            $(xData.responseXML).find("[nodeName=z:row]").each(function() {
                                outString += "<tr>";
                                outString += "<th class='" + opt.headerCSSClass + "'>" + relatedColumnsXML[i].attr("DisplayName") + "</th>";
                                outString += "<td class='" + opt.rowCSSClass + "'>" + showColumn(relatedColumnsXML[i], $(this).attr("ows_" + opt.relatedColumns[i]), opt) + "</td>";
                                outString += "</tr>";
                            });
                        }
                        outString += "</table>";
                        break;
                    default:
                        break;
                }
                $("#" + divId).html("").append(outString);
            }
        });
        // If present, call completefunc when all else is done
        if (opt.completefunc != null) opt.completefunc();
    }

Coordinator
Dec 29, 2011 at 2:36 PM

Russ:

ptavares has done a similar thing for SPCascadeDropdowns, which you can see here: http://spservices.codeplex.com/discussions/274158

Thanks for posting your code. I'll try to get something similar into the next release.

M.

Coordinator
Dec 29, 2011 at 2:37 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Dec 29, 2011 at 2:37 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Dec 29, 2011 at 3:45 PM

Russ,

As Marc indicated, I also added a similar option to the SPCascadeDropdown tool...

I reviewing your code above, I suggest you change the CAML query <value> tag to use a type=Integer when using your "matchonid" input option... Sharepoint is not consistant on the type when it comes to large lists...  With small lists, setting this attribute to Integer does not seem to be important... however, I encountered a problem with large lists where the queries stop matching on the ID if the type was not Integer...

So your code above for the following snippet:

if (opt.matchonid) {
    camlQuery += "<Eq><FieldRef Name='ID'/><Value Type='Lookup'>" + columnSelectSelected + "</Value></" + opt.matchType + ">";
}...

Would be changed to:

if (opt.matchonid) {
    camlQuery += "<Eq><FieldRef Name='ID'/><Value Type='Integer'>" + columnSelectSelected + "</Value></" + opt.matchType + ">";
}...

 

Paul

 

 

 

 

 

 

Coordinator
Jan 17, 2012 at 9:27 PM

Guys:

I've just posted a new alpha with this functionality. I'd appreciate any testing you might be able to do.

M.

Coordinator
Jan 17, 2012 at 9:32 PM

p.s. The option is matchOnId, which is false by default.

Jan 17, 2012 at 10:33 PM
Hey Marc,
The Cacascading Dropdowns really fly in the new version. It used to take web service calls, now it takes ONE. Nice Job!!

I also tried testing the related list change, but it didn't work for me [ it displayed no related info]. I'll try to debug though it tomorrow.

Do I need a new version of jquery to run this. I'm currently running 1.4.4.
Russell

On , sympmarc <notifications@codeplex.com> wrote:
>
> From: sympmarc
> p.s. The option is matchOnId, which is false by default.
>
> Read the full discussion online.
> To add a post to this discussion, reply to this email (SPServices@discussions.codeplex.com)
> To start a new discussion for this project, email SPServices@discussions.codeplex.com
> You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.
> Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com
>
>
Coordinator
Jan 17, 2012 at 10:39 PM

Great to hear that you see a performance difference. I've rebuilt a lot of the function.

I haven't tested with older versions of jQuery. Try 1.7 and see how that works. I can try to march back through the main versions as well.

M.