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.