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 Jan 15 '12 at 10:43
  • yes , Please read my update. – ar.gorgin Jan 16 '12 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 Jan 16 '12 at 13:08
  • Yes , i have tried. but doesn't work. – ar.gorgin Jan 16 '12 at 14:35
  • Is it a standard XsltListViewWebPart? – Anatoly Mironov Jan 19 '12 at 11:03

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;
| improve this answer | |
  • 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 Jan 17 '12 at 16:51
  • What masterpage do you use. Is ScriptManager included? – Anatoly Mironov Jan 17 '12 at 19:37
  • Yes , i use v4.master. – ar.gorgin Jan 18 '12 at 8:06
  • Weird. Can you try someghing in Browser Console (F12) when your sharepoint page is loaded, like SP.ListOperation – Anatoly Mironov Jan 18 '12 at 8:52
  • yes, when i load page , i found SP.ListOperation – ar.gorgin Jan 19 '12 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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