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

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");
      });
}
  • No that doesn't work. splist.get_title() causes the script to fail. – unibod50 May 16 '18 at 10:22
  • please check the edited code, it will handle the curly braces issue – Gautam Sheth May 16 '18 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! – unibod50 May 16 '18 at 10:58
  • My mistake. I had If instead of if. Hang over from VB programming. – unibod50 May 16 '18 at 12:59
  • did you try the edited code ? – Gautam Sheth May 18 '18 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

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.