2013, 2016, Development Blog, Office 365, SharePoint
SharePoint JavaScript CSOM – currentItem.get_item(columnName) – how do I get at the property options and values?
No doubt if you’ve worked with CSOM/JSOM (JavaScript Client-Side Object Model), you’ve accessed lists and libraries to consume the contained data.
If you want to see a sample of how to do this to get you started, check out Clean CSOM JavaScript structure to access SharePoint list data.
This article aims to help you access the data AFTER you’ve successfully loaded the list data.
To duplicate the steps below, you’ll need basic knowledge of debugging JavaScript and the Chrome browser (best of luck if you’re using IE).
Keep in mind
There is no exhaustive list of properties for list items, due to:
- Custom columns (that you or your organisation may have added to the list you are accessing)
- Custom content types (I RARELY have the luxury of dealing with anything out-of-box – most of the time, content types are customised for individual organisation needs).
This is why you need to know your data, and if you have problems finding properties, you can use the method below to help you focus in on the ones you need to access.
Let’s get started
Set your breakpoint inside your list item enumerator – either with a debugger; statement or by selecting the line number.
When you refresh your browser window, the breakpoint should be hit and will allow you to test some things out in your Watch pane.
When the breakpoint is hit inside your list enumerator, add the following to your Watch pane (by clicking the plus sign in the top-right):
listItem.get_fieldValues();
This should yield the following in your Watch pane:
You can access these properties via
listItem.get_item(KEY);
so some examples would be:
listItem.get_item("ID").toString(); // This is an INT, which we can convert to a string or leave as an INT. listItem.get_item("Author").get_lookupValue(); // This is a lookup field, so if you want the value, you use .get_lookupValue(); listItem.get_item("Title"); // This is plain text, so you can access the property directly listItem.get_item("Modified"); // This is a DateTime field, which you can format like the example below: listItem.get_item("Modified").format('dd MMMM yyyy'); // Outputs: 10 August 2016
Your Watch pane may look something like this with some of the values above:
You can also browse their properties by expanding the items with arrows.
About Author
Comments are closed