1

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

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

2 Answers 2

1

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

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 that you have read and understand our privacy policy and code of conduct.

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