0

I am currently using SP.ClientContext via JavaScript to display items from a SharePoint list. I am able to display the items via an alert or via input button onClick to display the data. However, I want it to display onLoad.

I am currently using an iframe tag to do so but it runs very slow. When I run the Dev Tool in Chrome it states "Page cannot be found" (I suspect it's trying to locate the source and I'm not referencing the source correctly...currently using src="DispForm.aspx" but I'm sure that's incorrect).

Is there another way of displaying the items onLoad via JavaScript or finding the correct src reference for the iframe tag? Below is the code I am using:

    <!--iframe is slow...maybe because it's looking for DispForm.aspx?-->
    <iframe style="display:none" onload="runCode()" src="/DispForm.aspx"></iframe>
    <span id="Sig"></span>
    <script type="text/javascript">

        var itemId = GetUrlKeyValue("ID");  
        var targetListItem;
        ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");               
       function runCode() {
         var clientContext = new SP.ClientContext(); 
         var targetList = clientContext.get_web().get_lists().getByTitle(myList');
         targetListItem = targetList.getItemById(itemId);
         clientContext.load(targetListItem, 'ws_x002d_sig');
         clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
       }

       function onQuerySucceeded() {
            if (targetListItem.get_item("ws_x002d_sig")==="" || targetListItem.get_item("ws_x002d_sig")==null) {
                var prefVar= "<a href='#ws'>Sign</a>";
            }
            else {
                var prefVar=targetListItem.get_item('ws_x002d_sig');
            }

            var myObject = {
                prefObject: function() {
                    return prefVar;
                }
            }

            x = myObject.prefObject();
            document.getElementById("Sig").innerHTML = x;
            //"alert works great" alert('Request succeeded. \n\nRetrieved Item is: ' + targetListItem.get_item('ws_x002d_sig'));
        }

       function onQueryFailed(sender, args) {
         alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
       }                

    </script>

    <!--input button works great -->
    <!--<input id="Button1" type="button" value="Run Code" onclick="runCodeWs()" />-->
0

Can't you use jQuery $(document).ready() Method? I've used in similar situation to retrieve context's user role to show/hide certain sections of the page.

Here is a jQuery Code Snippet; was added using SharePoint Designer.

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">

<script type="text/javascript" src="../../SiteAssets/jquery.min.js"/>
<script type="text/javascript" src="../../SiteAssets/jquery.SPServices-0.7.1a.min.js"/>

<script language="javascript" type="text/javascript">

   var LoggedOnUserRole="";   
   var dialogResult;

    $(document).ready(function () {

    //run the code only after sp.js has been loaded
      SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){

      dialogResult= SP.UI.ModalDialog.showWaitScreenWithNoClose("", "Please wait..", 100, 200);       

      GetCurrentUserRoles();

      dialogResult.close(0);   

      });
  }); 
    </script>   
</asp:Content>

  • Thanks. This is something I might use on another component of the SharePoint site. When a user clicks on "sign", if I'm reading your code correctly, a modal popup will show. I'll just capture the users "ID", add a date stamp, and add the "signature" as a list item. – adams-j Oct 22 at 13:24
  • No, the pop-up here is for wait screen. You can get user info by getting currentWeb from the current context and then currentWeb.get_currentUser(); I can share "GetCurrentUserRoles()" function if you would like – Matiur Rahman Oct 23 at 14:38
  • thanks, I think I got the get current user. I was looking for the SP.UI.ModalDialog as one of my solutions. I think I can get your wait screen and use it as a popup signature though that I was thinking of creating. I'll probably have to create a separate post for that though (using Modal dialog box form to update items in a list). – adams-j Oct 24 at 12:32
0

Sample test demo( not sure the requirement why you get item id from iframe url).

<iframe id="MyIFrame" style="display:none" src="/Lists/MyList/DispForm.aspx?ID=1"></iframe>
    <span id="Sig"></span>
    <script type="text/javascript">

        var itemId ;
        var targetListItem;
        ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
        function runCode() {
            var clientContext = new SP.ClientContext();
            var targetList = clientContext.get_web().get_lists().getByTitle('MyList');
            targetListItem = targetList.getItemById(itemId);
            clientContext.load(targetListItem, 'Title');
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }

        function onQuerySucceeded() {
            if (targetListItem.get_item("Title") === "" || targetListItem.get_item("Title") == null) {
                var prefVar = "<a href='#ws'>Sign</a>";
            }
            else {
                var prefVar = targetListItem.get_item('Title');
            }

            var myObject = {
                prefObject: function () {
                    return prefVar;
                }
            }

            x = myObject.prefObject();
            document.getElementById("Sig").innerHTML = x;
            //"alert works great" alert('Request succeeded. \n\nRetrieved Item is: ' + targetListItem.get_item('ws_x002d_sig'));
        }

        function onQueryFailed(sender, args) {
            alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
        }
        function Initialize() {
            var CurrentUrl = document.getElementById('MyIFrame').contentWindow.location.href;
            itemId = GetUrlKeyValue('ID', false, CurrentUrl)
            runCode();
        }
    </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.