I am developing a word add-in using word javascript api and need to get contentControl for current selection, so used parentContentControl for current selection.
**Code:**
var range = context.document.getSelection().parentContentControl;
context.load(range);
But on console it showing error:
Error: {"name":"OfficeExtension.Error","code":"GeneralException","message":"GeneralException","traceMessages":[],"debugInfo":{"errorLocation":"Range.parentContentControl"},"stack":"GeneralException: GeneralException\n at Anonymous function (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:189006)\n at pi (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:211583)\n at ht (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:211670)\n at g (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:211490)\n at l (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:210076)"}
Debug info: {"errorLocation":"Range.parentContentControl"}
If the current selection not contain any contentControl it should return NULL, but it giving error. Please advice.
Thanks.
This is a very good question and goes to one of the core concepts of the office.js technology: how do we handle nulls? The long story short is that whenever there is a possibility that a method/property returns null, we offer to flavors of that method/property:
The second flavor is available starting on the November fork (builds 16.0.7668+), so please make sure to update your clients to see this working.
So to concretely answer your question: that behavior is by design. If you want to validate existence of a content control within the selection you need to use the range.parentContentControlOrNullObject property. Then you can check if its null or not. Here is an example of how you can accomplish this:
Hope this clarifies the concept