0

SharePoint 2010

I am trying to find a method that gets all the items(id, URL ..) from an existing document library. I found a guide from https://msdn.microsoft.com/en-us/library/office/hh185007(v=office.14).aspx and I am following it.

I created a new site called MySite1 and inside it I created two document libraries 'MyDocuments1' and 'MyDocuments2'. I put some documents in MyDocuments1, and I wrote a html file and put it in MyDocuments2. The html file contains JavaScript code used for retrieving list items from MyDocuments1. Here is what it looks like (very similar to the guide):

    <script type="text/javascript">
        ExecuteOrDelayUntilScriptLoaded(retrieveListItems,"sp.js");
        function retrieveListItems(){
            var clientContext = SP.ClientContext.get_current();
            var oList = clientContext.get_web().get_lists().getByTitle('MyDocuments1');

            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
                '<Value Type=\'Number\'>100</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
            this.collListItem = oList.getItems(camlQuery);

            clientContext.load(collListItem);

            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }

        function onQuerySucceeded(sender, args) {

            var listItemInfo = '';

            var listItemEnumerator = collListItem.getEnumerator();

            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                listItemInfo += '\nID: ' + oListItem.get_id() + 
                '\nTitle: ' + oListItem.get_item('Title') + 
                '\nBody: ' + oListItem.get_item('Body');
            }

            alert(listItemInfo.toString());
        }           

        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    </script>

And I included all the 5 source files I need in correct order:

<script  type="text/javascript" src="/_layouts/1033/init.js"></script>
<script  type="text/javascript" src="/_layouts/MicrosoftAjax.js"></script>
<script  type="text/javascript" src="/_layouts/sp.core.js"></script>
<script  type="text/javascript" src="/_layouts/sp.runtime.js"></script>
<script  type="text/javascript" src="/_layouts/sp.js"></script>

But when I tried to open this page, I was getting this error:

Uncaught TypeError: Cannot read property 'webServerRelativeUrl' of undefined sp.js:2, which came from SP.ClientContext.get_current().

If I replace

` var clientContext = SP.ClientContext.get_current();`
 with
 `var siteUrl = '/MySite1/';var clientContext = SP.CilentContext.get_current(siteUrl)`

I still get some other errors.

I have no idea why I got this error and how to fix it. Any suggestions will be very appreciated.

0

You should create a new Page using Site Actions -> Add New Page.

Then edit the page (either useing SharePoint Designer) or in (Browser). And add the JavaScript. This will make sure the respective SharePoint context and libraries get loaded properly.

The approach you are following by creating a plain html file will not work straight forward. You can make it work but that will not help you in any case.

  • Thanks for your help. I tried to followed your instruction and created a new page in the site 'MySite1'. After I clicked Site Actions -> Edit Page, I noticed that there is a 'HTML' button at the top-right side. I clicked on it and in the drop down list I selected 'edit HTML Source', then I added my script code. But after I clicked OK, nothing happened in that page and nothing showed up. I also tired a very simple JavaScript code: <script> alert('hello'); </script> but still got nothing. Do you know what happened and how to fix it? I am new to sharepoint so thanks for your patience. – Yuki Snow Jul 25 '16 at 23:34
  • Since you are not using SharePoint Designer... Edit Page -> Add a new Webpart -> Content Editor Webpart.. Then paste the html inside that....ucdenver.edu/about/departments/WebServices/sharepoint/… – Amal Hashim Jul 25 '16 at 23:36

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.