I have a webpart .I add a listviewbyquary in createchildcontrols and a hyperlink in visualwebpart.

i use this


I want get selected item in ListView when hyperlink click. i use this code in webpart

<script >
function geturl()
var context = SP.ClientContext.get_current();
this.web = context.get_web();
var listId = SP.ListOperation.Selection.getSelectedList();
var list = this.web.get_lists().getById(listId);
var items = SP.ListOperation.Selection.getSelectedItems(context);  //Get selected   documents
var itemId = items[0];
var url="@Url"+itemId;
return url;

and code in Hyperlink

<a href="javascript:geturl()" >click</a>

but don't work .

var context = SP.ClientContext.get_current(); is null


This code return the value when i use in list page , but when i use on Webpart , don't work

  • 2
    I've checked this code - it's work fine. Are you sure your js code executed? Can you give full code of linkbutton?
    – IAfanasov
    Commented Jan 15, 2012 at 10:43
  • yes , Please read my update.
    – ar.gorgin
    Commented Jan 16, 2012 at 5:19
  • Have you tried OnClientClick rather than PostBackURL? And when calling your function, you should need brackets. And a 'runat="server" ' attribute.
    – Andy Burns
    Commented Jan 16, 2012 at 13:08
  • Yes , i have tried. but doesn't work.
    – ar.gorgin
    Commented Jan 16, 2012 at 14:35
  • Is it a standard XsltListViewWebPart? Commented Jan 19, 2012 at 11:03

2 Answers 2


First, you can't run postback (by defining postbackurl in your linkbutton). If postback occurs, javascript looses its context.

Just add simple html markup such as:

<a href="javascript:getSelectedItemUrl()">Click on me </a>

To get the id of the first selected list item:


If SP.ClientContext.get_current() is null, then you have to ensure SP.js is loaded first, like in this question:

ExecuteOrDelayUntilScriptLoaded(GetUrl, "SP.js")

So your code can be encapsulated in this ExecuteOrDelay..:

function getSelectedItemUrl() {
  ExecuteOrDelayUntilScriptLoaded(GetUrl, "SP.js");
function GetUrl() {
  return @url + SP.ListOperation.Selection.getSelectedItems()[0].id;
  • Thanks a lot , but when i use this code,i come across this error:The value of the property 'ExecuteOrDelayUntilScriptLoaded' is null or undefied , not a function object
    – ar.gorgin
    Commented Jan 17, 2012 at 16:51
  • What masterpage do you use. Is ScriptManager included? Commented Jan 17, 2012 at 19:37
  • Yes , i use v4.master.
    – ar.gorgin
    Commented Jan 18, 2012 at 8:06
  • Weird. Can you try someghing in Browser Console (F12) when your sharepoint page is loaded, like SP.ListOperation Commented Jan 18, 2012 at 8:52
  • yes, when i load page , i found SP.ListOperation
    – ar.gorgin
    Commented Jan 19, 2012 at 8:22

Have you checked that the script is for it is included? I had problem before that the correct script files not where included.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.