1

そのため、SharePoint の表示テンプレートを作成していますが、さまざまなリストやそのアイテムなどにアクセスするために必要になるため、コンテキストを取得するのに問題があります。検索中に見た方法の 1 つは次のとおりです。

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>
4

1 に答える 1

2

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.executeFuncfunctionを利用できます。

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()); 
        });
 });
于 2016-02-25T19:45:11.617 に答える