Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to use ScriptResx.ashx in an Application Page I'm building (just to test out the functionality) and every time I try to grab a resource from what should be a JavaScript object, SharePoint comes back and tells me that SP.Publishing.Resources is not a valid object. What bothers me is that I can run http://dev/_layouts/15/ScriptResx.ashx?name=sp.publishing.resources&culture=en-us in the browser and I get an entire Javascript object (with namespace returned):

_EnsureJSNamespace('SP.Publishing');
SP.Publishing.Resources=
    {spellcheckerCheckSpelling:'Check Spelling', 
    ..., 
    cui_stt_ButVariationsMenuListSettingsTooltip15:'View variation list settings.'};
if (typeof(Sys)!='undefined' && Sys && Sys.Application) { Sys.Application.notifyScriptLoaded(); }

The code I'm using in my Application Page is as follows:

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript" src="/_layouts/15/Scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    function pageLoad() {
        RegisterSod("sp.publishing.resources.resx", "/_layouts/15/ScriptResx.ashx?name=sp.publishing.resources&culture=en-us");
        var test = SP.Publishing.Resources;
        $('#testDiv').text(SP.Publishing.Resources.toString);
    }
</script>

Does anyone know what I'm doing wrong?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

RegisterSod doesn't fetch the script file. You must load it. Use LoadSodByKey, or use the plain tag to load it synchronously. I wrote a blog post about how to use javascript localizaton in SharePoint and scriptresx.ashx . Waldek Mastykarz shows how to use it with RegisterSod. But you have to know, that the script won't be downloaded before you explicitly load it. A similar issue was reported in another question on SharePoint StackExchange

After you have added the sod:

RegisterSod("sp.publishing.resources.resx"
     , "/_layouts/15/ScriptResx.ashx?name=sp.publishing.resources&culture=en-us");

You could do it so:

LoadSodByKey("sp.publishing.resources", function() {
   //this is when the generated script with resources has been loaded
});

Or you can load it and invoke your functions (which depend on the sp.publishing.resources) in ExecuteOrDelayUntilScriptLoaded:

LoadSodByKey("sp.publishing.resources");

//... somewhere esle
ExecuteOrDelayUntilScriptLoaded(function() {
   //this is when the generated script with resources has been loaded
}, "sp.publishing.resources");

This AMD (asynchronous module dependency) approach is great when you don't need this script everywhere.

On the other hand, if you know, you need this allmost everywhere, just add the script-tag like this:

<script src="/_layouts/15/ScriptResx.ashx?name=sp.publishing.resources&culture=en-us">
</script>

This script will download the file and will be ready for you immediately when the page has been loaded.

share|improve this answer
    
Thank you. I've read both your blogs (and started reading into that other StackExchange thread after I posted this) but finally got it when all the pieces are placed together. Thanks for the help! –  EHorodyski Mar 13 '13 at 14:34
    
I'm having issues with ExecuteOrDelayUntilScriptLoaded. For whatever reason I can get my function to work if it's attached to LodSodByKey but it just doesn't do anything when I call it like this: ExecuteOrDelayUntilScriptLoaded(function (){$('#testDiv').text(SP.Publishing.Resources.spellcheckerSpellButtonTooltip);}, "sp.publishing.resources.resx"); –  EHorodyski Mar 13 '13 at 17:36
    
Even if you use ExecuteOrDelayUntilScriptLoaded, you have to run LoadSodByKey. Do you do so? –  Anatoly Mironov Mar 14 '13 at 8:53
    
I tried a bunch of approached with ExecuteOrDelayUntilScriptLoaded, and maybe all those times I was using it wrong, but I figured out another way to go about it. I didn't realize that LoadSodByKey required a function as a secondary parameter, so I just feed it null. Then I'm free to write my jQuery each loop that does the conversion for any elements that contain the micro-data attribute I'm setting aside for Resource Strings. Thanks for all your help. –  EHorodyski Mar 15 '13 at 15:43
1  
Thank you! I learned as well. I'll update my answer so it is complete. –  Anatoly Mironov Mar 15 '13 at 17:30
add comment

It can also be done in code behind OnInit function e.g.,

protected override void OnInit(EventArgs e)
{
     base.OnInit(e);
     InitializeControl();

     CultureInfo ci = SPContext.Current.Web.Locale;
     string url = string.Format("/_layouts/15/ScriptResx.ashx?name=MyResourceFile&culture={0}&rev={1}", ci.ToString(), DateTime.Now.ToString("yyyyMMdd"));

     if (!cs.IsClientScriptIncludeRegistered("MyResourceFile.resx"))
           cs.RegisterClientScriptInclude("MyResourceFile.resx", url);
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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