Im developing a Visual Web part for SharePoint 2010 via Visual Studio 2013.
And i want to add css link to visual web part in .ascx file like this:

<SharePoint:CssRegistration Name="<% $SPUrl:~site/_layouts/CompanyName/CSS/AddComment.css %>" runat="server" />  

But when i do that ".ascx.g.cs" file gets empty and ".ascx.cs" code file will not gonna know any controls.
How can i fix it or use any other way to get current web url?

  • try adding the ID attribute to the control, eg. ID="Css1"
    – MdMazzotti
    Commented Jan 30, 2014 at 13:28
  • 1
    @MdMazzotti: I tested it but unfortunately the problem still exists :( Commented Jan 30, 2014 at 19:09

1 Answer 1


Based on documentation in MSDN it can't be done using the old regular way.

Visual web parts don't directly support SharePoint server tokens such as $SPUrl. For more information, see "Token Restrictions in Sandboxed Visual Web Parts".

Visual web parts in sandboxed solutions can't process standard tokens, such as $SPUrl, that the SharePoint runtime supports. As a result, the URL isn't resolved, and you can't preview the content in Design view in the visual web part designer if you refer to it directly in a script element, such as in the following example:

<script src=”<% $SPUrl:~site/SiteAssets/ListOperations.js %>"></script>

To work around this limitation and resolve the token, refer to it by using literals:

<asp:literal ID="Literal1" runat="server" Text="&lt;script src='" />
<asp:literal ID="Literal2" runat="server" Text="<% $SPUrl:~site/SiteAssets/ListOperations.js %>" />
<asp:literal ID="Literal3" runat="server" Text="' type='text/javascript' &gt;&lt;/script&gt;" />  

More info on MSDN: Troubleshooting SharePoint Solutions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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