Monday, 21 July 2014

SharePoint read Host web query string in App Part

Read Host web values in App web 
Problem Statement: I have to read Host web Query string value in App web.
Steps:
 Read query string from url.
 Create a property bag in host web, push query string in to property bag In app web read Property bag  values and use it.
Solution:
Add Script Editor webpart then add below code
 Read query string and create property bag
<script type="text/javascript">
$(document).ready(function () {
var queryStringValue = GetUrlKeyValue('MyPropertyName', window.location.href);
if(!queryStringValue){
queryStringValue="0";
}
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', writeProperty("MyPropertyName",queryStringValue));
});
function writeProperty(propertyName, propertyValue) {
var clientContext;
clientContext = SP.ClientContext.get_current();
var web = clientContext.get_site().get_rootWeb();
this.props = web.get_allProperties();
this.props.set_item(propertyName, propertyValue);
web.update();
clientContext.executeQueryAsync(Function.createDelegate(this, writePropertySuccess), Function.createDelegate(this, writePropertyFailed));
}
function writePropertySuccess(){
var nid = SP.UI.Notify.addNotification("Property registered", false);
}
function writePropertyFailed(sender, args){
var nid = SP.UI.Notify.addNotification("Failure while trying to write the property: " + args.get_message(), false);
}
</script>
view raw gistfile1.js hosted with ❤ by GitHub
In App part, write below code, where you want to use query string value.
Read property bag in SharePoint App
<script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var scriptbase = hostweburl + '/_layouts/15/';
$.getScript(scriptbase + 'SP.Runtime.js',
function () {
$.getScript(scriptbase + 'SP.js',
function () { $.getScript(scriptbase + 'SP.RequestExecutor.js', readProperty(hostweburl)); }
);
}
);
});
function readProperty(hostweburl) {
var appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
var clientContext = new SP.ClientContext.get_current();
var hostContext = new SP.AppContextSite(clientContext, hostweburl);
var web = hostContext.get_web();
clientContext.load(web);
webProperties = web.get_allProperties();
clientContext.load(webProperties);
clientContext.executeQueryAsync(propertiesSucceeded, propertiesFailed);
}
function propertiesSucceeded() {
var allProps = webProperties.get_fieldValues();
var customProp = "";
//Check for My property.
//My Property name is: MyPropertyName
if (webProperties.get_fieldValues().MyPropertyName != undefined) {
var customProp = webProperties.get_fieldValues().MyPropertyName;
alert("Value from App" + customProp);
}
}
function propertiesFailed(args, sender) {
alert("failed");
}
function getQueryStringParameter(param) {
var params = document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == param) {
return singleParam[1];
}
}
}
</script>
view raw gistfile1.js hosted with ❤ by GitHub