Introduction
In this article I describe how to create Windows Store Apps for an Advanced Search in the Home Group using JavaScript. The file broker APIs can be used to filter the scope of Home Group searches so that only a specific user's shared files are searched. If your PC is part of a Home Group, then the users in your Home Group are shown below. Select the user to return all files shared by that user.
I assume you can create a simple Windows Store App using JavaScript; for more help visit Simple Windows Store Apps using JavaScript.
To start the creation of the apps, add two JavaScript pages by right-clicking on the js folder in the Solution Explorer and select Add > new item > JavaScript Page and then give an appropriate name. In the same way, add a HTML page to your project.
Write the following code in the default.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>app</title>
<link rel="stylesheet" href="//Microsoft.WinJS.1.0/css/ui-light.css" />
<script src="//Microsoft.WinJS.1.0/js/base.js"></script>
<script src="//Microsoft.WinJS.1.0/js/ui.js"></script>
<link rel="stylesheet" href="/css/default.css" />
<script src="/js/script1.js"></script>
<script src="/js/default.js"></script>
</head>
<body role="application">
<div id="rootGrid">
<div id="content">
<h1 id="featureLabel"></h1>
<div id="contentHost"></div>
</div>
</div>
</body>
</html>
Write the following code in the default.js:
(function () {
"use strict";
var appTitle = "";
var pages = [
{ url: "page.html", title: "Advanced search" }
];
function activated(eventObject) {
if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
eventObject.setPromise(WinJS.UI.processAll().then(function () {
var url = WinJS.Application.sessionState.lastUrl || pages[0].url;
return WinJS.Navigation.navigate(url);
}));
}
}
WinJS.Navigation.addEventListener("navigated", function (eventObject) {
var url = eventObject.detail.location;
var host = document.getElementById("contentHost");
host.winControl && host.winControl.unload && host.winControl.unload();
WinJS.Utilities.empty(host);
eventObject.detail.setPromise(WinJS.UI.Pages.render(url, host, eventObject.detail.state).then(function () {
WinJS.Application.sessionState.lastUrl = url;
}));
});
function ensureUnsnapped() {
var currentState = Windows.UI.ViewManagement.ApplicationView.value;
var unsnapped = ((currentState !== Windows.UI.ViewManagement.ApplicationViewState.snapped) || Windows.UI.ViewManagement.ApplicationView.tryUnsnap());
if (!unsnapped) {
WinJS.log && WinJS.log("Cannot unsnap the app application.", "app", "status");
}
return unsnapped;
}
WinJS.Namespace.define("App", {
appTitle: appTitle,
pages: pages,
ensureUnsnapped: ensureUnsnapped
});
WinJS.Application.addEventListener("activated", activated, false);
WinJS.Application.start();
})();
Write the following code in the page.html:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="/js/script.js"></script>
</head>
<body>
<div data-win-control="App.pageInput">
<button class="action" id="searchFilesUser1">
User 1
</button>
<button class="action" id="searchFilesUser2">
User 2
</button>
<button class="action" id="searchFilesUser3">
User 3
</button>
<button class="action" id="searchFilesUser4">
User 4
</button>
<br />
<br />
</div>
<div data-win-control="App.pageOutput">
<label id="searchProgress" class="progressRingText">
<progress class="win-ring withText"></progress>Searching
</label>
</div>
</body>
</html>
Write the following code in the script.js:
(function () {
"use strict";
var page = WinJS.UI.Pages.define("page.html", {
ready: function (element, options) {
document.getElementById("searchFilesUser1").addEventListener("click", AdvancedSearchClicked, false);
document.getElementById("searchFilesUser2").addEventListener("click", AdvancedSearchClicked, false);
document.getElementById("searchFilesUser3").addEventListener("click", AdvancedSearchClicked, false);
document.getElementById("searchFilesUser4").addEventListener("click", AdvancedSearchClicked, false);
document.getElementById("searchProgress").style.visibility = "hidden";
initAdvancedSearch();
}
});
function AdvancedSearchClicked(sender) {
WinJS.log && WinJS.log("", "app", "status");
document.getElementById("searchProgress").style.visibility = "visible";
var userToSearch = sender.srcElement.innerHTML;
var options = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.orderBySearchRank, ["*"]);
var outputString = "";
try {
var hg = Windows.Storage.KnownFolders.homeGroup;
hg.getFoldersAsync().done(function (users) {
users.forEach(function (user) {
if (user.name === userToSearch) {
var query = user.createFileQueryWithOptions(options);
query.getFilesAsync().done(function (files) {
if (files.size === 0) {
document.getElementById("searchProgress").style.visibility = "hidden";
outputString = "<b>No files shared by " + userToSearch + "</b>";
}
else {
document.getElementById("searchProgress").style.visibility = "hidden";
outputString = (files.size === 1) ? (files.size + " file found\n") : (files.size + " files shared by ");
outputString = outputString.concat(userToSearch, "\n");
files.forEach(function (file) {
outputString = outputString.concat(file.name, "\n");
});
}
WinJS.log && WinJS.log(outputString, "app", "status");
});
}
});
});
}
catch (e) {
document.getElementById("searchProgress").style.visibility = "hidden";
WinJS.log && WinJS.log(e.message, "app", "errror");
}
}
function initAdvancedSearch() {
var idString = "";
for (var buttonIndex = 0; buttonIndex < 4; buttonIndex++) {
idString = "searchFilesUser" + (buttonIndex + 1);
document.getElementById(idString).style.visibility = "hidden";
}
try {
var hg = Windows.Storage.KnownFolders.homeGroup;
hg.getFoldersAsync().done(function (folders) {
if (folders) {
var userCount = (folders.size < 4) ? folders.size : 4;
for (var userIndex = 0; userIndex < userCount; userIndex++) {
idString = "searchFilesUser" + (userIndex + 1);
document.getElementById(idString).innerHTML = folders.getAt(userIndex).name;
document.getElementById(idString).style.visibility = "visible";
}
}
else {
id("page4Output").innerHTML = "No homegroup users could be found. Make sure you are joined to a homegroup and there is someone sharing";
}
});
}
catch (e) {
WinJS.log && WinJS.log(e.message, "app", "error");
}
}
})();
Write the following code in the script1.js:
(function () {
var pageOutput = WinJS.Class.define(
function (element, options) {
element.winControl = this;
this.element = element;
new WinJS.Utilities.QueryCollection(element)
.setAttribute("role", "region")
.setAttribute("aria-labelledby", "outputLabel")
.setAttribute("aria-live", "assertive");
element.id = "output";
this._addOutputLabel(element);
this._addStatusOutput(element);
}, {
_addOutputLabel: function (element) {
var label = document.createElement("h2");
label.id = "outputLabel";
label.textContent = "";
element.parentNode.insertBefore(label, element);
},
_addStatusOutput: function (element) {
var statusDiv = document.createElement("div");
statusDiv.id = "statusMessage";
element.insertBefore(statusDiv, element.childNodes[0]);
}
}
);
var currentpageUrl = null;
WinJS.Navigation.addEventListener("navigating", function (evt) {
currentpageUrl = evt.detail.location;
});
WinJS.log = function (message, tag, type) {
var statusDiv = document.getElementById("statusMessage");
};
function activated(e) {
WinJS.Utilities.query("#featureLabel")[0].textContent = App.appTitle;
}
WinJS.Application.addEventListener("activated", activated, false);
WinJS.Namespace.define("App", {
pageOutput: pageOutput
});
})();
Output
Summary
In this article I described how to create a Windows Store App for an Advanced Search in the Home Group using JavaScript. I hope this article has helped you to understand this topic. Please share it. If you know more about this, your feedback and constructive contributions are welcome.