access list using java script and application page for sp2010

Answered access list using java script and application page for sp2010

  • sexta-feira, 6 de abril de 2012 05:15
     
     
    I have create an application page for a sit as follows and use java script to access the lists.

    <asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

    <script type = "text/ecmascript" src ="/hr/_layouts/Jquery/jquery-1.7.1.min.js"  />
    <script type ="text/ecmascript"  src ="/hr/_layouts/SP.js"  />

    <script language="ecmascript" type ="text/ecmascript">
        var txtCtx;
        var myweb;
        var mylist;

        function getdata() {
            alert("hello");


            txtCtx = SP.ClientContext.get_current();
            myweb = txtCtx.get_web();
            mylist = myweb.get_lists();      
          
           
           
        }


    </script>

    </asp:Content>

    <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <ul id ="ulv"></ul>
    <div id ="Divlv"></div>
    <input type ="button" runat ="server" name ="test"  onclick ="getdata()"  title="click" />
    </asp:Content>

    <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Application Page

    </asp:Content>

    <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
    My Application Page
    <asp:Label runat ="server" Text ="my test page " />
    </asp:Content>


    When i run the program it will run fine but when i click the button i mm getting following error on "Onclick= "getdata()"
    error is => Microsoft JScript runtime error: Object expected
    but when i remove
    <script type = "text/ecmascript" src ="/hr/_layouts/Jquery/jquery-1.7.1.min.js"  />
    <script type ="text/ecmascript"  src ="/hr/_layouts/SP.js"  />

    and replace with
    <SharePoint:ScriptLink name ="sp.js" LoadAfterUI= "true" Localizable ="false" runat ="server" />

    I can click the  nutton without any error , BUT WHEN I USE THIS SHAREPOINT:SCRIPTLINK I CANT GET FOLLOWINGS
           txtCtx = SP.ClientContext.get_current();<= IT WONT GIVE THIS SP.SP.ClientContext.get_current()
            myweb = txtCtx.get_web();<=
            mylist = myweb.get_lists();<=


    CAN SOME ONE HELP ME TO SOLVE THIS ISSUE






    d.n weerasinghe

Todas as Respostas

  • sábado, 7 de abril de 2012 14:55
     
     

    i think you need to post this in the SharePoint 2010 forums

    kind regards,

    paul Keijzers


    Check my website http://www.kbworks.nl or follow me on @KbWorks be sure to Check my SharePoint-Specialist.nu for dutch informationworkers check Wat Is microsoft SharePoint.nl for dutch readers who want to know what microsoft office365 is.

  • segunda-feira, 16 de abril de 2012 06:27
     
     Respondido Contém Código

    Hi d.n weerasinghe,

    Thanks for your post!

    Below is a sample code to access a list and display its title, please refer, and the main issue of your code is that you did not run your code asynchronous, so actually you need this line code like below:

    ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

    The sample code:

    <script type="text/ecmascript">
    var list;
    function runCode() {
       var ctx = new SP.ClientContext.get_current();
        var web = ctx.get_web();
        list = web.get_lists().getByTitle("list");
       ctx.load(list);
       ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
    
    function onQuerySucceeded() {
      alert(list.get_title());    
    }
    
    function onQueryFailed(sender, args) {
      alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }</script><input name="ClientDemo" onclick="runCode();" type="button" value="Get Current User"/> 

    And you do not need this line:

    <script type ="text/ecmascript"  src ="/hr/_layouts/SP.js"  /> OR <SharePoint:ScriptLink name ="sp.js" LoadAfterUI= "true" Localizable ="false" runat ="server" />

    By default, SharePoint will load the SP.js automatically.

    Thanks,
    Simon


    Simon Huang

    TechNet Community Support

    • Marcado como Resposta newnw segunda-feira, 16 de abril de 2012 10:19
    •