mdevm
mdevm

Reputation: 97

How to show the Site Name specifically on the document library landing page?

One of the Project I am working on, I have a requirement to show the site name on the document library page so that the user is aware about the site he/she is working on.

Also I want the site name to appear only on the Document Library page level. I believe if this is done at the master page level by leveraging the breadcrumb but then it would reflect everywhere in the navigation so want to make sure that it reflects only on the document library page.

Any thoughts on how to achieve this? I am working with Office 365/SharePoint online. Thanks in advance.

Upvotes: 0

Views: 90

Answers (2)

Mike Smith - MCT
Mike Smith - MCT

Reputation: 1231

Code to add for updating every page where the Site Title is not displayed. (Classic UI SP 2013, 2016 and Online only) This has one more IF statement than the previous example that checks to see if the site title is already displayed.

<script>
function TTNAddSiteTitle() {

  if( document.querySelector("#DeltaPlaceHolderPageTitleInTitleArea").innerText.trimRight() != ctx.SiteTitle) {

    // if(ctx.listTemplate == "101" ) {
      document.querySelector("#DeltaPlaceHolderPageTitleInTitleArea").insertAdjacentHTML("beforeBegin",ctx.SiteTitle + " - ");
    // }
  }
}
_spBodyOnLoadFunctionNames.push("TTNAddSiteTitle");
</script>

Upvotes: 1

Mike Smith - MCT
Mike Smith - MCT

Reputation: 1231

Several options, but most will add text to a SPAN in the page using a pattern like this:

document.querySelector("#DeltaPlaceHolderPageTitleInTitleArea span").insertAdjacentHTML("beforeBegin","Your Site Name - ")

You can have it pull the site name from the context (ctx.SiteTitle):

document.querySelector("#DeltaPlaceHolderPageTitleInTitleArea span").insertAdjacentHTML("beforeBegin",ctx.SiteTitle + " - ")

A full script block:

<script>
function TTNAddSiteTitle() {
  document.querySelector("#DeltaPlaceHolderPageTitleInTitleArea").insertAdjacentHTML("beforeBegin",ctx.SiteTitle + " - ");
}
_spBodyOnLoadFunctionNames.push("TTNAddSiteTitle");
</script>

Now the question is where do you put the script...

  • In the view page. Edit each view page using SharePoint Designer, click the Advance Mode button, and add the code above just after "</WebPartPages:WebPartZone>".
  • In a Content Editor Web Part added to each view page. While the easiest, the problem with this one is that the page will not display the FILE and LIBRARY ribbons until the user clicks an item in the list. (The page sees two web parts, and does not know which ribbon to display until one has been clicked.)
  • In the master page, with some code to check to see if it is a library: if(ctx.listTemplate == "101" { ... }. But for existing sites, this will require an update to each site's master page.

Possible code for a master page.

<script>
function TTNAddSiteTitle() {
  if(ctx.listTemplate == "101" ) {
    document.querySelector("#DeltaPlaceHolderPageTitleInTitleArea").insertAdjacentHTML("beforeBegin",ctx.SiteTitle + " - ");
  }
}
_spBodyOnLoadFunctionNames.push("TTNAddSiteTitle");
</script>

Upvotes: 0

Related Questions