title: Add masterpage to hostweb masterpage galley with SPapp
body: <p>Im trying to add a masterpage to the HostWeb mastrepageGallery from my SharePointApp. I modified the code from CHRIS O'BRIEN (<a href="http://www.sharepointnutsandbolts.com/2013/05/sp2013-host-web-apps-provisioning-files.html" rel="nofollow">http://www.sharepointnutsandbolts.com/2013/05/sp2013-host-web-apps-provisioning-files.html</a> ) but when I run it I get the error message: "Failed to provision file into host web. Error: undefined". Im using JSOM (Java Script Object Model).
  I also get the Error: "Failed to get the user name. Error:Unexpected response data from server." And the last Error is weird also because it worked fine before this MasterPage thing...</p>
  
  <p>Anyone knows what's wrong?</p>
  
  <pre><code>    'use strict';
  
  window.KEM = window.KEM || {};
  
  window.KEM.HostWebApp = function () {
      var hostWebUrl,
          appWebUrl,
          hostWebContext,
          destinationServerRelativeUrl,
          destinationFileName,
  
      // locate a file in the app web and retrieve the contents. If successful, provision to host web..
      readFromAppWebAndProvisionToHost = function (appPageUrl, hostWebServerRelativeUrl, hostWebFileName) {
          destinationServerRelativeUrl = hostWebServerRelativeUrl;
          destinationFileName = hostWebFileName;
  
          var req = $.ajax({
              url: appPageUrl,
              type: "GET",
              cache: false
          }).done(function (fileContents) {
              if (fileContents !== undefined &amp;&amp; fileContents.length &gt; 0) {
                  uploadFileToHostWebViaCSOM(destinationServerRelativeUrl, destinationFileName, fileContents);
              }
              else {
                  alert('Failed to read file from app web, so not uploading to host web..');
              }
          }).fail(function (jqXHR, textStatus) {
              alert("Request for page in app web failed: " + textStatus);
          });
      },
  
      // utility method for uploading files to host web..
      uploadFileToHostWebViaCSOM = function (serverRelativeUrl, filename, contents) {
          var createInfo = new SP.FileCreationInformation();
          createInfo.set_content(new SP.Base64EncodedByteArray());
          for (var i = 0; i &lt; contents.length; i++) {
  
              createInfo.get_content().append(contents.charCodeAt(i));
          }
          createInfo.set_overwrite(true);
          createInfo.set_url(filename);
          var files = hostWebContext.get_web().getFolderByServerRelativeUrl(serverRelativeUrl).get_files();
          hostWebContext.load(files);
          files.add(createInfo);
  
  
          hostWebContext.executeQueryAsync(onProvisionFileSuccess, onProvisionFileFail);
      },
      onProvisionFileSuccess = function () {
          $('#message').append('&lt;br /&gt;&lt;div&gt;File provisioned in host web successfully: ' + destinationServerRelativeUrl + '/' + destinationFileName + '&lt;/div&gt;');
          setMaster('/' + destinationServerRelativeUrl + '/' + destinationFileName);
      },
      onProvisionFileFail = function (sender, args) {
          alert('Failed to provision file into host web. Error:' + sender.statusCode);
      },
  
      // set master page on host web..
      setMaster = function (masterUrl) {
          var hostWeb = hostWebContext.get_web();
          hostWeb.set_masterUrl(masterUrl);
          hostWeb.update();
  
          hostWebContext.load(hostWeb);
          hostWebContext.executeQueryAsync(onSetMasterSuccess, onSetMasterFail);
      },
      onSetMasterSuccess = function () {
          $('#message').append('&lt;br /&gt;&lt;div&gt;Master page updated successfully..&lt;/div&gt;');
      },
      onSetMasterFail = function (sender, args) {
          alert('Failed to update master page on host web. Error:' + args.get_message());
      },
  
      init = function () {
          var hostWebUrlFromQS = QS("SPHostUrl");
          hostWebUrl = (hostWebUrlFromQS !== undefined) ? decodeURIComponent(hostWebUrlFromQS) : undefined;
  
          var appWebUrlFromQS = QS("SPAppWebUrl");
          appWebUrl = (appWebUrlFromQS !== undefined) ? decodeURIComponent(appWebUrlFromQS) : undefined;
      }
  
      return {
          execute: function () {
              init();
  
              hostWebContext = new SP.ClientContext(window.KEM.AppHelper.getRelativeUrlFromAbsolute(hostWebUrl));
              readFromAppWebAndProvisionToHost(appWebUrl + '/MasterPages/MasterOslo.txt', '_catalogs/masterpage', 'KEM.master');
          }
      }
  }();
  
  window.KEM.AppHelper = {
      getRelativeUrlFromAbsolute: function (absoluteUrl) {
          absoluteUrl = absoluteUrl.replace('https://', '');
          var parts = absoluteUrl.split('/');
          var relativeUrl = '/';
          for (var i = 1; i &lt; parts.length; i++) {
              relativeUrl += parts[i] + '/';
          }
          return relativeUrl;
      },
  };
  
  function QS(name) {
      //Simplifies the code
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regex = new RegExp("[\\?&amp;]" + name + "=([^&amp;#]*)"),
          results = regex.exec(location.search);
      return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  }
  
  $(document).ready(function () {
      window.KEM.HostWebApp.execute();
  });
  </code></pre>