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 am a long-time ASP.NET developer but a first-time SharePoint developer. One of the most powerful features of ASP.NET custom scriptcontrols. Yet when I try to apply these controls to a custom SharePoint 2010 solution, i have run into nothing but trouble.

Most commonly, the script references are not loaded, so the script descriptors throw a "variable is undefined" error. Usually after I deploy the solution from visual studio, everything will work for only one request; script references are not loaded on additional requests. I have traced through my code & confirmed that my control's GetScriptReferences method is being called.

The following code is an example of a control i cannot get to work in Sharepoint 2010:

HelloWorld.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Reflection;

[assembly: WebResource(Test.Controls.HelloWorld.ClientResource.Release, "text/javascript")]

namespace Test.Controls {
    public class HelloWorld : ScriptControl {

        public static class ClientResource {
            public const string Release = "Test.Controls.HelloWorld.js";
        }

        protected class HelloWorldScriptDescriptor : ScriptControlDescriptor {

            private const string ClientType = "Test.HelloWorld";

            public HelloWorldScriptDescriptor(HelloWorld control) : base(ClientType, control.ClientID) { }
        }

        public static readonly IEnumerable<ScriptReference> ScriptReferences = new ScriptReference[] {
            new ScriptReference(ClientResource.Release, Assembly.GetExecutingAssembly().FullName)
        };

        protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors() {
            yield return new HelloWorldScriptDescriptor(this);
        }

        protected override IEnumerable<ScriptReference> GetScriptReferences() {
            return ScriptReferences;
        }
    }
}

HelloWorld.js

Type.registerNamespace("Test");

Test.HelloWorld = function (element) {
    Test.HelloWorld.initializeBase(this, [element]);
};

Test.HelloWorld.prototype = {
    initialize: function () {
        Test.HelloWorld.callBaseMethod(this, "initialize");
        this.get_element().innerHTML = "Hello World";
    }
};

Test.HelloWorld.registerClass("Test.HelloWorld", Sys.UI.Control);

HelloWorldUserControl.ascx

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HelloWorldUserControl.ascx.cs" Inherits="Test.HelloWorld.HelloWorldUserControl" %>
<%@ Register TagPrefix="test" Namespace="Test.Controls" Assembly="$SharePoint.Project.AssemblyFullName$" %>

<test:HelloWorld ID="testHelloWorld" runat="server" />

This exact code works in an non-SharePoint ASP.NET web application.

I appreciate your time.

share|improve this question
    
Can you explain what it is exactly that you are trying to do? –  Dave Wise Feb 27 '12 at 22:01
    
I am trying to add a custom script control (as described at here) to a custom webpart in Visual Studio. The control above is a simple "Hello World" example that reproduces the issue i am having. –  Markus Feb 29 '12 at 15:12
add comment

1 Answer

For some reason, GetScriptReferences cannot return my custom ScriptReferences property if it is static. Making it a non-static property resolved the issue. Not sure why it works in ASP.NET & not SharePoint.

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.