2

I wrote javascript for getting list items but here I'm hard coded list name as "Projects"(my list name). How can I get list name automatically with out hard coding,

function DoLogicalDelete()
{

    var clientContext = null;
    var oList = null;
    var oListItem = null;
   //var lstItmIsDeleted = null;

    var itmID = getQuerystring('ID');

    clientContext = SP.ClientContext.get_current();

    oList = clientContext.get_web().get_lists().getByTitle('Projects');


    //var oListItemID = SP.ListOperation.Selection.getSelectedItems(clientContext);


    oListItem = oList.getItemById(itmID);                         // getting ID

    clientContext.load(oListItem,"Title", "IsDeleted");            // load items to oListItem


    oListItem.set_item('IsDeleted', true);
    oListItem.update();

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed)); 
}

Appreciate if anyone help.

Thank you.

2
  • Where are you writing this code? Feb 28, 2014 at 5:35
  • Hi Vardhaman,Actually added one button(not on ribbon. took sp action button ) to Dispform aspx page. When I click on button , IsDeleted checkbox will change no to yes. Here I want to write this globally thats why I'm asking instead of 'Projects'(my list name) what I have to write? Feb 28, 2014 at 5:56

2 Answers 2

6

You could change

oList = clientContext.get_web().get_lists().getByTitle('Projects'); 

to

oList = clientContext.get_web().get_lists().getById(_spPageContextInfo.pageListId)
1
  • Where did you get that information? Maybe it is so in SP2010? Since I specifically checked it in display form of a custom list and the property was there in SP2013.
    – Rae23
    Mar 2, 2014 at 17:55
1
var listTitle = _spPageContextInfo.listTitle;

Works for forms in 2019 On Prem. Haven't tried in 2013/2016 but I bet it works there too.

1
  • Can confirm is works in SharePoint Online. Thanks for posting - saved me a lot of work!
    – Jimmy
    Nov 20, 2020 at 15:44

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.