Create filtered view in MSCRM


Filtered Custom View for a lookup using MSCRM 13+

Below function works on an example where there is two fields Team and User, Users are filtered based on Team in MSCRM. You can use this code for any scenario matching to this.


function users() {
    var team = Xrm.Page.getAttribute("ccs_assignedteam").getValue() ? Xrm.Page.getAttribute("ccs_assignedteam").getValue()[0].id : null;
    if (team != null) {
        var FetchXMLuser = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
            "<entity name='systemuser'>" +
            "<attribute name='fullname' />" +
            "<attribute name='businessunitid' />" +
            "<attribute name='title' />" +
            "<attribute name='systemuserid' />" +
            "<order attribute='fullname' descending='false' />" +
            "<link-entity name='teammembership' from='systemuserid' to='systemuserid' visible='false' intersect='true'>" +
            "<link-entity name='team' from='teamid' to='teamid' alias='ac'>" +
            "<filter type='and'>" +
            "<condition attribute='teamid' operator='eq' value='" + team + "'/>" +
            "</filter>" +
            "</link-entity>" +
            "</link-entity>" +
            "</entity>" +
            "</fetch>";
        var Results1 = XrmServiceToolkit.Soap.Fetch(FetchXMLuser); //alert(Results1);
        var user = null;
        for (var indxResults = 0; indxResults & lt; Results1.length; indxResults++) {
            if (Results1[0].attributes["systemuserid"] != null) {
                user = Results1[0].attributes["systemuserid"].value; //alert(user);
            }
        }
        var viewId = "{1DFB2B35-B07C-44D1-868D-258DEEAB88E2}"; //Random GUID
        var entityName = "systemuser";
        var viewDisplayName = "Filtered Users";
        var layoutXml = "<grid name='resultset' " +
            "object='1' " +
            "jump='fullname' " +
            "select='1' " +
            "icon='1' " +
            "preview='1'>" +
            "row name='result' " +
            "id='systemuserid'>" +
            "cell name='fullname' " +
            "width='150' />" +
            "</row>" +
            "</grid>";

        Xrm.Page.getControl("ccs_assigneduser").addCustomView(viewId, entityName, viewDisplayName, FetchXMLuser, layoutXml, true);

    }
}