none
Couldnt run JQuery in Content Editor Webpart

    Question

  • Hi All,

    I am not able to run belo JQuery snippet in the content editor webpart of a  sharepoint site.

    Below is the code snippet which i have used....

    <script type="text/javascript" src="http://sitename:100/Style%20Library/jquery-1.7.1.js" ></script>

    <script type="text/javascript"> 
    var oCtx;
    var oWeb;
    $(document).load(function() { 
    function Initial{
    oCtx = new SP.ClientContext.get_current(); 
    oWeb = oCtx.get_web(); 
     oCtx.load(oWeb);
    oCtx.executeQueryAsync(
     Function.createDelegate(this, initialiseCtxSuccess),
    Function.createDelegate(this, initialiseCtxFailed));
    }
     function initialiseCtxSuccess(sender, args) { alert('get context succeeded'); }
    function initialiseCtxFailed(sender, args) { alert('get context failed');

    function isUserAdmin() {
     var oPerm = new SP.BasePermissions();
    oPerm.set(SP.PermissionKind.manageWeb);  
    bRes = oWeb.doesUserHavePermissions(oPerm); 
     alert(bRes.get_value());   }
    });
    </script>

    Could anyone please let me know where i am going wrong?

    Thanks,

    Shailu.


    Success is not to be pursued; it is to be attracted by the person we become.

    Thursday, February 16, 2012 2:31 PM

Answers

  • Hi Shailu,

    If I understood you correctly, you want to get the client context and determine whether the current user has the permission to manage the web as the page load.

    First, if you want to use SharePoint ECMAScript, you should run it on Sharepoint 2010, SharePoint 2007 did not support the client ECMAScript.

    Second,the script you post did not work because you use SharePoint ECMAScript, and this will require SP.JS loaded first, so you can modify the code as below:


    <script src="/SiteAssets/jquery-1.7.1.js" type="text/javascript"></script>
    
    <script type="text/javascript">
    
    $(document).ready(function(){
    SP.SOD.executeOrDelayUntilScriptLoaded(initialize,'SP.js');
    SP.SOD.executeOrDelayUntilScriptLoaded(test,'SP.js');
    
      
     });
    
    function test()
    {
    var oPerm = new SP.BasePermissions(); 
    oPerm.set(SP.PermissionKind.manageWeb);   
    bRes = oWeb.doesUserHavePermissions(oPerm);  
     alert(bRes.get_value());
    }
    function initialize()
    {
    alert("hi");
    oCtx = new SP.ClientContext.get_current();  
    oWeb = oCtx.get_web();  
     oCtx.load(oWeb); 
    oCtx.executeQueryAsync( 
     Function.createDelegate(this, initialiseCtxSuccess),
     Function.createDelegate(this, initialiseCtxFailed));
    }
    function initialiseCtxSuccess(sender, args) { alert('get context succeeded'); }
    function initialiseCtxFailed(sender, args) { alert('get context failed'); }
     
     </script>



    Thanks,
    Simon


    Simon Huang

    TechNet Community Support



    • Edited by Shimin Huang Tuesday, February 21, 2012 7:55 AM
    • Marked as answer by Shimin Huang Friday, February 24, 2012 5:27 AM
    Tuesday, February 21, 2012 7:49 AM
  • Hi Shailu,

    I am afraid that the Javascript client object model only be supported in SharePoint 2010.

    For SharePoint 2007, you may consider use SPServices jQuery library to achieve your goal:
    http://spservices.codeplex.com/

    Thanks,
    Simon


    Simon Huang

    TechNet Community Support

    • Marked as answer by Shimin Huang Friday, February 24, 2012 5:27 AM
    Thursday, February 23, 2012 3:03 AM

All replies

  • Hi Shailu,

    If I understood you correctly, you want to get the client context and determine whether the current user has the permission to manage the web as the page load.

    First, if you want to use SharePoint ECMAScript, you should run it on Sharepoint 2010, SharePoint 2007 did not support the client ECMAScript.

    Second,the script you post did not work because you use SharePoint ECMAScript, and this will require SP.JS loaded first, so you can modify the code as below:


    <script src="/SiteAssets/jquery-1.7.1.js" type="text/javascript"></script>
    
    <script type="text/javascript">
    
    $(document).ready(function(){
    SP.SOD.executeOrDelayUntilScriptLoaded(initialize,'SP.js');
    SP.SOD.executeOrDelayUntilScriptLoaded(test,'SP.js');
    
      
     });
    
    function test()
    {
    var oPerm = new SP.BasePermissions(); 
    oPerm.set(SP.PermissionKind.manageWeb);   
    bRes = oWeb.doesUserHavePermissions(oPerm);  
     alert(bRes.get_value());
    }
    function initialize()
    {
    alert("hi");
    oCtx = new SP.ClientContext.get_current();  
    oWeb = oCtx.get_web();  
     oCtx.load(oWeb); 
    oCtx.executeQueryAsync( 
     Function.createDelegate(this, initialiseCtxSuccess),
     Function.createDelegate(this, initialiseCtxFailed));
    }
    function initialiseCtxSuccess(sender, args) { alert('get context succeeded'); }
    function initialiseCtxFailed(sender, args) { alert('get context failed'); }
     
     </script>



    Thanks,
    Simon


    Simon Huang

    TechNet Community Support



    • Edited by Shimin Huang Tuesday, February 21, 2012 7:55 AM
    • Marked as answer by Shimin Huang Friday, February 24, 2012 5:27 AM
    Tuesday, February 21, 2012 7:49 AM
  • Thanks for your reply, Simon.

    I am using Sharepoint 2007 environment.

    Can i run the same script on SP 2007 also? If not could you please let me know the correct script for it.

    Thanks,

    Shailu.


    Success is not to be pursued; it is to be attracted by the person we become.

    Wednesday, February 22, 2012 9:47 AM
  • Hi Shailu,

    I am afraid that the Javascript client object model only be supported in SharePoint 2010.

    For SharePoint 2007, you may consider use SPServices jQuery library to achieve your goal:
    http://spservices.codeplex.com/

    Thanks,
    Simon


    Simon Huang

    TechNet Community Support

    • Marked as answer by Shimin Huang Friday, February 24, 2012 5:27 AM
    Thursday, February 23, 2012 3:03 AM