1

I have some javascript that runs when a button is clicked. It processes the items that have been selected in a list web-part. However, I want to have two list web-parts on my page that refer to two different lists. When the button is clicked I want to know what the name of the list is that has items selected in, so that I can change the script logic according to the list web-part selected.

I have;

function getMailto() {

  var ctx = SP.ClientContext.get_current();
  var listId = SP.ListOperation.Selection.getSelectedList(); //selected list Id
  var splist = ctx.get_web().get_lists().getById(listId);

 alert(splist.get_name());

}

The alert(splist.get_name()) does not work of course because I do not know the syntax to get the name of splist.

Seems simple, if you know how!

EDIT:

If I do alert(listId); I see the list internal id {E7B55200-013C-4A48-A92F-F3D9CF859D88}. So I could use this for my script logic. However;

  If (listId == "{E7B55200-013C-4A48-A92F-F3D9CF859D88}") {
     ...
  }

Does not work. It does not appear to be a string. Can I convert it to one.

Either of the above requested solutions will enable me to do what I want. Help please!

0

2 Answers 2

0

You can get list name as below. You need to load the list object and then you will be able to retrieve it's title value :

function getMailto() {

      var ctx = SP.ClientContext.get_current();
      var listId = SP.ListOperation.Selection.getSelectedList(); //selected list Id
      var formattedString = listId.substr(1,listId.length - 2);
      var splist = ctx.get_web().get_lists().getById(formattedString);

      ctx.load(splist);
      ctx.executeQueryAsync(function(){
           alert(splist.get_title());   
        },function(){
           console.log("error");
      });
}
6
  • No that doesn't work. splist.get_title() causes the script to fail. Commented May 16, 2018 at 10:22
  • please check the edited code, it will handle the curly braces issue Commented May 16, 2018 at 10:33
  • From what you have shown the listID is a string. What I don't understand is why if (listID == "{E7B55200-013C-4A48-A92F-F3D9CF859D88}") {...} does not work. I've tried === instead of == as some blogs suggest but this line causes the javascript to fail. Perplexed! Commented May 16, 2018 at 10:58
  • My mistake. I had If instead of if. Hang over from VB programming. Commented May 16, 2018 at 12:59
  • did you try the edited code ? Commented May 18, 2018 at 12:27
0

Sorted out a solution to my question;

<script type="text/javascript">

function getMailto() {
  var ctx = SP.ClientContext.get_current();
  var listId = SP.ListOperation.Selection.getSelectedList(); //selected list Id
  var splist = ctx.get_web().get_lists().getById(listId);

  if (listId == "{E7B55200-013C-4A48-A92F-F3D9CF859D88}") {
     <Do stuff here>
  }
  else if (listId == "{836BD247-725F-486E-A617-38EA98AD31F6}") {
     <Do stuff here>
  } 
}

The {E7B55200-013C-4A48-A92F-F3D9CF859D88} and {836BD247-725F-486E-A617-38EA98AD31F6} are the internal Ids of the respective lists present in my web-parts on my page. I get them by going to my list app via the Site Contents page. Click on List Properties and pasting the URL in this online URL Decoder/Encoder

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.