Quantcast
Channel: Second Life of a Hungarian SharePoint Geek
Viewing all articles
Browse latest Browse all 206

Using properties of the current SharePoint list when working with JavaScript

$
0
0

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.

Code Snippet
  1. var ctxT = GetCurrentCtx();
  2. // SPList.BaseType property (SPBaseType enumeration)
  3. alert(ctxT.listBaseType);
  4. // SPList.ID property
  5. alert(ctxT.listName);
  6. // SPList.BaseTemplate property (SPListTemplateType enumeration)
  7. alert(ctxT.listTemplate);
  8. // SPList.RootFolder.Url property
  9. alert(ctxT.listUrlDir);
  10. // SPList.Title property
  11. alert(ctxT.ListTitle);
  12. if (ctxT.ListTitle == 'YourListName') {
  13.     // do the custom action
  14. }

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.



Viewing all articles
Browse latest Browse all 206

Trending Articles