Search code examples
sharepointoffice365sharepoint-onlinesharepoint-appssharepoint-jsom

How to provision Branding files using SharePoint Hosted App in SharePoint Online/Office 365?


I am looking for SharePoint Hosted App Solution which will provision Branding files (JS/CSS/Images) into SharePoint Online/Office 365 environment.

I got a very good article to achive this and tried to implement the same as shown in below link: http://www.sharepointnutsandbolts.com/2013/05/sp2013-host-web-apps-provisioning-files.html

This solution is not working for me and while execution of app, I am getting below error: Failed to provision file into host web. Error: Unexpected response data from server. Here is the code which is giving me error:

// 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 < 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);
}

Please suggest me, what can be the issue in this code? Or else suggest me another way/reference in which I can Create a SharePoint-Hosted App to provision Branding Files.

Thanks in Advance!


Solution

  • I would use a different method to access host web context as follows:

    //first get app context, you will need it.
    var currentcontext = new SP.ClientContext.get_current();
    //then get host web context
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var hostcontext = new SP.AppContextSite(currentcontext, hostUrl);
    
    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];
            }
        }
    }
    

    Here are some references:

    https://sharepoint.stackexchange.com/questions/122083/sharepoint-2013-app-create-list-in-host-web

    https://blog.appliedis.com/2012/12/19/sharepoint-2013-apps-accessing-data-in-the-host-web-in-a-sharepoint-hosted-app/

    http://www.mavention.com/blog/sharePoint-app-reading-data-from-host-web

    http://www.sharepointnadeem.com/2013/12/sharepoint-2013-apps-access-data-in.html

    Additionally, here is an example of how to deploy a master page, however as you might notice during your testing the method used to get host web context is not working as displayed in the video and you should use the one I described before.

    https://www.youtube.com/watch?v=wtQKjsjs55I

    Finally, here is a an example of how to deploy branding files through a Console Application using CSOM, if you are smart enough you will be able to convert this into JSOM.

    https://channel9.msdn.com/Blogs/Office-365-Dev/Applying-Branding-to-SharePoint-Sites-with-an-App-for-SharePoint-Office-365-Developer-Patterns-and-P