2016-04-11 5 views
0

タブとコンテンツパネルはほとんどありません。エンドユーザー(EU)はタブをクリックし、パネルのコンテンツは非同期に変更されます。Sitecore ServicesクライアントのItemServiceの使用方法

例:

<ul class="products"> 
    <li class="p1">product1</li> 
    <l1>product2</li> 
</ul> 
<div class="product-data"> 
    NAME : <span> <product name here> </span> 
    COLOR : <span> <product color here> </span> 
</div> 

<script> 
    (function ($) { 
     $(".p1").click(function(){ 
      HelloWorld(); 
     }); 

     function HelloWorld() { 
      $.ajax({ 
       type: "POST", 
       url: 'http://mysite/Services/myService.asmx/HelloWorld', 
       data: "pid:" + someId, 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (response) { 
        alert(response.d); 
       }, 
       failure: function (response) { 
        alert("ERR-" + response.d); 
       } 
      }); 
     } 
    })(jQuery);  

</script> 

各製品は、コンテンツツリー内のアイテムです。 EUがタブをクリックすると、その製品のデータを取得してパネルにバインドするajax呼び出しが行われます。私は、Webサービスを使用してこの機能を実現し、フロントエンドにJSONを返すようにしようとしている

:私はこれはと行う必要があります理解して当然

[WebMethod] 
public string HelloWorld(string pid) 
{ 
    //get a certain product details and return it as JSON 
    Sitecore.Data.Items.Item newItem = Sitecore.Context.Item; 

    if (newItem != null) 
    return newItem.Name; 
    else 
    return "it was null";     
    } 

を、結果は「それがnullでした」アイテムサービスSSCの、しかし、適切な/初心者の例を見つけることができません。

ASP.NET

+0

についてはこちらを参照してください。正確に達成しようとしている。あなたは解決する方法がわからないという問題は何ですか? –

答えて

2

とサイトコア8を使用してあなたはEntity Service代わりのSitecore.Services.ClientItem Service使用したいと思うでしょう。これにより、各製品に表示するデータの種類に固有のカスタムモデルを提供できます。

まず、商品を表すクラスを作成する必要があります。 Sitecore.Services.Core.Model.EntityIdentityを実装する必要があります。

Sitecore SPEAKを使用している場合は、そのケーシングでitemIdという名前のプロパティを定義するようにしてください。

public class ProductModel : Sitecore.Services.Core.Model.EntityIdentity 
{ 
    public string itemId { get; set; } 
    public string ProductName { get; set; } 
    ... 
} 

あなたはサイトコアのベストプラクティスに従うことになるでしょうSitecore.Services.Clientを使用した開発。単純なControllerは、すべての計算をモデルのタイプのRepositoryに渡します。この場合、ProductRepositoryです。

コントローラーはのタイプのEntityServiceを実装する必要があります。

[ValidateAntiForgeryToken] 
[ServicesController] 
public class ProductController : EntityService<ProductModel> 
{ 
    public ProductController(IRepository<ProductModel> repository) 
     : base(repository) 
    { 
    } 

    public ProductController() 
     : this(new SitecoreItemRepository()) 
    { 
    } 
} 

このコントローラは、など、Repositoryの方法が追加、GetByIdをゲット公開し

public class ProductRepository : Sitecore.Services.Core.IRepository<ProductModel> 
{ 
    public ProductModel FindById(string id) 
    { 
     // code to find by id 
    } 
} 

「これは何不明であるfull example of Entity Servicehere

+0

(1)Webサービスで 'ProductRepository.FindById'を呼び出す必要があります。そうです。 (2)私のソリューションにコントローラを作成する場所はどこですか? (3)Web API2コントローラですか? – Qwerty

+0

1)コントローラは、そのメソッドをWeb要求に提供します。 /Product/findById 2)あなたのウェブアプリケーションプロジェクトのコントローラ用のフォルダです。3)空のコントローラを作成します。 –

+0

私のソリューションには「Sitecore.Services.Core」はありません。私はこれらのdllを追加しようとしましたが、運がありません - Sitecore.Services.Client、Sitecore.Services.Core、Sitecore.Services.Infrastructure、Sitecore.Services.Infrastructure.Sitecore – Qwerty