Showing posts with label sandbox solutions. Show all posts
Showing posts with label sandbox solutions. Show all posts

Tuesday, December 20, 2011

Programmatically add Javascript or CSS to pages in SandBox web part

When you are creating a farm solution, it is easy to include javascript files into site pages using a CustomAction. You can add the JavaScript file to _layouts folder and use as:

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

The problem is, in sandboxed solutions you are not allowed to deploy files to the file system (_layouts), and it is not possible to reference Assets files. I'd recommend you to use native SharePoint controls: CssRegistration and ScriptLink

Example

protected override void CreateChildControls()
{
    base.CreateChildControls();
    this.Controls.Add(new ScriptLink()
    {
        Name = "/_layouts/MyFolder/MyScript.js",
        Language  = "javascript",
        Localizable  = false
    });
    this.Controls.Add(new CssRegistration()
    {
        Name = "/_layouts/MyFolder/MyStyles.css"
    });
}

SharePoint AI Innovations Reach General Availability

Microsoft continues to strengthen the foundation for AI-powered workplaces with the announcement that several SharePoint AI innovations are ...