Skip to content

API Reference

Full reference for every method, option, and return type exposed by the ChWebSdk object on community pages.

DOM and lifecycle

onReady(callback)

Run a callback when the DOM is ready. If the document is already loaded (complete or interactive), the callback runs immediately; otherwise it runs on DOMContentLoaded.

javascript
ChWebSdk.onReady(() => {
  // Safe to use DOM and SDK
})

observeElement(selector, callback)

Wait until an element matching selector appears in the DOM, then run callback with that element. Uses a MutationObserver and disconnects after the element is found.

javascript
ChWebSdk.observeElement('#my-widget-root', (el) => {
  el.textContent = 'Widget loaded'
})

onEvent(selector, event, handler, options?)

Bind an event listener to the first element that matches selector (waiting for it via observeElement if needed). Returns an unsubscribe function to remove the listener.

ParameterTypeDescription
selectorstringCSS selector for the target element
eventstringDOM event name (e.g. 'click', 'submit')
handlerfunctionEvent handler
optionsboolean | AddEventListenerOptionsOptional; e.g. { once: true }
javascript
const unsubscribe = ChWebSdk.onEvent('.submit-btn', 'click', (ev) => {
  ev.preventDefault()
  // ...
})
// Later: unsubscribe() to remove the listener

loadScript(src, callback?)

Load an external script by URL. If the script is already present, calls callback when appropriate. Scripts are loaded with async: true.

javascript
ChWebSdk.loadScript('https://example.com/plugin.js', () => {
  console.log('Script loaded')
})

loadStyle(href)

Inject a stylesheet link. No-op if a link with the same href already exists.

javascript
ChWebSdk.loadStyle('https://example.com/widget.css')

Content API

The Content API provides typed access to community content (articles, questions, conversations, ideas, product updates), search, and actions like like, unlike, vote, and unvote.

Content types and instances

Get a content instance for a specific type. Pass an optional private topic ID when you need to perform actions (like, unlike, vote, etc.) on a specific topic.

MethodDescription
Content.Article(privateId?)Knowledge base articles
Content.Question(privateId?)Community questions
Content.Conversation(privateId?)Conversations / discussions
Content.Idea(privateId?)Ideation topics
Content.ProductUpdate(privateId?)Product updates
javascript
// Search only ideas (no privateId needed)
const ideaInstance = ChWebSdk.Content.Idea()
const ideas = await ideaInstance.search('feature request', { limit: 10 })

// Like a specific idea (privateId required)
const idea = ChWebSdk.Content.Idea(12345)
await idea.like()  // uses current logged-in user

Content instance methods

When created with a privateId, an instance supports:

MethodDescription
like(likedBy?)Like the topic. likedBy defaults to current user.
unlike(unlikedBy?)Unlike the topic.
vote(votedBy?)Vote (e.g. for ideas).
unvote(unvotedBy?)Remove vote.
likeReply(replyId, likedBy?)Like a reply.
unlikeReply(replyId, unlikedBy?)Unlike a reply.
search(query, options?)Search content of this type only.

All of these require a browser environment and, where applicable, a logged-in user (or an explicit user ID).

Content.search(query, options?)

Search across all content types (no content-type filter). Same options as the per-type search.

javascript
const results = await ChWebSdk.Content.search('help with login', {
  limit: 20,
  page: 0,
  fetchMetadata: true
})

Search options

OptionTypeDefaultDescription
querystringSearch query (required).
contentTypestring | string[]Filter by type: 'idea', 'ideation', 'article', 'question', 'discussion', 'conversation', 'event', 'productUpdate'.
limitnumber30Max results per page.
pagenumber0Page index (0-based).
fetchMetadatabooleantrueWhen true, enriches results with author, engagement, and topic metadata from the backend.
categoryNamestringFilter by community category name (top-level facet).
categoryIdsnumber[]Filter by category/forum IDs.
kbCategoryNamestringFilter by knowledge base category name.
productAreaNamestringFilter by product area name.
facetFiltersstring[][]Raw facet filters for advanced filtering.

Search result shape

Each result includes at least: id, title, url, contentType, excerpt, and optionally views, likes, votes. When fetchMetadata: true, additional fields such as firstPost, forum, numberOfViews, numberOfReplies, numberOfLikes, hasCurrentUserLiked, ideationStatus, bugStatus, etc. are populated.


User API

User.search(query)

Search users by query string. Returns an array of user objects.

Browser-only. Requires a non-empty string query.

javascript
const users = await ChWebSdk.User.search('john')

User.getUsersById(ids)

Fetch users by an array of user IDs. Returns an object keyed by user ID.

Browser-only. Requires a non-empty array of IDs.

javascript
const usersMap = await ChWebSdk.User.getUsersById(['1', '2', '3'])
const user1 = usersMap['1']

User object shape

Each user includes: id, url, name, avatar, userTitle, userLevel, badges, isBanned, and optional rank (with name, color, icon, etc.).


Subscription API

All subscription methods are browser-only and require valid identifiers.

Topics

MethodDescription
Subscription.subscribeTopic(publicId)Subscribe to a topic by its public ID.
Subscription.unsubscribeTopic(publicId)Unsubscribe from a topic.
Subscription.getTopicSubscriptionStatus(publicId)Returns true if the current user is subscribed.

Categories

MethodDescription
Subscription.subscribeToCategory(categoryId)Subscribe to a category (ID as string).
Subscription.unsubscribeFromCategory(categoryId)Unsubscribe from a category.
Subscription.getCategorySubscriptionStatus(categoryId)Returns true if the current user is subscribed to the category.
javascript
await ChWebSdk.Subscription.subscribeTopic('topic-123')
const isSubscribed = await ChWebSdk.Subscription.getTopicSubscriptionStatus('topic-123')

await ChWebSdk.Subscription.subscribeToCategory('42')
const subscribedToCategory = await ChWebSdk.Subscription.getCategorySubscriptionStatus('42')

Error handling

SDK methods that call the backend or DOM throw on failure. Use try/catch and handle missing or invalid arguments.

javascript
try {
  const users = await ChWebSdk.User.search('john')
  console.log(users)
} catch (err) {
  console.error('User search failed:', err.message)
}

Common cases:

ScenarioCause
"can only be called in a browser environment"Method was run in Node or an environment without window.
"Search query is required" / "User IDs array is required"Missing or invalid arguments.
"privateId is required for this operation"Content action (like, vote, etc.) was called on an instance created without a private ID.
"User must be logged in or provide a userId"Like/vote/unvote used without a logged-in user or explicit ID.
HTTP/network errorsBackend request failed (e.g. subscription, search, user fetch).

Gainsight CC Developer Portal