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'd like to have a feature which adds a script reference to all pages in a site collection when activated.

This seems like a scenario where I should just modify the master page, but for it to affect all pages in the site collection, it will need to modify every master page.

Is it possible to get a list of all master pages so that I can iterate over them and add the script reference?

I have it working with a single master page using:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite site = properties.Feature.Parent as SPSite;
    SPWeb web = site.RootWeb;
    string url = web.MasterUrl;
    SPFile file = web.GetFileOrFolderObject(url) as SPFile;

    /*customize file here*/
}

Are there any differences between SharePoint 2010 and 2013 which will require a different solution in one than the other?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

I can see several issues here: 1. I fail to understand how come you have so many master pages, as for sure (unless you have a specific business scenario) you shouldn't have that many. In either case, do NOT modify the out-of-the-box. 2. If you by "script reference" you mean some JavaScript, there is a simpler, better way starting SharePoint 2010, and that is to create a new Feature that deploy a CUSTOM ACTION, which instead uses Scriptlink such as :

<CustomAction Location="ScriptLink" ScriptSrc="/_layouts/.../your_javascript_file.js" Sequence="100">

alternatively you could also do something like:

<CustomAction Location="ScriptLink" ScriptBlock="alert('hello');" Sequence="100">

Neither of the above would require changing your master page in any way, and as soon as any page is loaded the script will be executed as part of the life-cycle.

share|improve this answer
    
Thanks for the answer. To address your points: 1. The problem isn't that I have a lot of master pages, it is that this feature will be deployed in other SharePoint instances of which I have no knowledge of. What is the downside to modifying the out-of-the-box master pages? 2. Yes, by script reference I mean a basic <script src="foo.js> Thanks for mentioning custom actions. I'll start looking into them. –  Jeff Jan 10 '13 at 22:58
    
It's one of my fav approaches where you place the scripts without having to modify the pages or master pages, in real. –  Falak Mahmood Jan 10 '13 at 23:47
    
Copy and modify is the standard best practice. This gives you a fall back and doesn't hose you if a service pack should modify the file. –  PirateEric Jan 10 '13 at 23:49
1  
Just to elaborate more, this method requires an element file to inject the script into site. The source JavaScript can be either manually placed in appropriate location in the SharePoint farm or it can be deployed through a Module. The location attribute in CustomAction element should be set as "ScriptLink". The sequence number attribute in CustomAction element decides the order in which the script has to be loaded into the page. –  Falak Mahmood Jan 10 '13 at 23:55
    
Just as Falak detailed that is exactly the process. I find it quite un-intrusive as it does work in all situations –  C. Marius - MVP Jan 11 '13 at 13:43

What you describe is how the DelegateControl works: http://msdn.microsoft.com/en-us/library/ms463169(v=office.12).aspx

(The CustomAction that Marius describes will also work.)

share|improve this answer
    
Delegate control is also an option to inject JS into a Master Page without editing it. This usually is doable with a delegate control with control id AdditionalPageHead –  Falak Mahmood Jan 10 '13 at 23:57

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.