2012-01-10 84 views
3

我有一個與當前記錄n:n關係的子網格。如何將過濾視圖添加到功能區「添加現有」按鈕

我想添加一個過濾的視圖到這個子網格的「Add Existing」按鈕。

有什麼想法?

(我跟着這篇文章是完全一樣的我的要求:http://danielcai.blogspot.com/2011/12/filtered-lookup-for-existing-button-of.html

+1

爲了節省一些時間,您可以下載可視化功能區編輯器並跳過一些XML樂趣:) – user1231231412 2012-01-10 14:05:37

+0

@JonC:無法忽略Renaud在下面展示的內容 - 這超出了我所知的任何現有可視化編輯器的功能。 – 2012-01-11 17:39:51

+0

正確。它不能單獨做這一切。它可以用來添加一個按鈕,設置它的屬性,設置它調用的方法,它需要的參數,顯示和啓用規則。 – user1231231412 2012-01-11 18:06:49

回答

5

首先,你要導出包含的實體類型要過濾的解決方案:

定製.XML找到RibbonDiffXml節點,並添加以下代碼:

<RibbonDiffXml> 
    <CustomActions /> 
    <Templates> 
     <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates> 
    </Templates> 
    <CommandDefinitions /> 

而在該個CommandDefinitions節點,補充一點:

<CommandDefinitions> 
    <CommandDefinition Id="Mscrm.AddExistingRecordFromSubGridAssociated"> 
    <EnableRules> 
     <EnableRule Id="Mscrm.AppendToPrimary" /> 
     <EnableRule Id="Mscrm.EntityFormIsEnabled" /> 
    </EnableRules> 
    <DisplayRules> 
     <DisplayRule Id="Mscrm.AddExisting" /> 
     <DisplayRule Id="Mscrm.ShowForManyToManyGrids" /> 
     <DisplayRule Id="Mscrm.AppendToPrimary" /> 
     <DisplayRule Id="Mscrm.AppendSelected" /> 
     <DisplayRule Id="Mscrm.CanWriteSelected" /> 
    </DisplayRules> 
    <Actions> 
     <JavaScriptFunction FunctionName="addExistingCustomFilter" Library="$webresource:new_yourLibrary"> 
     <CrmParameter Value="SelectedEntityTypeCode" /> 
     <CrmParameter Value="SelectedControl" /> 
     <CrmParameter Value="PrimaryEntityTypeName" /> 
     </JavaScriptFunction> 
    </Actions> 
    </CommandDefinition> 
    <CommandDefinition Id="Mscrm.AddExistingRecordFromSubGridStandard"> 
    <EnableRules> 
     <EnableRule Id="Mscrm.AppendToPrimary" /> 
     <EnableRule Id="Mscrm.EntityFormIsEnabled" /> 
    </EnableRules> 
    <DisplayRules> 
     <DisplayRule Id="Mscrm.AddExisting" /> 
     <DisplayRule Id="Mscrm.ShowForManyToManyGrids" /> 
     <DisplayRule Id="Mscrm.AppendToPrimary" /> 
     <DisplayRule Id="Mscrm.AppendSelected" /> 
     <DisplayRule Id="Mscrm.CanWriteSelected" /> 
    </DisplayRules> 
    <Actions> 
     <JavaScriptFunction FunctionName="addExistingCustomFilter" Library="$webresource:new_yourLibrary"> 
     <CrmParameter Value="SelectedEntityTypeCode" /> 
     <CrmParameter Value="SelectedControl" /> 
     <CrmParameter Value="PrimaryEntityTypeName" /> 
     </JavaScriptFunction> 
    </Actions> 
    </CommandDefinition> 
</CommandDefinitions> 

代碼來自您可以在CRM 2011 SDK發現並已被修改爲調用從自定義JavaScript庫的自定義功能的XML文件。

然後,在庫屬性中創建具有上面指定名稱的新JS庫。

添加第一個泛型函數:

/*****************************************/ 
/*          */ 
/*  Add Custom View To Subgrid  */ 
/*          */ 
/*****************************************/ 
function addExistingFromSubGridCustom(params) { 

    var relName = params.gridControl.getParameter("relName"), 
     roleOrd = params.gridControl.getParameter("roleOrd"), 
     viewId = "{00000000-0000-0000-0000-000000000001}"; // a dummy view ID 

    var customView = { 
     fetchXml: params.fetchXml, 
     id: viewId, 
     layoutXml: params.layoutXml, 
     name: params.name, 
     recordType: params.gridTypeCode, 
     Type: 0 
    }; 

    var lookupItems = LookupObjects(null, "multi", params.gridTypeCode, 0, null, "", null, null, null, null, null, null, viewId, [customView]); 
    if (lookupItems && lookupItems.items.length > 0) { 
     AssociateObjects(crmFormSubmit.crmFormSubmitObjectType.value, crmFormSubmit.crmFormSubmitId.value, params.gridTypeCode, lookupItems, IsNull(roleOrd) || roleOrd == 2, "", relName); 
    } 
} 

最後,加應由按鈕來調用的函數:

function addExistingCustomFilter(gridTypeCode, gridControl, primaryEntityName) { 

// Here you can specify for which entity the filter should be applied. 
// For example, filter only when you try to add an existing record to a client. 
// In the other cases, you will call the default method. 
    if (primaryEntityName != "client") { 
     Mscrm.GridRibbonActions.addExistingFromSubGridStandard(gridTypeCode, gridControl); 
     return; 
    } 

    // Add some logic to retrieve information needed to filter your view if you want to 


    //Update the fetch that will be used by the grid. 
    var fetch = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' + 
       '<entity name="...">' + 
       '<attribute name="..." />' + 
       '<filter type="and">' + 
       '<condition ... />' + 
       '</filter>' + 
       '</entity>' + 
       '</fetch>'; 
    // Add conditions to your fetch xml dynamically 

    // Call the generic method with the rights arguments. 
    addExistingFromSubGridCustom({ 
     gridTypeCode: gridTypeCode, 
     gridControl: gridControl, 
     fetchXml: fetch, 
     name: "My dynamyc custom filtered view!", 
     layoutXml: '<grid name="" object="' + gridTypeCode + '" jump="all_name" select="1" icon="1" preview="0">' + 
       // Provide a layout xml ... 
       '</grid>' 
    }); 
} 

發佈一切,這應該是好!

+1

嗨Renaud,謝謝你的回覆。我按照步驟操作,然後現在缺少「添加現有」按鈕。我認爲是顯示規則問題。你能幫我嗎? – nixjojo 2012-01-11 03:15:45

+1

我用''替換''並且它可以工作。 – nixjojo 2012-01-11 07:18:10

+1

啊,的確,這是一個多對多的關係。我在我的答案中替換了它。 ;) – 2012-01-11 08:11:07