none
SP.ListOperation.Selection.getSelectedList() returns null?

    Question

  • I'm trying to create a custom webpart in SharePoint 2010 Designer and embedding a script in Content Editor Web Part among with XSLT data view web part for rendering a document library. using EMCAScript object model, it seems that when I do this:

     

    var listId = SP.ListOperation.Selection.getSelectedList();

     

    listId is null after the call. The MSDN documentation is very sparse and I have no idea what could be causing this behavior. I've already tried moving my script out of CEWP and into the page directly just to rule out possibility of script running in a different context and thus unable to see the selection to no avail. Looking at various code sample over the internet, I can verfiy that this should work for a document library, that I don't need to pass in context as an argument (but I did try anyway just to rule it out). 

    If it's any help, I can successfully call SP.ListOperation.Selection.getSelectedItems(ctx) but I get nothing back even though there were items selected on the page. The page itself does not have any ribbon or native menu items that are typically available with the data part web part, the intention being to use only custom menu items.

    What would cause the method to return null? I'm sure it's to do with context being different but I'm not sure how I'd get it in same context without causing an error on the XSLT data web part.

     

    Thanks.

    Sunday, November 07, 2010 5:55 PM

Answers

  • After starting over with clean slate and doing it carefully, I still get the issue. Only by manually inserting the missing _wpSelected was I able to fix it. I think the problem I'm observing may be a result of a bug... can anyone please confirm if they can reproduce this?

     

    Steps to reproduce:

    1) Start with a blank ASPX page.

    2) Add Web Part Zone; SPWebPartManager should be added automatically.

    3) Add Data View Web Part.

    4) Insert in a custom HTML, a hyperlink that invokes a custom javascript should suffice. The code I used looks similar to this (embedded into the XSLT):

    <a>
    	<xsl:attribute name="onclick">javascript:{javCustom()}</xsl:attribute>
    	<xsl:attribute name="href">javascript:</xsl:attribute>
    	<xsl:text disable-output-escaping="yes">&amp;nbsp;Test HyperLink</xsl:text>
    </a>
    <script type="text/javascript">
        function javCustom() {
    		var ctx = new SP.ClientContext.get_current();
     		if (ctx != undefined &amp;&amp; ctx != null) {
    			var listId = SP.ListOperation.Selection.getSelectedList();	
    			var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
    
    			var key;
    			for (key in selectedItems) {
    				alert(key.ID);
    			}
     		}
       	}
    </script>
    

    5) Publish the page and preview the page.

    6) Click the hyperlink.

     

    When Javascript runs, the SelectedList will be null and SelectedItems will be empty even if there's selection. This can be then fixed by adding this manually:

    <input type="hidden" name="_wpSelected" id="_wpSelected" value="MSOZoneCell_WebPartWPQ1" />
    

    Where "MSOZoneCell_WebPartWQ1" is the rendered name of the DVWP. When this is added, the above code will then work normally, but that should be not necessary, unless I've done something wrong and missed a crucial step that would prompt the Designer to add this crucial piece of HTML into the final product.

     

    Can anyone please confirm/deny/clarify the finding? TIA.

    • Proposed as answer by Wayne Fan Wednesday, November 17, 2010 3:46 AM
    • Marked as answer by Wayne Fan Thursday, November 18, 2010 2:04 AM
    Monday, November 08, 2010 2:48 PM

All replies

  • Some additional information:

    I worked out a way to inject my custom HTML and Javascript directly into the XSLT Data View Web Part to rule out the possibility of CEWP running in a different context from the XSLT Data View Web Part. Once done, I still get null for the getSelectedList().

    I stepped into the core.js and noted that both getSelectedList and getSelectedItems depends on this:

    var c=document.getElementById("_wpSelected");

    The element by this name was nowhere to be found in the source. Is this the culprit? I assume that was supposed to be supplied by SharePoint Designer, or is there a specific step I need to take to get _wpSelected to return a valid value?

    (Just to be clear, though, the calls to ClientContext, get_Web() and get_Lists() does appear work, in that I do get an object returned from those calls rather than nulls but I'm not certain if they are in fact the objects I expect them to be, as I had difficulty deciphering any properties of objects.)

    Monday, November 08, 2010 12:33 AM
  • After starting over with clean slate and doing it carefully, I still get the issue. Only by manually inserting the missing _wpSelected was I able to fix it. I think the problem I'm observing may be a result of a bug... can anyone please confirm if they can reproduce this?

     

    Steps to reproduce:

    1) Start with a blank ASPX page.

    2) Add Web Part Zone; SPWebPartManager should be added automatically.

    3) Add Data View Web Part.

    4) Insert in a custom HTML, a hyperlink that invokes a custom javascript should suffice. The code I used looks similar to this (embedded into the XSLT):

    <a>
    	<xsl:attribute name="onclick">javascript:{javCustom()}</xsl:attribute>
    	<xsl:attribute name="href">javascript:</xsl:attribute>
    	<xsl:text disable-output-escaping="yes">&amp;nbsp;Test HyperLink</xsl:text>
    </a>
    <script type="text/javascript">
        function javCustom() {
    		var ctx = new SP.ClientContext.get_current();
     		if (ctx != undefined &amp;&amp; ctx != null) {
    			var listId = SP.ListOperation.Selection.getSelectedList();	
    			var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
    
    			var key;
    			for (key in selectedItems) {
    				alert(key.ID);
    			}
     		}
       	}
    </script>
    

    5) Publish the page and preview the page.

    6) Click the hyperlink.

     

    When Javascript runs, the SelectedList will be null and SelectedItems will be empty even if there's selection. This can be then fixed by adding this manually:

    <input type="hidden" name="_wpSelected" id="_wpSelected" value="MSOZoneCell_WebPartWPQ1" />
    

    Where "MSOZoneCell_WebPartWQ1" is the rendered name of the DVWP. When this is added, the above code will then work normally, but that should be not necessary, unless I've done something wrong and missed a crucial step that would prompt the Designer to add this crucial piece of HTML into the final product.

     

    Can anyone please confirm/deny/clarify the finding? TIA.

    • Proposed as answer by Wayne Fan Wednesday, November 17, 2010 3:46 AM
    • Marked as answer by Wayne Fan Thursday, November 18, 2010 2:04 AM
    Monday, November 08, 2010 2:48 PM
  • I see that Wayne Fan has marked the above as a solution -- am I to assume that this was also reproduced? I also hesitate to call it an answer since this is just a workaround for what should not be needed and I doubt I actually completely understand the repercussions of introducing an ad hoc fix when it could be a bug or something that's entirely my fault.
    Thursday, November 18, 2010 5:44 AM
  • You are the man!

    You save my life with this line:

    <input type="hidden" name="_wpSelected" id="_wpSelected" ...

    Thank you much!


    Federico

    Saturday, February 25, 2012 11:48 PM