A blog by James Hodges about SharePoint and ASP.NET Technologies

7. January 2014 22:24
by james
5 Comments

How to safely get the absolute URL of the page that hosts your app part.

7. January 2014 22:24 by james | 5 Comments

Add the event listener to your AppPart. When the listener is triggered, it will update the message div with the url of the host page.

window.addEventListener("message", receiveMessageInApp, false);
function receiveMessageInApp(event)
{  
 jQuery('#message').text('The host page url is:' + event.data.msg);
}

Add the event broadcaster to your page via a script part, SharePoint Designer, or to the master page (with some performance tweaking). The current script requires jQuery but it can be done without it as well:

<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" language="JavaScript">
 jQuery("iframe").ready( function() {
  setInterval(function(){
   var iframeid = jQuery("iframe").attr('id');
   apppartiframe=document.getElementById(iframeid);
   var appMsg = {'msg':window.location.href};
   apppartiframe.contentWindow.postMessage(appMsg, "*");
  },3000);
 });
</script>

31. December 2013 14:49
by james
1 Comments

Querying HostWeb from AppWeb using REST and getting past Cross Site Scripting Limitations

31. December 2013 14:49 by james | 1 Comments

I have been playing around with Office 365 today and ran across an interesting challenge querying the HostWeb from the AppWeb. To get around Cross Site Scripting limitations, you have to take a very special approach which I have documented below. To use, create a new project using NAPA Development tools and copy the code below into your App.js file. Make sure that you have also included jQuery and json2.min.js in your Default.aspx file.

References used to create this code:

http://msdn.microsoft.com/en-us/library/office/fp179927.aspx

http://www.sharepointnutsandbolts.com/2012/11/access-end-user-data-in-host-web-from.html 

My Code:

var hostweburl;
var appweburl;

$(document).ready(function () {
	sharePointReady();    
});
 
function sharePointReady() {
    hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    loadDependentScripts();
}
 
function loadDependentScripts() {
    var scriptbase = hostweburl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js",
                function () { $.getScript(scriptbase + "SP.RequestExecutor.js", getAllDocumentsFromHostWeb); }
                );
        }
    );
}

function getAllDocumentsFromHostWeb() {
    var executor;
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync(
        {
            url:appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getByTitle('Documents')/items/?$select=ID,FileLeafRef,EncodedAbsUrl&@target='" + hostweburl + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
			success: function (data) {
				alert("success: " + JSON.stringify(data));
			},
			error: function (err) {
				alert("error: " + JSON.stringify(err));
			}
        }
    );
}

function getHostWebListsUsingREST() {
    var executor;
 
    // although we're fetching data from the host web, SP.RequestExecutor gets initialized with the app web URL..
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync(
        {
            url:
                appweburl + "/_api/SP.AppContextSite(@target)/web/lists/?@target='" + hostweburl + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
			success: function (data) {
				alert("success: " + JSON.stringify(data));
			},
			error: function (err) {
				alert("error: " + JSON.stringify(err));
			}
        }
    );
}

 
function getAllHostWebPropsUsingREST() {
    var executor;
	
	 // although we're fetching data from the host web, SP.RequestExecutor gets initialized with the app web URL..
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync(
        {
            url:appweburl + "/_api/SP.AppContextSite(@target)/web/?@target='" + hostweburl + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
			success: function (data) {
				alert("success: " + JSON.stringify(data));
			},
			error: function (err) {
				alert("error: " + JSON.stringify(err));
			}
        }
    );
}


function getQueryStringParameter(paramToRetrieve) {
	var params =
	    document.URL.split("?")[1].split("&");
	for (var i = 0; i < params.length; i = i + 1) {
	  var singleParam = params[i].split("=");
	  if (singleParam[0] == paramToRetrieve)
	    return singleParam[1];
	}
}

21. December 2013 18:49
by james
19 Comments

Office 365 Online

21. December 2013 18:49 by james | 19 Comments

Well, I had taken a little break from training on SharePoint 2013 to focus on my wife's blog and then this one. I'm picking it back up today.

I've decided to focus my SP2013 training on Office 365 since it seems like there is so much of a push away from on-premises installations. I know that they won't ever go away but if you don't learn to be a SharePoint script junkie then your days of being an SP Developer seem numbered.

 

You can get to my public facing site from here:

https://jhodgesenterprises-public.sharepoint.com/

 

20. December 2013 21:26
by james
2 Comments

Welcome to Jhodges.com Blog

20. December 2013 21:26 by james | 2 Comments

Hi, my names is James Hodges. I am web developer who specializes in products like SharePoint and ASP.NET.

JHodges.com has been around for quite some time now and has gone through quite a few facelifts. This site was originally written in Classic ASP. A few years later I rewrote it in ASP.NET 2.0. I then upgraded and enhanced it with ASP.NET 4 a few years later.

A couple of years ago, my wife got her 15 minutes of fame and did a bunch of commercials, talk shows, local news shows, the Today Show (I was on there too!). A lot of excitement was happening so I rebranded JHodges.com (which was 100% content managed) and gave it to her to borrow for a while. Eventually, I created her another site and am in the process of reclaiming and redoing Jhodges.com for my own purposes. My own domain again...so exciting.

Something Blog Worthy:

Back in my college days I took a philosophy class and learned a lot about Plato and Socrates. The lessons in the class have helped me with Technology more than anything else I have run across to date. I can go into the background of the dialogue but, to keep it brief, there is a famous quote supposedly said by Socrates that goes something like: "I am the wisest man alive, for I know one thing, and that is that I know nothing."

Technology is rapidly evolving. There is no person on the planet who is capable of keeping up with this extreme rate of evolution. Technologies like SharePoint and ASP.NET are at the pinnacle of this evolutionary process. Teams consisting of hundreds of developers are constantly adding and changing things.

I would like to think that I have gotten pretty good with SharePoint. I have an MCPD, MCTS, MCITP in this technology and have been working full time in it for about 4 years now. I also have a completely separate MCPD, MCTS in ASP.NET technologies. Yes, there is a little self promotion in those statements but also something I really want you to think about. With all that work I put into those certifications, SharePoint 2013 has recently come out. ASP.NET MVC 4.5 is also out and seems to be slowly replacing web forms. With as much as I would sometimes like to think I know...I'm back to square one again. Humbled and struggling to master concepts like App Models, doing declarative Workflows in Visual Studio, SharePoint Client Side Object Model, REST.

Will I figure it out...yes. How long will this new knowledge be good for?...probably until 2016 when they change all these things once again.

The other thing I realize is, that if someone else can figure something out, then so can I. Its just a matter of hard work and patience. I refuse to ever say someone is smarter than me for I realize that I am the wisest person alive simply because I know nothing at all.

Happy Holidays and check back soon.

 

P.S.

If you are looking for information about my wife Jennifer, her site is now at:  http://MetamorFit.org .