1

I want to add the <base> element(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) in my masterpage <head>. I want it to be dynamic so whe the masterpage is used on different sub/sites it writes the correct href. For example it might resolve to the following when reused...

I think I need to use the ~site token, but I'm not sure how I mark it up to tell SharePoint to expand the token in a base tag. In the past I've used SharePoint:ScriptLink and SharePoint:CssRegistration to dynamically add things to the page. But I'm not sure what model to follow here?

What control/markup do I need to add to my .html masterpage to get it to correctly render <base href="current site url" />?

EDIT

Found this answer which is close. I tried the following values:

  <base  href="<%$SPUrl:~site%>" runat="server" />
  <base  href="$SPUrl:~site" runat="server" />
  <base  href="~site" runat="server" />

The first one throws an error that < is an illegal character. The rest just append ~site to the end of the tag. What I'm missing is the part that SP uses from the masterpage html that tells the asp to expand the token and then rewrite it as an actual tag...

2
+50

If you use the .html master page, this should render the base tag:

<!--SPM:<asp:Literal runat="server" Text="&#60;base href='"/>-->
<!--SPM:<asp:Literal runat="server" Text="&#60;% $SPUrl:~site/ %&#62;"/>-->
<!--SPM:<asp:Literal runat="server" Text="'/&#62;"/>-->

And if you are editing the .master page, than this should do it:

<base href='<asp:Literal runat="server" Text="&#60;% $SPUrl:~site/ %&#62;"/>' />

You can also use the $SPUrl:~sitecollection if you need the absolute path.

  • Thank you can you provide a link to any kind of documentation for that? I spent hours looking for what kinds of things I could add to the page. I never once saw asp:Literal in any blog, post, or tutorial. – Rothrock Jun 29 '18 at 22:34
  • Glad it helped you! I didn't find any official documentation on this, but using asp:Literal is an old trick for replacing these url tokens.. I've been using it since SP2007 – Damjan Tomic Jun 30 '18 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.