Friday, February 24, 2012

Different ways to get site collection Url (root site) and current Site url in Master page and Layouts without using code snippets in Sharepoint 2010

In many cases Sitecollection are created at different levels using the managed paths rather than only at the root. considering these scenario while custom branding the site templates have to make sure the path are not absolute and are refering to the current site collection.

Following some of the different way to refer the resource using relative path rather than a fixed absolute paths.
  1. $SPUrl:~sitecollection :
    This is a default variable provided by Sharepoint framework. The disadvantage of this variable is it works only for server controls i.e. Controls or links with runat=server, and also in some of the cases it fails if you use it directly.
    < link rel="stylesheet" type="text/css" runat="server" href="<% $SPUrl:~sitecollection/Style%20Library/custom_style.css %>"/>
  2. SharePoint:EncodedLiteral along with SPUrl:~SiteCollection:
    This is a sharepoint Control to show Plain text out of Server variable like a label control, but can be used outside of the form tag i.e. can be used in the Head section of the master page.
    < script type="text/javascript" src='< SharePoint:EncodedLiteral runat="server" text="<%$SPUrl:~SiteCollection/style%20library/jquery.min.js%>" EncodeMethod="HtmlEncode"/>'>
  3. _spPageContextInfo.siteServerRelativeUrl:
    This is a sharepoint Javascript API variable which returns the Sitecollection relative path at any level of the subsites.
    _SitecollectionUrl = _spPageContextInfo.siteServerRelativeUrl =="/"? "" : _spPageContextInfo.siteServerRelativeUrl;
  4. L_Menu_BaseUrl:
    This is another JavaScript API variable which returns current subsite or Sites Url.

0 comments:

Post a Comment

Powered by Blogger.