The below question was based on this article: http://www.learningsharepoint.com/2011/09/10/get-all-folders-using-ecmascriptjavascript-client-object-model-sharepoint-2010/
I think the author there didnt post all code, because a simple copy and paste Just does not work.
The following code is supposed to query for the folders list in a specific site pages library. I think the problem is in the success method, also I have no idea how to debug .js files in sharepoint? any clue?
But its showing this exception on Internet Explorer status bar: Webpage error details
Update1 I tried this line
context.Load(allItems,'Include(Title, Id)');
But it throws this exception
Object doesn't support this property or method
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E) Timestamp: Mon, 5 Mar 2012 14:41:29 UTC
Message: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
Line: 5
Char: 62099
Code: 0
Type.registerNamespace('COB.SharePoint.Ribbon.PageComponent');
COB.SharePoint.Ribbon.PageComponent = function () {
COB.SharePoint.Ribbon.PageComponent.initializeBase(this);
}
// the initialize function needs to be called by some script added to the page elsewhere - in the end, it does the important work
// of calling PageManager.addPageComponent()..
COB.SharePoint.Ribbon.PageComponent.initialize = function () {
ExecuteOrDelayUntilScriptLoaded(Function.createDelegate(null, COB.SharePoint.Ribbon.PageComponent.initializePageComponent), 'SP.Ribbon.js');
}
COB.SharePoint.Ribbon.PageComponent.initializePageComponent = function () {
var ribbonPageManager = SP.Ribbon.PageManager.get_instance();
if (null !== ribbonPageManager) {
ribbonPageManager.addPageComponent(COB.SharePoint.Ribbon.PageComponent.instance);
}
}
COB.SharePoint.Ribbon.PageComponent.prototype = {
init: function () { },
getFocusedCommands: function () {
return ['COB.PageComponent.Command.FieldControl.GroupCommand', 'COB.PageComponent.Command.FieldControl.TabCommand', 'COB.PageComponent.Command.FieldControl.ContextualGroupCommand', 'COB.PageComponent.Command.FieldControl.RibbonCommand'];
},
getGlobalCommands: function () {
return ['COB.PageComponent.Command.DoAction', 'COB.PageComponent.Command.PopulateDropDown', 'COB.PageComponent.Command.QueryDoAction'];
},
canHandleCommand: function (commandId) {
if ((commandId === 'COB.PageComponent.Command.DoAction') ||
(commandId === 'COB.PageComponent.Command.PopulateDropDown') || (commandId === 'COB.PageComponent.Command.QueryDoAction')) {
return true;
}
else {
return false;
}
},
handleCommand: function (commandId, properties, sequence) {
if (commandId === 'COB.PageComponent.Command.FieldControl.GroupCommand') {
alert("COB.PageComponent.Command.FieldControl.GroupCommand fired");
}
if (commandId === 'COB.PageComponent.Command.FieldControl.TabCommand') {
alert("COB.PageComponent.Command.FieldControl.TabCommand fired");
}
if (commandId === 'COB.PageComponent.Command.FieldControl.ContextualGroupCommand') {
alert("COB.PageComponent.Command.FieldControl.ContextualGroupCommand fired");
}
if (commandId === 'COB.PageComponent.Command.FieldControl.RibbonCommand') {
alert("COB.PageComponent.Command.FieldControl.RibbonCommand fired");
}
if (commandId === 'COB.PageComponent.Command.QueryDoAction') {
// this command executes as soon as tab is requested, so do initialization here ready for if our dropdown gets requested..
//loadCurrentWebLists();
GetFolders();
}
if (commandId === 'COB.PageComponent.Command.PopulateDropDown') {
// actually build the dropdown contents by setting the PopulationXML property to a value with the expected format. We have to deal with possible
// timing issues/dependency on core SharePoint JS code with an ExecuteOrDelay..
ExecuteOrDelayUntilScriptLoaded(Function.createDelegate(null, getDropdownItemsXml), 'SP.js');
properties.PopulationXML = getDropdownItemsXml();
}
if (commandId === 'COB.PageComponent.Command.DoAction') {
// here we're using the SourceControlId to detect the selected item, but more normally each item would have a unique commandId (rather than 'DoAction').
// However this isn't possible in this case since each item is a list in the current web, and this can change..
var selectedItem = properties.SourceControlId.toString();
var listName = selectedItem.substring(selectedItem.lastIndexOf('.') + 1);
alert("You selected the list: " + listName);
}
},
isFocusable: function () {
return true;
},
receiveFocus: function () {
return true;
},
yieldFocus: function () {
return true;
}
}
// **** BEGIN: helper code specific to this sample ****
// some global variables which we'll use with the async processing..
// var lists = null;
var querySucceeded = false;
// use the Client Object Model to fetch the lists in the current site..
function loadCurrentWebLists() {
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
this.lists = web.get_lists();
clientContext.load(lists);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed));
}
var allItems;
function GetFolders() {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('Site Pages');
var query = SP.CamlQuery.createAllFoldersQuery();
allItems = list.getItems(query);
//context.load(allItems);
//context.Load(allItems, 'Include(Id, DisplayName)');
context.load(allItems, 'Include(Id, DisplayName)');
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success() {
var FolderNames = "";
var ListEnumerator = this.allItems.getEnumerator();
while (ListEnumerator.moveNext()) {
var currentItem = ListEnumerator.get_current();
var context = new SP.ClientContext.get_current();
context.load(currentItem);
var _contentType = currentItem.get_fileSystemObjectType();
if (currentItem.get_fileSystemObjectType() == '1') {
FolderNames += currentItem.get_displayName() + '\n';
}
}
alert(FolderNames);
}
function failed(sender, args) {
alert("failed. Message:" + args.get_message());
}
function onQuerySucceeded() {
querySucceeded = true;
}
function onQueryFailed(sender, args) {
querySucceeded = false;
}
function getDropdownItemsXml() {
var sb = new Sys.StringBuilder();
sb.append('<Menu Id=\'COB.SharePoint.Ribbon.WithPageComponent.PCNotificationGroup.Dropdown.Menu\'>');
sb.append('<MenuSection DisplayMode=\'Menu\' Id=\'COB.SharePoint.Ribbon.WithPageComponent.PCNotificationGroup.Dropdown.Menu.Manage\'>');
sb.append('<Controls Id=\'COB.SharePoint.Ribbon.WithPageComponent.PCNotificationGroup.Dropdown.Menu.Manage.Controls\'>');
if (querySucceeded) {
var listEnumerator = lists.getEnumerator();
while (listEnumerator.moveNext()) {
var oList = listEnumerator.get_current();
sb.append('<Button');
sb.append(' Id=\'COB.SharePoint.Ribbon.WithPageComponent.PCNotificationGroup.Dropdown.Menu.Manage.');
sb.append(oList.get_title());
sb.append('\'');
sb.append(' Command=\'');
sb.append('COB.PageComponent.Command.DoAction');
sb.append('\'');
sb.append(' LabelText=\'');
sb.append(SP.Utilities.HttpUtility.htmlEncode(oList.get_title()));
sb.append('\'');
sb.append('/>');
}
}
sb.append('</Controls>');
sb.append('</MenuSection>');
sb.append('</Menu>');
return sb.toString();
}
// **** END: helper code specific to this sample ****
COB.SharePoint.Ribbon.PageComponent.registerClass('COB.SharePoint.Ribbon.PageComponent', CUI.Page.PageComponent);
COB.SharePoint.Ribbon.PageComponent.instance = new COB.SharePoint.Ribbon.PageComponent();