Suppose that we need to get folder (SP.Folder) where specific list item (SP.ListItem) is located. The following code shows how to do that:
1: var ctx = SP.ClientContext.get_current();
2: var file = item.get_file();
3: ctx.load(file);
4: ctx.executeQueryAsync(
5: Function.createDelegate(this, function (sender, args) {
6: var folderUrl = file.get_serverRelativeUrl().substring(0,
7: file.get_serverRelativeUrl().lastIndexOf("/"));
8: var folder = ctx.get_web().getFolderByServerRelativeUrl(folderUrl);
9: ctx.load(folder);
10: ctx.executeQueryAsync(
11: Function.createDelegate(this, function (sender, args) {
12: ...
13: }),
14: Function.createDelegate(this, function (sender, args) {
15: console.log(args.get_message());
16: }));
17: }),
18: Function.createDelegate(this, function (sender, args) {
19: console.log(args.get_message());
20: }));
Here at first we load file (SP.File) (lines 2-3), then get relative url (lines 6-7) and by this relative url get folder (lines 8-9). JSOM documentation says that there is SP.ListItem.folder property available, but it always returned error for some reason. May be it will be fixed in future updates.
No comments:
Post a Comment