Search code examples
jquerysharepoint-2010migrationsharepoint-2013web-parts

SharePoint custom web part jQuery is "undefined"


I am trying to move a custom web app (contacts list) from sharepoint 2010 to sharepoint 2013. The contact list works by using jQuery to show a name with a picture, and after clicking on the dark gray bar reveals the rest of the contact information like this: https://i.sstatic.net/hVtOj.jpg

The list works perfectly in sharepoint 2010 but not in 2013. Here is the code that seems to be breaking:

<script type="text/javascript" language="javascript">
jQuery('#' + '<%= firstpane.ClientID %>' + " div.menu_head").click(function () 
{
jQuery(this).css({ backgroundImage: "" }).next("table.menu_body").slideToggle(100).siblings("table.menu_body").slideUp("fast");
jQuery(this).siblings().css({ backgroundImage: "" });
});

</script>

In sharepoint 2010 when the contact bar is clicked, the table of information is shown perfectly. In 2013, the contact bar is shown but when clicked does not toggle. When I run this in developer tools I get an error stating:

in IE: SCRIPT5009: 'jQuery' is undefined

in chrome: Uncaught ReferenceError: jQuery is not defined

If I try and change jQuery to $ then I get the same error but instead it says $ is not defined. I checked the permissions of the jQuery file in sharepoint and it says the file is there and accessible. Does anyone know how to fix this? Thanks!

Roger


Solution

  • You can create a visual webpart and add all js file which you need with the this code

         protected void Page_Load(object sender, EventArgs e)
        {
    
            Type csType = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
    
            String jQueryURL0 = path+ "_layouts/15/jquery/js/jquery-1.9.1.min.js";
            if (!cs.IsClientScriptIncludeRegistered(csType, "jquery.js"))
            {
                cs.RegisterClientScriptInclude(csType, "jquery.js", jQueryURL0);
            }
    
        }
    

    and add jquery link to your webpart

    <SharePoint:ScriptLink ID="jqgrid" runat="server" Name="/_layouts/15/jquery/js/jquery-1.9.1.min.js"></SharePoint:ScriptLink>
    

    Or add the link to jquery to the masterpage directly