11

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();
6
  • I tested your code. It works fine for me. So it must be something else. Commented Mar 5, 2012 at 15:26
  • I updated the question with all code of the .JS, any idea what should I look for?, when I use the method loadCurrentWebLists it WORKs Commented Mar 5, 2012 at 15:47
  • The 'Object doesn't support this property or method' is VERY generic - it means that youre trying to call a method or property that doesn't defined. It will be much easier to help if you debug your code in IE Developer toolbar for example and tell us what's the method/property that's undefined.
    – Alex Boev
    Commented Mar 5, 2012 at 15:53
  • And I still strongly insist that a page component is unnecessary complex for your task - the simple command handler with the function call is enough.
    – Alex Boev
    Commented Mar 5, 2012 at 16:42
  • can you please provide an example? do you mean put the js code in the elements.xml file embedded? Why? Actually I like to separate the files, I think maintenance is easier/! Commented Mar 6, 2012 at 16:01

2 Answers 2

5

This error means that you are trying to get some property that you didn't request explicitly. Maybe it's the Title field you're trying to use. Look at the example from here:

       function retrieveListItems() {

        var clientContext = new SP.ClientContext('/sites/MySiteCollection');
        var oList = clientContext.get_web().get_lists().getByTitle('My List');

        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><Where><Leq>' + 
            '<FieldRef Name=\'ID\'/><Value Type=\'Number\'>100</Value>' + 
            '</Leq></Where></Query><RowLimit>50</RowLimit></View>');

        this.collListItem = oList.getItems(camlQuery);

        clientContext.load(collListItem, 'Include(Id, DisplayName)');

        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() + 
            '\nDisplay name: ' + oListItem.get_displayName();
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

It's the way to tell client object model what fields do you want. The interesting part of this example is:

clientContext.load(collListItem, 'Include(Id, DisplayName)');
4
  • that syntax is .net, not javascript, searching I tried this context.Load(allItems,'Include(Title, Id)'); but now it says Object doesn't support this property or method @Alex Boev Commented Mar 5, 2012 at 15:11
  • Oops, I didn't notice that I get the wrong example from that page. I've updated the answer.
    – Alex Boev
    Commented Mar 5, 2012 at 15:20
  • Please see the updated question, I tried to do the same, but I still get the exception. I just cant find any difference. Commented Mar 5, 2012 at 15:39
  • I have updated ALL the code of the .JS file. Commented Mar 5, 2012 at 15:45
1

try with get_id() instead of get_item('Id');

var oItem ='';
function retrieveWebSite() {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
        var clientContext = new SP.ClientContext.get_current();
        this.oWebsite = clientContext.get_web();
        clientContext.load(this.oWebsite);

        var lstObject = oWebsite.get_lists().getByTitle('Listname');
        oItem = lstObject.getItemById(5);
        clientContext.load(oItem);

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

function onQuerySucceeded(sender, args) {
    var look  = oItem.get_item('LookupColumnName').get_lookupValue();
    var title = oItem.get_item('Title');
    var id = oItem.get_id();
    alert("Loook up column value: "+look);
    alert("Title column: "+title);
    alert("Id column value: "+id);
}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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