Roughly 1,5 years ago I wrote a post about how we can use the SP.ListOperation.Selection object and its getSelectedList method to find out the ID of the current list, and how to submit this value as an asynchronous query using the ECMAScript Client Object Model to get other list properties, like Title. Although this method is probably still adequate for more advanced scenarios, there seems to be a way to get simple properties (like Title) easier, without a second round-trip to the server.
The key to the success is the non-documented GetCurrentCtx function. The next code snippet shows a few interesting properties available in this context.
- var ctxT = GetCurrentCtx();
- // SPList.BaseType property (SPBaseType enumeration)
- alert(ctxT.listBaseType);
- // SPList.ID property
- alert(ctxT.listName);
- // SPList.BaseTemplate property (SPListTemplateType enumeration)
- alert(ctxT.listTemplate);
- // SPList.RootFolder.Url property
- alert(ctxT.listUrlDir);
- // SPList.Title property
- alert(ctxT.ListTitle);
- if (ctxT.ListTitle == 'YourListName') {
- // do the custom action
- }
Note: Because this function is defined in CORE.JS, you should delay your script (for example, using SP.SOD.executeOrDelayUntilScriptLoaded), while the declaring script is fully loaded.
Using these properties is much easier and faster, than bothering with asynchronous Client OM requests. If you need to use other list / web / site properties, you should first check the source code of the page to decide if that property is available in the context. If it is not included in the page, you can fall back to the Client OM.