8

Recently I came across the variable ctx (link) in SharePoint which allows you to get the name of current list/library without any fuss. Apparently the ctx variable has quite few more tricks up its sleeve. It can return values like, GUID of list/library, site title, no. of currently selected items and more.

But I cannot find any MSDN documentation for this variable. Is the use of this variable supported by Microsoft? I am currently developing on SP 2010 so I can verify that it works there. But what about SP 2013? Also with every example for JSOM starting with var ctx = SP.ClientContext.get_current(); we are bound to override this useful variable.

  • 1
    var ctx inside a function will create a locally scoped variable, not overriding ctx in the global scope. – Danny '365CSI' Engelman Dec 29 '15 at 8:56
9

ctx is a global variable. It belongs in the list view pages.

var ctx = SP.ClientContext.get_current();

So if you use above line in any list view pages, it will cause an issue. The value of ctx will be overridden by SP.ClientContext.get_current();. You will not get any info from ctx after above line.

In SharePoint 2013, it works and widely using in CSR. It has more info than in 2010. Following things we can get from ctx in SharePoint 2013

["listBaseType", "listTemplate", "listName", "view", "listUrlDir", "HttpPath", "HttpRoot", "serverUrl", "imagesPath", "PortalUrl", "RecycleBinEnabled", 
 "enteringGridMode", "inGridMode", "isWebEditorPreview", "rootFolderForDisplay", "isPortalTemplate", "isModerated", "recursiveView", "displayFormUrl", 
 "editFormUrl", "newFormUrl", "ctxId", "CurrentUserId", "isForceCheckout", "EnableMinorVersions", "ModerationStatus", "verEnabled", "isVersions", 
 "WorkflowsAssociated", "ExternalDataList", "HasRelatedCascadeLists", "CascadeDeleteWarningMessage", "ContentTypesEnabled", "SendToLocationName", 
 "SendToLocationUrl", "StateInitDone", "TotalListItems", "CurrentSelectedItems", "LastSelectableRowIdx", "SelectAllCbx", "TableCbxFocusHandler", "TableMouseoverHandler",
 "onItemSelectionChangedHandlers", "wpq", "Templates", "ListData", "ListSchema", "BaseViewID", "ListTemplateType", "existingServerFilterHash", "noGroupCollapse", 
 "NavigateForFormsPages", "BasePermissions", "CurrentUserIsSiteAdmin", "IsAppWeb", "AllowGridMode", "rootFolder", "viewTitle", "NoScriptEnabled", "OfficialFileName", 
 "OfficialFileNames", "WriteSecurity", "SiteTitle", "ListTitle", "isXslView", "IsClientRendering", "RegionalSettingsTimeZoneBias", "CanShareLinkForNewDocument", "SiteTemplateId", 
 "bInitialRender", "ListDataJSONItemsKey", "ControlMode", "SiteClientTag", "CurrentLanguage", "CurrentCultureName", "CurrentUICultureName", "OnPreRender", "OnPostRender", 
 "canDragUpload", "RenderView", "RenderHeader", "RenderBody", "RenderFooter", "RenderGroups", "RenderItems", "RenderFields", "RenderFieldByName", "heroId", "CurrentItem", 
 "CurrentItemIdx", "CurrentFieldSchema", "fHidden", "clvp", "queryString"]
  • Total number of properties of ctx in 2010 is 49
  • Total number of properties of ctx in 2013 is 94

If you have multiple lists in a page, then you will have multiple ctx.

enter image description here

I have two lists in a page. My ctx's are ctx37 and ctx73

PS: Unfortunately, It is not documented by MSDN. The all we have for exploring it is: Browser's console.

  • 5
    note that ctx has different content when the page is being built. Javascript variables are passed by reference so the ctx objects you check (after the whole page was loaded) is the Final ctx object. If you want to play sleuth use console.log(ObjectUtil.deepCopy(ctx)) inside called function to see what ctx was at time of execution. (**) – Danny '365CSI' Engelman Dec 29 '15 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.