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

来源:互联网 发布:美国农业部数据网站 编辑:程序博客网 时间:2024/05/01 17:42

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

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


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


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

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


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

其中:

web = SPContext.Current.Web

site = SPContext.Current.Site

alertEnabledsite.WebApplicaiton.AlertsEnabled && site.WebApplication.IsEmailServerSetallowSilverlightPromptsite.WebApplication.AllowSilverlightPromptclientServerTimeDeltanew Date(DateTime.UtcNow.ToString("o", CultureInfor.InvariantCulture))crossDomainPhotosEnabledsite.WebApplication.CrossDomainPhotosEnabledcurrentCultureNameThread.CurrentThread.CurrentCulture.NamecurrentLanguageThread.CurrentThread.CurrentUICulture.LCIDcurrentUICultureNameThread.CurrentThread.CurrentUICulture.NamelayoutsUrlSPHttpUtility.EcmaScriptStringLiteralEncoded(HttpContextLayoutsFolder)pagePersonalizationScopeweb.WebPartManager不为null: web.WebPartManager.Scope
否则:wePersonalizationScope.SharedserverRequestPathSPHttpUtility.EcmaScriptStringLiteralEncode(HttpContext.Current.Request.Path)siteAbsoluteUrlSPHttpUtility.EcmaScriptStringLiteralEncode(web.Site.Url)siteClientTagweb.SiteClientTag.ToString(CultureInfo.InvariantCulture)siteServerRelativeUrlSPHttpUtility.EcmaScriptStringLiteralEncode(site.ServerRelativeUrl)systemUserKeySPHttpUtility.EcmaScriptStringLiteralEncode(web.CurrentUser.SystemUserKey)tenantAppVersionSPHttpUtility.EcmaScriptStringLiteralEncode(SPTenantAppUtils.GetTenantAppEtag(web))userIdweb.CurrentUser.ID.ToString(CultureInfo.InvariantCulture)webAbsoluteUrlSPHttpUtility.EcmaScriptStringLiteralEncode(web.Url)webLanguageweb.LanguagewebLogoUrlscriptLiteralToEncode = string.IsNullOrEmpty(web.SiteLogoUrl) ? (SPUtility.GetLayoutsFolder(web) + "/images/siteicon.png") : web.SiteLogoUrl;
SPHttpUtility.EcmaScriptStringLiteralEncode(scriptLiteralToEncode)webServerRelativeUrlSPHttpUtility.EcmaScriptStringLiteralEncode(web.ServerRelativeUrl)webTemplate如果有open的权限:
SPHttpUtility.EcmaScriptStringLiteralEncode(web.WebTemplateId.ToString(CultureInfo.InvariantCulture))
否则是空值webTitle如果有open的权限:SPHttpUtility.EcmaScriptStringLiteralEncode(web.Title) 否则是空值webUIVersionweb.UIVersion.ToString(CultureInfo.InvariantCulture)webPermMasksSPUtility.BasePermissionsToJson(web.EffectiveBasePermissions)  


0 0