Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

i use this

http://graegert.com/featured/using-the-editorpart-to-create-a-custom-listview-web-part

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;
 </script>

and code in Hyperlink

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

but don't work .

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

//Edit

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

share|improve this question
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
show 3 more comments

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:

SP.ListOperation.Selection.getSelectedItems()[0].id

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;
}
share|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
show 4 more comments

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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