Second Life of a Hungarian SharePoint Geek

February 26, 2013

Using properties of the current SharePoint list when working with JavaScript

Filed under: JavaScript, SP 2010, Tips & Tricks — Tags: , , — Peter Holpar @ 13:04

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.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: