1

I have a master page deployed with a feature which has custom javascript and css references on it. I am using the <Sharepoint:ScriptLink> and <Sharepoint:CssLink> tags and the code is deployed into the Style Library collection. Here are the exact tags:

<SharePoint:ScriptLink ID = "jquery152" Language="javascript" Name="~sitecollection/Style Library/AcmeCompany/js/jquery-1.5.2.min.js" runat="server" />
<SharePoint:ScriptLink ID="cyclePlugin" Language="javascript" Name="~sitecollection/Style Library/AcmeCompany/js/jquery.cycle.min.js" runat="server" />
<SharePoint:ScriptLink ID="dartTheme" Language="javascript" Name="~sitecollection/Style Library/Dart/js/DartIntranetTheme.js" runat="server" />

When the feature is deployed I am using the ChildSiteInit function to also declare that all child sites should use these same files. The following is the code I am using to activate this for child sites:

public class ChildSiteInit : SPWebEventReceiver
{
    /// <summary>
    /// A site was provisioned.
    /// </summary>
    public override void WebProvisioned(SPWebEventProperties properties)
    {
        //base.WebProvisioned(properties);

        SPWeb childSite = properties.Web;
        SPWeb topSite = childSite.Site.RootWeb;
        childSite.MasterUrl = topSite.MasterUrl;
        childSite.CustomMasterUrl = topSite.CustomMasterUrl;
        childSite.AlternateCssUrl = topSite.AlternateCssUrl;
        childSite.SiteLogoUrl = topSite.SiteLogoUrl;
        childSite.ApplyTheme("AcmeCoIntranet");
        childSite.Update();
    }
}

This works great for the root site and the master page with javascript and css are working as expected. However, when I create a child site the javascript and css are broken.

Does anyone know how to get the child sites to also see the root css and js files? I have put them in the Style Library collection but maybe they need to go in the global _layouts folder...? I'm using SharePoint Foundation 2010.

5
  • 1
    Broken how? Are you getting an error message? Apr 23, 2011 at 16:36
  • Be aware that the WebProvisioned event only fires for child sites, not for the root site. Apr 23, 2011 at 17:07
  • If I understand this correctly, after deploying your custom theme into your root site collection you create a sub site and the sub site does not automatically inherit the theme, master pages etc? Apr 23, 2011 at 18:02
  • OK upon further investigation it seem that the code above does work for child sites however, not upon initial feature deployment. What is happening is that upon deployment to a root site the theme, javascript, and alternateCssUrl are working properly. If, from this site, I create a new child site the child site is not automatically being set to the top level custom mater page (even though I am specifiying this in the child site in it function above). If I go in and set the master page manually then the JavaScript is picked up but the alternative CSS URL is not.
    – KevinS
    Apr 25, 2011 at 9:55
  • Also, the only way the alternate CSS is applied to the child site is if I redeploy the packae again to the root site - then any available child sites will get the master page, javascript, and alternate CSS url as expected... very strange.
    – KevinS
    Apr 25, 2011 at 9:58

4 Answers 4

1

I have run into this and my solution was to place all javascript files in the layouts folder

1
  • 1
    yes but I am trying to keep all of my custom theme files isolated to their represented theme folder which is supposed to be the purpose of the style library virtual document libraries
    – KevinS
    Apr 25, 2011 at 9:43
1

Found the answer myself through some trial and error debugging. It appears to be a bug in the AlternateCssUrl activation that occurs within the ChildSiteInit() call stack. Basically, the expected result from the code I originally listed would be that each subsequent child site would, upon creation, have the its AlternateCssUrl set to be the root level's AlternateCssUrl however, this is not what is happening so I think its most likely a bug. To get the child site to pick up the AlternateCss property from the parent you must create the child site from the new site command. Then go back the root level collection and deactivate then reactive the theme feature. Once you do this the child site then has the alternate CSS URL properly set and you don't have to do any work a rounds with either hard coding the CSS directly in the custom master page or putting all of your files within the layouts folder which is a globally shared folder. Also, if you are in standard edition or higher you can easily set the AlternateCssUrl manually and get around this problem however, in Foundation this is not possible since there is not ability to manually set the AlternateCssUrl and it must be done via feature activation.

