Change App Web Logo In SharePoint Hosted Apps

Whenever you create a SharePoint hosted app, it will create an app on your host web. In Appweb, you can see that the site logo is the default SharePoint logo and even if we change the app icon in the Appmanifest.xml file, it will change the logo of the app but not the app website logo URL and we can't change the Logo using SiteSettings as it's not accessible in Appweb. So generally, developers change the image src tag attribute at runtime. But we can set the SiteLogoUrl property of Appweb using REST API or JSOM.
 
So today, we will see how to implement it using REST API.
  • Create a SharePoint Hosted App using VS and target it to the developer site which will add a default.aspx page.
  • Add a logo of your appweb into the image folder.
  • Open App.js or you can create a new JS file and and copy the below code.
  • Give reference to the script file which you have created to the pages.
  1. 'use strict';  
  2. ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js"); 
  3.  
  4. //to get query string parameters
  5. function getParameterByName(name, url) {  
  6.    if (!url) url = window.location.href;  
  7.    name = name.replace(/[\[\]]/g, "\\$&");  
  8.    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),  
  9.    results = regex.exec(url);  
  10.    if (!results) return null;  
  11.    if (!results[2]) return '';  
  12.    return decodeURIComponent(results[2].replace(/\+/g, " "));  
  13. }  
  14.    
  15. //On document ready get the current site logo
  16. $(document).ready(function () {  
  17.    var SPAppWebUrl = getParameterByName("SPAppWebUrl");  
  18.    if (SPAppWebUrl) {  
  19.       getSiteLogoUrl(SPAppWebUrl);  
  20.    }  
  21. });  
  22.   

  23. //Using web rest api end post and updating the site logo url   
  24. function changeSiteLogo(weburl) {  
  25.    var imageUrl = weburl + "/Images/News.png";  
  26.    var webData = {  
  27.       '__metadata': { type: 'SP.Web' },  
  28.       'SiteLogoUrl': imageUrl  
  29.    }  
  30.    $.ajax({  
  31.       url: weburl+ "/_api/web",  
  32.       type: "POST",  
  33.       data: JSON.stringify(webData),  
  34.       headers: {  
  35.          "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  36.          "accept""application/json;odata=verbose",  
  37.          "content-type""application/json;odata=verbose",  
  38.          "X-HTTP-Method""MERGE"  
  39.       },  
  40.       success: function (data) {                                                                                          console.log("App web logo changed") ;                                                            
  41.           window.location.href = window.location.href;
  42.       },  
  43.       error: function (data) { alert("errrr")}  
  44.    });  
  45. }  
  46.    
  47. function getSiteLogoUrl(weburl) {  
  48.    //Logo which you want to assign   
  49.    var imageUrl = weburl + "/Images/News.png";  
  50.    $.ajax({  
  51.       url: weburl + "/_api/web?$select=SiteLogoUrl",  
  52.       type: "GET",  
  53.       headers: {  
  54.          "accept""application/json;odata=nometadata",  
  55.       },  
  56.       success: function (data) {  
  57.          var SiteLogoUrl = data.SiteLogoUrl;  
  58.          //if site logo is already changed no need to apply changes, if not we have to change
  59.          if (SiteLogoUrl != imageUrl) {  
  60.             changeSiteLogo(weburl);  
  61.          }  
  62.       },  
  63.       error: function (data) { alert("errrr") }  
  64.    });  
  65. }  
Ebook Download
View all
Learn
View all