I am trying to reference JS and CSS files in sandbox web part, but once I try to deploy it gives me this error:

The expression prefix 'SPUrl' was not recognized. Please correct the prefix or register the prefix in the section of configuration.

I am referencing my css and js this way:

<SharePoint:ScriptLink Name="<% $SPUrl:~sitecollection/style library/assets/js/modernizr.js %>" ID="modernizr" runat="server" ></SharePoint:ScriptLink>
<SharePoint:CssRegistration ID="CssRegistration1" Name="<% $SPUrl:~sitecollection/style library/assets/css/style.css %>" runat="server" After="corev15.css" ></SharePoint:CssRegistration>

Anything wrong with that? Is there a limitation for this in Sandbox solutions?

  • Did you try removing SPUrl and simply start with tilde and site collection token. Also remove starting and ending less than , grester than and percent symbols – Nadeem Yousuf Feb 1 '15 at 7:20
  • Hi Nadeem, can you show me a snippet of what you mean? – Brittany Rutherford Feb 1 '15 at 8:33
  • Name="~sitecollection/style library/assets/js/modernizr.js" – Nadeem Yousuf Feb 1 '15 at 8:51

Ok it turns out that both ScriptLink and CssRegistration are not available in Sandboxed solutions. These are in Microsoft.SharePoint.WebControls namespace and you don’t have access to that in Sandboxed solutions. The fix is to use <link> tag for CSS. Example:

<link rel="stylesheet" type="text/css" href="<% $SPUrl:~sitecollection/style library/assets/css/style.css %>" />

Similarly use following code to refer to JavaScript file:

<script type="text/javascript" src="<% $SPUrl:~sitecollection/style library/assets/js/modernizr.js %>"></script>

Sources: http://blog.pixelmill.com/1143/cssregistration-sharepoint-2010-sandbox/ http://blog.pixelmill.com/1150/link-javascript-files-sharepoint-2010-sandbox-solution/ https://coder87.wordpress.com/2013/09/30/cssregistration-is-not-allowed-in-sharepoint-2013-sandbox/

  • Hi Nadeem. It's now telling me: Literal expressions like '<% $SPUrl:~SiteCollection/Style%20Library/assets/css/style.css %>' are not allowed. Use <asp:Literal runat="server" Text="<%$SPUrl:~SiteCollection/Style%20Library/assets/css/style.css%>" /> instead. I can't even deploy the web part, it's telling me Error 1 The name 'InitializeControl' does not exist in the current context unless I remove the references. – Brittany Rutherford Feb 1 '15 at 11:51
  • Hmm, in that case can you try to write the code in the .cs file of the webpart as described in PixelMill blog. The first two source links I have provided. What it is doing is that it is getting the Url of the root site collection and appending it ot the file location in style library. – Nadeem Yousuf Feb 1 '15 at 12:02
  • Is this the recommended way of doing it in Sandbox? I just want to learn the right thing, so if you say so I will go with it. – Brittany Rutherford Feb 1 '15 at 12:15
  • As can be seen neither ScriptLink nor ScriptLink is allowed in sandboxed solutions. Secondly, it appears from your error that $SPUrl:~SiteCollection tokens are also not working. So, finally option left is to use full url of the css and js file. One way which I wont like personally is to hardcode the url, something like "yourootsite/stylelibrary/yourcss.css" and same for .js file. However, a better approach is to dynamically generate the root site collection url in the code and then inject other parts. Choice is yours. – Nadeem Yousuf Feb 1 '15 at 12:22

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.