Hope this helps someone else that runs into this same problem!

Regard, Kevin

0

This may be helpful to you. For more details you can click here

If you added those lines of code in a custom SharePoint page, you’ll find that the page will ignore them. Thankfully, SharePoint has given us some controls to add these references.

At this point its worth stating that I stored all my required JavaScript and CSS files within the “Style Library” directory situated in the root of any SharePoint 2010 intranet. In order to get these files I used controls called”CssRegistration” and “ScriptLink”: ?

<SharePoint:CssRegistration ID="CssRegistration1" Name="/Style Library/Home/CSS/jcarousel.css" runat="server" After="corev4.css" />
<SharePoint:ScriptLink ID="ScriptLink1" Name="~sitecollection/Style Library/Home/JS/jquery-1.4.4.min.js" runat="server" />

If you have stored your CSS and JavaScript within the physical file directory situated in the 14 hive folder, you will need to modify the above example to the following:

<SharePoint:CSSRegistration Name="<% $SPUrl:~SiteCollection/Style Library/Core Styles/jcarousel.css%>" runat="server"/>
<SharePoint:ScriptLink ID="ScriptLink1" Name="<% $SPUrl:~SiteCollection/Style Library/Core Styles/jquery.js%>" runat="server" />

The only difference between this example and our earlier example is that when we have just added “SPUrl” to get files relative to the current site collection.

1
  • This won't work in share point foundation as SpUrl isn't a valid object here its only available in Sp standard edition or higher
    – KevinS
    Apr 25, 2011 at 9:41
0

You can create custom Feature Receiver. Assume you have deployed your master page:

    public class MasterPageEventReceiver : SPFeatureReceiver
{
    private const string CustomMasterPageUrl            = "/_catalogs/masterpage/Your.master";
    private const string CustomPublishMasterPageUrl     = "/_catalogs/masterpage/YourPublish.master";
    private const string DefaultMasterPageUrl        = "/_catalogs/masterpage/v4.master";

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        try
        {
            using ( var curSite = ( SPSite ) properties.Feature.Parent )
            {
                using ( var curWeb = curSite.RootWeb )
                {
                    // Create full master URL
                    var masterUri = new Uri( curWeb.Url + CustomMasterPageUrl );
                    var masterPublishUri = new Uri( curWeb.Url + CustomPublishMasterPageUrl );

                    // Master page used by all froms and pages on the site that are NOT publishing pages
                    curWeb.MasterUrl = masterUri.AbsolutePath;

                    // Master page used by all publishing pages on the site
                    curWeb.CustomMasterUrl = masterPublishUri.AbsolutePath;
                    curWeb.Update();
                }

            }
        }
        catch (Exception ex)
        {
            // Handle Error here
        }
    }

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        try
        {
            using ( var curSite = ( SPSite ) properties.Feature.Parent )
            {
                using ( var curWeb = curSite.RootWeb )
                {
                    // Create full master URL
                    var masterUri = new Uri( curWeb.Url + DefaultMasterPageUrl );

                    // Master page used by all froms and pages on the site that are NOT publishing pages
                    curWeb.MasterUrl = masterUri.AbsolutePath;

                    // Master page used by all publishing pages on the site
                    curWeb.CustomMasterUrl = masterUri.AbsolutePath;
                    curWeb.Update();
                }
            }
        }
        catch ( Exception ex )
        {
            // Handle Error here
        }
    }

    public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
    {
        try
        {
            using ( var curSite = ( SPSite ) properties.Feature.Parent )
            {
                using ( var curWeb = curSite.RootWeb )
                {
                    // Create full master URL
                    var masterUri = new Uri( curWeb.Url + DefaultMasterPageUrl );

                    // Master page used by all froms and pages on the site that are NOT publishing pages
                    curWeb.MasterUrl = masterUri.AbsolutePath;

                    // Master page used by all publishing pages on the site
                    curWeb.CustomMasterUrl = masterUri.AbsolutePath;
                    curWeb.Update();
                }
            }
        }
        catch ( Exception ex )
        {
            // Handle Error here
        }
    }
1
  • I'm not sure multiple master pages are supported in SP Foundation - can you confirm?
    – KevinS
    Apr 27, 2011 at 9:47

Your Answer

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

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