Welcome to my blog, stay tunned :
Home | Blogs | Stephane Eyskens's blog

EcmaScript Client Object Model : How to get the list of items/files/folders of the current user?

Hi All,

The code snippet below shows how to retrieve the currently logged in user and to retrieve his documents/folders or listitems/folders according to the type of list you're working with:

The first things you need to do is to prepare the objects such as ClientContext, the current web & site, the current user (get_currentUser()), then you load them and you call your first asynchroneous execution.
Once executed, all the objects are populated with their medatadata. In OnLoadQuerySucess, you can prepare your CAML query that's recursively taking all the items of the currently logged in user.

You need to load the resulting item collection this way :

this.ClientContext.load(this.CollListItem,'Include(DisplayName, ContentType, File, Id, Created, Modified)');

The Include keyword is very important because we tell SharePoint that we want to retrieve some metadata such as DisplayName, Id, Created and Modified and that we plan to work with the SPFile and SPContentType objects. If you don't include those, you won't be able to perform operations such as SPFile.get_name().

At last, the third part is to iterate through the returned item collection and to display the output. You'll notice that the following calls ContentType.get_name(), File.get_timeCreated() etc... were only made possible by the prior inclusion above.

Here is the result of the above on a list containing one document :

I'll soon publish a new tool on http://sptoolbasket2010.codeplex.com/ with a new toolbar associated to every type of list.

Cheers

Comments

Get File size of the attachment

Thank you for the lovely post.
I have one question: how do I get the file size of the attachment?
I tried using File.get_length(); and it gives an error "Object doesn't support property or method 'get_length' ".
I get this in error in SharePoint 2010 and the same works good in SharePoint 2013.
can you please help me resolve this?
is there any js file reference that's missing in SP2010?

Thank you!.

Error in reading items using ECMAScript

I am getting this error: The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.
And my list contains 28000 records and I am using ECMAScript to read data. And I am reading only 1 item from the list by using query,

var q = "
<View>
<Query>
<Where>
<And>
<And>
<Eq><FieldRef Name='QuestionnaireID' /><Value Type='Lookup'>56</Value></Eq>
<Eq><FieldRef Name='Evalution' /><Value Type='Lookup'>Q3-2011</Value></Eq>
</And>
<Eq><FieldRef Name='Hotel' /><Value Type='Lookup'>933</Value></Eq>
</And>
</Where>
</Query>
<RowLimit>1</RowLimit>
</View>";

Don't know why it is throwing error. Please help.

List threshold

Hi,

The Client Object Model is also impacted by the treshold limits but the problem has nothing to see with ECMAScript...You have to review your treeshold policies or review the way you store your list items/documents. You can also make use of indexing.

Go read this MSDN article for more info:

http://msdn.microsoft.com/en-us/library/ff798465.aspx

Best Regzards

Screenshots of Code Samples

Why do people continue to post screenshots of code samples?

screenshots

Hi,

It's not the case anymore (see my newer posts)...but I understand your frustration, that's why I installed the necessary javascript stuff in order to let people copy/paste the code samples.

Cheers