Sıra geldi plug'inimizi yazmaya.Bu plug'in sayesinde CRM'in internal pipeline 'ına entegre olarak sistemde gerçekleşen istediğimiz event'i yakalayıp akabininde kullanıcı bazlı bu değeri alarak gridlerde sayfa bazında gösterilecek olan kayıt sayısını set edebileceğiz.Geriye kalan en önemli unsur handle ettiğimiz bu integer değere göre her grid için oluşturulan fetchXML bilgisini editleyerek istediğimiz şekilde bir görünüm oluşturma işlemini tamamlamak..
17 using System;
18 using System.Collections.Generic;
19 using System.Text;
20 using Microsoft.Crm.Sdk;
21 using System.Xml;
22 using Microsoft.Crm.SdkTypeProxy;
23 using Microsoft.Crm.Sdk.Query;
24
25 namespace RecordPerPage
26 {
27 public class PageRecordCountUtility : IPlugin
28 {
29 #region IPlugin Members
30
31 public void Execute(IPluginExecutionContext context)
32 {
33 if (context.MessageName == "Execute" && context.InputParameters.Contains("FetchXml"))
34 {
35 //crmservice bilgisi context üzerinden oluşturuluyor.
36 ICrmService crmservice = context.CreateCrmService(true);
37
38 //Login olan kullanıcımız için sayfa başına gösterilecek olan kayıt sayısı bilgisini
39 //DynamicEntity üzerinden sorgulayacağız.
40 TargetRetrieveDynamic target = new TargetRetrieveDynamic();
41 target.EntityId = context.UserId;
42 target.EntityName = EntityName.systemuser.ToString();
43
44 RetrieveRequest request = new RetrieveRequest();
45 request.ColumnSet = new ColumnSet(new string[] { "new_pagerecordcount" });
46 request.Target = target;
47 request.ReturnDynamicEntities = true;
48
49 //İlgili kaydı retrieve ediyoruz.
50 DynamicEntity currentuser = (DynamicEntity)((RetrieveResponse)crmservice.Execute(request)).BusinessEntity;
51
52 //Eğer new_pagerecordcount'ı bulunamazsa bizim için bir anlamı yok ,
53 //alanın varlığı kontrol ediliyor.
54 if (currentuser.Properties.Contains("new_pagerecordcount"))
55 {
56 //Var olan request'e ait XML query'sini editlemek için handle ediyoruz
57 XmlDocument indoc = new XmlDocument();
58 indoc.LoadXml((string)context.InputParameters["FetchXml"]);
59
60 //VOLA!count attribute olmazsa olmaz!bu değeri arıyoruz.
61 if (indoc.DocumentElement.Attributes["count"] != null)
62 {
63 //Bulabildiysek kullanıcı için tanımlanan değer üzerinden dinamik olarak set ediyoruz.
64 indoc.DocumentElement.Attributes["count"].Value = ((CrmNumber)currentuser["new_pagerecordcount"]).Value.ToString();
65 //editlediğimiz FetchXML değerini editleyerek context'i set ediyoruz.Burası önemli.
66 context.InputParameters["FetchXml"] = indoc.OuterXml;
67 //yukarıdaki değeri set etmezseniz yapılan bir işe yaramayacaktır!
68 }
69 }
70 }
71 }
72 #endregion
73 }
74 }
Yukarıda açıklamalarıyla birlikte yazdığımız plug'ini olabildiğince ifade etmeye çalıştım.Geriye kalan adımımıız bu plugini crmserver'a register etmek.Bunun normal yolu oldukça meşakatli fakat Plugin Registration Tool projesini internetten indirip test server'inizda bir kez derleyip çalıştırdığınızda bu adımların aslında çok rahat yapılabildiğini görebiliyoruz.Projeyi http://code.msdn.microsoft.com/crmplugin/Release/ProjectReleases.aspx?ReleaseId=2010 linkinden indirebilirsiniz..