SharePoint - 显示模板 - 访问客户端上下文对象
SharePoint - Display Templates - Accessing the Client Context Object
所以我正在为 SharePoint 创建显示模板,但我在获取上下文时遇到问题,因为我需要它来访问不同的列表及其项目等。 我在搜索时看到的一种方法是:
var context = Srch.ScriptApplicationManager.get_clientRuntimeContext();
问题是我不知道如何从这个对象访问内容。 每次我收到上下文未定义或为空的错误,或者我打印出它的文字函数时。 在其他程序(不是显示模板)中,我只会使用:
var context = new ClientContext();
或
var context = new SP.ClientContext();
或一些变体,但在这种情况下,我找不到使用第一个的任何文档或示例。 只是(大部分)博客说你可以用它来获取上下文。
我现在拥有的代码大部分都被注释掉了。 我现在只是想弄清楚这个背景。提前谢谢。
编辑:
这是整个(项目显示模板)文件:
<html lang="en" xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<head>
<title>Marketing Page Item Template</title>
<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>
<mso:MasterPageDescription msdt:dt="string">This is the item display template for the Marketing Page tasks. This will organize list/items under its practice.</mso:MasterPageDescription>
<mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId>
<mso:TargetControlType msdt:dt="string">;#SearchResults;#;#Content Web Parts;#</mso:TargetControlType>
<mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>
<mso:ManagedPropertyMapping msdt:dt="string">'Title'{Title}:'Title','Assigned To'{Assigned To}:'AssignedTo','Due Date'{Due Date}:'DueDateOWSDATE;DueDate','URL'{URL}:'URL'</mso:ManagedPropertyMapping msdt:dt="string">
</mso:CustomDocumentProperties>
</xml><![endif]-->
</head>
<body>
<div>
<!--#_
var siteURL = _spPageContextInfo.siteAbsoluteUrl;
var title = $getItemValue(ctx, "Title");
var assignedTo = $getItemValue(ctx, "Assigned To");
var dueDate = $getItemValue(ctx, "Due Date");
var listUrl = $getItemValue(ctx, "URL");
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
var context = Srch.ScriptApplicationManager.get_clientRuntimeContext();
var reqCtx = SP.RequestContext.getCurrent(context);
var web = reqCtx.get_web();
var pagesListId = SP.PageContextInfo.get_pageListId();
var list = web.get_lists().getById(pagesListId);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
context.load(items);
context.executeQueryAsync(
function(){
items.get_data().forEach(function(item){
console.log(item.get_item('FileRef'));
});
},
function(sender,args){
console.log(args.get_message());
});
});
_#-->
<li>
<div style="background-color: honeydew; margin: 5px; padding: 5px;">
<!--#_
if (!title.isEmpty)
{
_#-->
<h3 style="color: coral;">Title: _#= $htmlEncode(title) =#_</h3>
<!--<p>_#= $htmlEncode(ctx.CurrentItem.Title) =#_</p>-->
<p>URL: _#= $htmlEncode(listUrl) =#_</p>
<p>URL: _#= $htmlEncode(typeof reqCtx) =#_</p>
<!--#_
}
if (!assignedTo.isEmpty)
{
_#-->
<p style="color: goldenrod;">Assigned To: _#= $htmlEncode(assignedTo) =#_</p>
<!--<p>_#= $htmlEncode(ctx.CurrentItem.AssignedTo) =#_</p>-->
<!--#_
}
else
{
_#-->
<p>There is no assigned person!</p>
<!--#_
}
if (!dueDate.isEmpty)
{
_#-->
<p style="color: rosybrown;">Due Date: _#= $htmlEncode(dueDate) =#_</p>
<!--<p>_#= $htmlEncode(ctx.CurrentItem.DueDateOWSDATE) =#_</p>-->
<!--#_
}
else
{
_#-->
<p>There is no due date!</p>
<!--#_
}
_#-->
</div>
</li>
</div>
</body>
</html>
Srch.ScriptApplicationManager.get_clientRuntimeContext
函数返回SP.ClientRuntimeContext
表示the runtime context for accessing data from and invoking methods on remote objects
的对象
下面的示例演示如何在显示模板中检索列表项和打印页面 URL:
var context = Srch.ScriptApplicationManager.get_clientRuntimeContext();
var reqCtx = SP.RequestContext.getCurrent(context);
var web = reqCtx.get_web();
var pagesListId = SP.PageContextInfo.get_pageListId(); //Pages List Id
var list = web.get_lists().getById(pagesListId);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
context.load(items);
context.executeQueryAsync(
function(){
items.get_data().forEach(function(item){
console.log(item.get_item('FileRef'));
});
},
function(sender,args){
console.log(args.get_message());
});
为了确保SP.ClientRuntimeContext
对象已加载,您可以使用SP.SOD.executeFunc
函数,例如:
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
var context = Srch.ScriptApplicationManager.get_clientRuntimeContext();
//the remaining code goes here...
});
选项 2.使用SP.ClientContext
类
下面的示例演示如何在显示模板中使用SP.ClientContext class
:
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
var context = SP.ClientContext.get_current();
var web = context.get_web();
var pagesListId = SP.PageContextInfo.get_pageListId();
var list = web.get_lists().getById(pagesListId);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
context.load(items);
context.executeQueryAsync(
function(){
items.get_data().forEach(function(item){
console.log(item.get_item('FileRef'));
});
},
function(sender,args){
console.log(args.get_message());
});
});
相关文章:
- 客户端服务器REST API captcha实现
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 如何使用Socket.io将命令从客户端发送到服务器
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 从客户端获取修改后的对象,并将其与服务器上的原始对象组合
- 如何将我的javascript库公开给其他客户端使用
- 是否可以使用JavaScript/AAJAX在客户端创建一个文件
- 如何使用Javascript客户端对象模型检索Sharepoint 2010列表项权限
- 返回/从Twit's客户端.get
- 将客户端特定的日期格式返回到服务器MVC4
- MobileFirst:在客户端运行计时器作业-最佳选项
- 标签客户端的设置值
- 我的客户端选项是什么
- Node.js上的WebSocket,并在所有连接的客户端之间共享消息
- 使用javascript在客户端上使用Web服务
- 通过ajax从客户端调用C#方法来执行C#方法
- SharePoint - 显示模板 - 访问客户端上下文对象
- 禁用CKEditor上下文菜单客户端
- Reactjs:在带有上下文的客户端上呈现原始html