I'm not sure if this forum is also for posting solutions instead of a question but I will just do it (sorry I have no blog) ;)
It took me many hours to figure out how this can work and I finally have a solution for that:
So if anyone has the same problems in CRM 2016 this could be the solution for you.
Please feel free to adjust the code if there is a better way to do it.
2. maybe adjust the ACTIVITY_VIEWID of the view (if it's different to mine).
3. add onloadFilterActivity to your onload-function.
var ACTIVITY_VIEWNAME = "Alle Aktivitäten";
var ACTIVITY_VIEWID = "{00000000-0000-0000-00AA-000010001902}";
var ACTIVITY_DATEFILTER = "All";
function onloadFilterActivity() {
filterAllActivities();
}
function filterAllActivities() {
//wait for the activity-iframe to be injected on the form.
var observer = new MutationObserver(function(mutations) {
mutations.forEach(
function(mutation) {
if (!mutation.addedNodes)
return;
for (var i = 0; i < mutation.addedNodes.length; i++) {
var node = mutation.addedNodes[i];
if (node.children && node.children.areaActivitiesFrame) {
processFilterAll("areaActivitiesFrame"); //iframe found. lets set the filter...
observer.disconnect();
}
}
});
});
//observer-configuration
observer.observe(window.parent.document.body, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});
}
//will return the document of the iframe depending of the browser-support
function getFrameDocument(iframe) {
return (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
}
function getGridPrefix() {
var entityName = Xrm.Page.data.entity.getEntityName();
var entity = entityName.charAt(0).toUpperCase() + entityName.substr(1);
return "crmGrid_" + entity + "_ActivityPointers";
}
function getOptionSetName() {
return getGridPrefix() + "_datefilter";
}
function getSavedQuerySelectorName() {
return getGridPrefix() + "_SavedNewQuerySelector";
}
function getOptionSet(iframe, name) {
var doc = getFrameDocument(iframe);
return doc.getElementById(name);
}
function processFilterAll(iframeName) {
//this will take the top-iframe where the whole page is included.
var topFrame = top.document.getElementsByTagName("iframe")[0];
var topFrameDoc = getFrameDocument(topFrame);
//this will take the activity-iframe from the top-iframe
var iframe = topFrameDoc.getElementById(iframeName);
if (!iframe) {//not there? wait 500ms and check again
setTimeout(function () { processFilterAll(iframeName) }, 500);
return;
}
//get the document of the activity-IFrame
var iframeDoc = getFrameDocument(iframe);
//get the optionSet to set it to "All"
var optionSet = iframeDoc.getElementById(getOptionSetName());
if (!optionSet) {//optionSet not there? wait 500ms and check again
setTimeout(function () { processFilterAll(iframeName) }, 500);
return;
}
//wait till the optionSet is ready to modify
$(optionSet).ready(function () {
//only set the "ALL" filter. No Change-Events needed because we will set the View, too.
optionSet.value = ACTIVITY_DATEFILTER;
//this will set the View (above the DateFilter-OptionSet)
setSelectorView(iframeDoc);
});
}
function setSelectorView(iframeDoc) {
//Get the control-object of the Grid
var control = iframeDoc.getElementById(getSavedQuerySelectorName()).control;
//Get the selectionControl from the GridControl
var selectionControl = control.get_selectionControl();
if (!selectionControl) { //selectionControl not there? wait 200ms and check again.
setTimeout(function () {
console.log("selectionControl is null.");
setSelectorView(iframeDoc);
}, 200);
return;
}
//Set the datefilter-Property of the grid to "ALL"
selectionControl.SetParameter("datefilter", ACTIVITY_DATEFILTER);
//Call Function to set the new View ("All Activities")
control.setViewForNewSavedQuerySelector(ACTIVITY_VIEWNAME, ACTIVITY_VIEWID, 1039);
//this will refresh the grid and does some other internal magic.
iframeDoc.defaultView.handleView(null, selectionControl);
}
//To be sure that the MutationObserver will work in older browsers this is a Shime for it!
//License-Information;
// mutationobserver-shim v0.3.1 (github.com/megawac/MutationObserver.js)
// Authors: Graeme Yeates (github.com/megawac)
window.MutationObserver = window.MutationObserver || window.WebKitMutationObserver || function (r) {
function w(a) { this.g = []; this.k = a } function H(a) { (function c() { var d = a.takeRecords(); d.length && a.k(d, a); a.f = setTimeout(c, w._period) })() } function t(a) { var b = { type: null, target: null, addedNodes: [], removedNodes: [], previousSibling: null, nextSibling: null, attributeName: null, attributeNamespace: null, oldValue: null }, c; for (c in a) b[c] !== r && a[c] !== r && (b[c] = a[c]); return b } function I(a, b) {
var c = B(a, b); return function (d) {
var g =
d.length, n; b.a && c.a && A(d, a, c.a, b.d); if (b.b || b.e) n = J(d, a, c, b); if (n || d.length !== g) c = B(a, b)
}
} function A(a, b, c, d) { for (var g = {}, n = b.attributes, h, m, C = n.length; C--;) h = n[C], m = h.name, d && d[m] === r || (h.value !== c[m] && a.push(t({ type: "attributes", target: b, attributeName: m, oldValue: c[m], attributeNamespace: h.namespaceURI })), g[m] = !0); for (m in c) g[m] || a.push(t({ target: b, type: "attributes", attributeName: m, oldValue: c[m] })) } function J(a, b, c, d) {
function g(b, c, g, h, y) {
var r = b.length - 1; y = -~((r - y) / 2); for (var f, k, e; e = b.pop() ;) f =
g[e.h], k = h[e.i], d.b && y && Math.abs(e.h - e.i) >= r && (a.push(t({ type: "childList", target: c, addedNodes: [f], removedNodes: [f], nextSibling: f.nextSibling, previousSibling: f.previousSibling })), y--), d.a && k.a && A(a, f, k.a, d.d), d.c && 3 === f.nodeType && f.nodeValue !== k.c && a.push(t({ type: "characterData", target: f })), d.e && n(f, k)
} function n(b, c) {
for (var x = b.childNodes, p = c.b, y = x.length, w = p ? p.length : 0, f, k, e, l, u, z = 0, v = 0, q = 0; v < y || q < w;) l = x[v], u = (e = p[q]) && e.j, l === u ? (d.a && e.a && A(a, l, e.a, d.d), d.c && e.c !== r && l.nodeValue !== e.c && a.push(t({
type: "characterData",
target: l
})), k && g(k, b, x, p, z), d.e && (l.childNodes.length || e.b && e.b.length) && n(l, e), v++, q++) : (h = !0, f || (f = {}, k = []), l && (f[e = D(l)] || (f[e] = !0, -1 === (e = E(p, l, q, "j")) ? d.b && (a.push(t({ type: "childList", target: b, addedNodes: [l], nextSibling: l.nextSibling, previousSibling: l.previousSibling })), z++) : k.push({ h: v, i: e })), v++), u && u !== x[v] && (f[e = D(u)] || (f[e] = !0, -1 === (e = E(x, u, v)) ? d.b && (a.push(t({ type: "childList", target: c.j, removedNodes: [u], nextSibling: p[q + 1], previousSibling: p[q - 1] })), z--) : k.push({ h: e, i: q })), q++)); k && g(k, b,
x, p, z)
} var h; n(b, c); return h
} function B(a, b) { var c = !0; return function g(a) { var h = { j: a }; !b.c || 3 !== a.nodeType && 8 !== a.nodeType ? (b.a && c && 1 === a.nodeType && (h.a = F(a.attributes, function (a, c) { if (!b.d || b.d[c.name]) a[c.name] = c.value; return a })), c && (b.b || b.c || b.a && b.e) && (h.b = K(a.childNodes, g)), c = b.e) : h.c = a.nodeValue; return h }(a) } function D(a) { try { return a.id || (a.mo_id = a.mo_id || G++) } catch (b) { try { return a.nodeValue } catch (c) { return G++ } } } function K(a, b) { for (var c = [], d = 0; d < a.length; d++) c[d] = b(a[d], d, a); return c }
function F(a, b) { for (var c = {}, d = 0; d < a.length; d++) c = b(c, a[d], d, a); return c } function E(a, b, c, d) { for (; c < a.length; c++) if ((d ? a[c][d] : a[c]) === b) return c; return -1 } w._period = 30; w.prototype = {
observe: function (a, b) {
for (var c = { a: !!(b.attributes || b.attributeFilter || b.attributeOldValue), b: !!b.childList, e: !!b.subtree, c: !(!b.characterData && !b.characterDataOldValue) }, d = this.g, g = 0; g < d.length; g++) d[g].m === a && d.splice(g, 1); b.attributeFilter && (c.d = F(b.attributeFilter, function (a, b) { a[b] = !0; return a })); d.push({
m: a, l: I(a,
c)
}); this.f || H(this)
}, takeRecords: function () { for (var a = [], b = this.g, c = 0; c < b.length; c++) b[c].l(a); return a }, disconnect: function () { this.g = []; clearTimeout(this.f); this.f = null }
}; var G = 1; return w
}(void 0);
if you have Trouble (like I had) using my solution in Internet Explorer you could use the solution below which doesn't use MutationObserver
var ACTIVITY_VIEWNAME = "Alle Aktivitäten";
var ACTIVITY_VIEWID = "{00000000-0000-0000-00AA-000010001902}";
var ACTIVITY_DATEFILTER = "All";
function onloadFilterActivity() {
if (Xrm.Page.context.client.getClient().toLowerCase() == "mobile") {//bei mobile-client nichts machen.
return;
}
filterAllActivities();
}
function filterAllActivities() {
var frames = top.document.getElementsByTagName("iframe");
var found = false;
for (var i = 0; i < frames.length; i++) {
if (frames[i].id.indexOf("content") < 0)
continue;
var topFrame = frames[i];
var doc = getFrameDocument(topFrame);
var iframe = doc.getElementById("areaActivitiesFrame");
if (iframe) {
found = true;
processFilterAll(iframe);
break;
}
}
if (!found) {
setTimeout(function() {
filterAllActivities();
}, 500);
return;
}
}
//will return the document of the iframe depending of the browser-support
function getFrameDocument(iframe) {
return (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
}
function getGridPrefix() {
var entityName = Xrm.Page.data.entity.getEntityName();
var entity = entityName.charAt(0).toUpperCase() + entityName.substr(1);
return "crmGrid_" + entity + "_ActivityPointers";
}
function getOptionSetName() {
return getGridPrefix() + "_datefilter";
}
function getSavedQuerySelectorName() {
return getGridPrefix() + "_SavedNewQuerySelector";
}
function getOptionSet(iframe, name) {
var doc = getFrameDocument(iframe);
return doc.getElementById(name);
}
function processFilterAll(iframe) {
//get the document of the activity-IFrame
var iframeDoc = getFrameDocument(iframe);
if (Xrm.Page.data.entity.getEntityName() === "lead") {
setSelectorView(iframe);
return;
}
//get the optionSet to set it to "All"
var optionSet = iframeDoc.getElementById(getOptionSetName());
if (!optionSet) {//optionSet not there? wait 500ms and check again
setTimeout(function () { processFilterAll(iframe) }, 500);
return;
}
//wait till the optionSet is ready to modify
$(optionSet).ready(function () {
//only set the "ALL" filter. No Change-Events needed because we will set the View, too.
optionSet.value = ACTIVITY_DATEFILTER;
//this will set the View (above the DateFilter-OptionSet)
setSelectorView(iframe);
});
}
function setSelectorView(iframe) {
//Get the control-object of the Grid
var iframeDoc = getFrameDocument(iframe);
var selectorElement = iframeDoc.getElementById(getSavedQuerySelectorName());
if (!selectorElement) {
setTimeout(function () {
console.log("selectionControl is null.");
setSelectorView(iframe);
}, 200);
return;
}
var control = selectorElement.control;
//Get the selectionControl from the GridControl
var selectionControl = control.get_selectionControl();
if (!selectionControl) { //selectionControl not there? wait 200ms and check again.
setTimeout(function () {
console.log("selectionControl is null.");
setSelectorView(iframe);
}, 200);
return;
}
//Set the datefilter-Property of the grid to "ALL"
selectionControl.SetParameter("datefilter", ACTIVITY_DATEFILTER);
//Call Function to set the new View ("All Activities")
control.setViewForNewSavedQuerySelector(ACTIVITY_VIEWNAME, ACTIVITY_VIEWID, 1039);
//this will refresh the grid and does some other internal magic.
iframeDoc.defaultView.handleView(null, selectionControl);
}