SharePoint 页面JS上下文对象 "_spPageContextInfo"

SharePoint在2010和2013的版本中,注册了一个jS对象“_spPageContextInfo”,在浏览器(Chrome或者IE)中打开SharePoint站点,按F12,在调试控制台中直接输入“_spPageContextInfo”,注意区分大小写,可以列出这个对象包含的一些属性,都是很有用的属性。

在SharePoint 2010的环境中是(使用IE浏览器的截图):

SharePoint 页面JS上下文对象 "_spPageContextInfo"_第1张图片

在SharePoint 2013的环境中添加了几个属性(使用Chrome浏览器的截图):

SharePoint 页面JS上下文对象 "_spPageContextInfo"_第2张图片

这些属性是很方便使用的,例如获取当前用户的id,当前站点使用的语言,culture,url等等。

查阅一下reflector,可以知道这个对象是通过SharePointClientJs_Register方法注册进来的:

SharePoint 页面JS上下文对象 "_spPageContextInfo"_第3张图片

从代码可以看到各个属性对应的具体的值是如何取得的(以SharePoint 2013为例):

其中:

web = SPContext.Current.Web

site = SPContext.Current.Site

alertEnabled site.WebApplicaiton.AlertsEnabled && site.WebApplication.IsEmailServerSet
allowSilverlightPrompt site.WebApplication.AllowSilverlightPrompt
clientServerTimeDelta new Date(DateTime.UtcNow.ToString("o", CultureInfor.InvariantCulture))
crossDomainPhotosEnabled site.WebApplication.CrossDomainPhotosEnabled
currentCultureName Thread.CurrentThread.CurrentCulture.Name
currentLanguage Thread.CurrentThread.CurrentUICulture.LCID
currentUICultureName Thread.CurrentThread.CurrentUICulture.Name
layoutsUrl SPHttpUtility.EcmaScriptStringLiteralEncoded(HttpContextLayoutsFolder)
pagePersonalizationScope web.WebPartManager不为null: web.WebPartManager.Scope
否则:wePersonalizationScope.Shared
serverRequestPath SPHttpUtility.EcmaScriptStringLiteralEncode(HttpContext.Current.Request.Path)
siteAbsoluteUrl SPHttpUtility.EcmaScriptStringLiteralEncode(web.Site.Url)
siteClientTag web.SiteClientTag.ToString(CultureInfo.InvariantCulture)
siteServerRelativeUrl SPHttpUtility.EcmaScriptStringLiteralEncode(site.ServerRelativeUrl)
systemUserKey SPHttpUtility.EcmaScriptStringLiteralEncode(web.CurrentUser.SystemUserKey)
tenantAppVersion SPHttpUtility.EcmaScriptStringLiteralEncode(SPTenantAppUtils.GetTenantAppEtag(web))
userId web.CurrentUser.ID.ToString(CultureInfo.InvariantCulture)
webAbsoluteUrl SPHttpUtility.EcmaScriptStringLiteralEncode(web.Url)
webLanguage web.Language
webLogoUrl scriptLiteralToEncode = string.IsNullOrEmpty(web.SiteLogoUrl) ? (SPUtility.GetLayoutsFolder(web) + "/images/siteicon.png") : web.SiteLogoUrl;
SPHttpUtility.EcmaScriptStringLiteralEncode(scriptLiteralToEncode)
webServerRelativeUrl SPHttpUtility.EcmaScriptStringLiteralEncode(web.ServerRelativeUrl)
webTemplate 如果有open的权限:
SPHttpUtility.EcmaScriptStringLiteralEncode(web.WebTemplateId.ToString(CultureInfo.InvariantCulture))
否则是空值
webTitle 如果有open的权限:SPHttpUtility.EcmaScriptStringLiteralEncode(web.Title) 否则是空值
webUIVersion web.UIVersion.ToString(CultureInfo.InvariantCulture)
webPermMasks SPUtility.BasePermissionsToJson(web.EffectiveBasePermissions)
   



你可能感兴趣的:(SharePoint)