0

I'm using Sharepoint 2013 JS Client Object Model to retrieve and process the list of Pages. For each page I collect information like title etc. Each page contains also AuthorId property, which I would like to use to retrieve author's name. How to do that?

Here is my sample code:

 var ctx = new SP.ClientContext(siteUrl);
 var items = ctx.get_web().get_lists().getByTitle('Pages').getItems(new SP.CamlQuery());
 ctx.load(items);
 ctx.executeQueryAsync(function () {
      var listEnumerator = items.getEnumerator();
      while(listEnumerator.moveNext()) {
          var page = listEnumerator.get_current();
          alert(page.get_item('Title') + ' - ' + ...);
      }
 });

In addition I would also like to retrieve page url.

filip
  • 1,444
  • 1
  • 20
  • 40

1 Answers1

2

I think what you're looking for the the Include parameter of SP.ClientContext.load. Its not well documented by Microsoft but there are examples on the Web. On the MSDN page, there is a community addition at the end that talks a little about it (http://msdn.microsoft.com/en-us/library/office/dn168903(v=office.15).aspx).

var ctx = new SP.ClientContext(siteUrl);
 var items = ctx.get_web().get_lists().getByTitle('Pages').getItems(new SP.CamlQuery());
 ctx.load(items, "Include(Id, Title, Author)");
 ctx.executeQueryAsync(function () {
      var listEnumerator = items.getEnumerator();
      while(listEnumerator.moveNext()) {
          var page = listEnumerator.get_current();
          alert(page.get_item('Title') + ' - ' + page.get_item('Author'));
      }
 });

EDIT

In this next sample I have added get_lookupvalue to the Author and included EncodedAbsUrl, FileDirRef, and FileRef for you to try out for the path. The one you use depends on your needs but I suspect you'll want EncodedAbsUrl or FileRef. Here is an example of what each returns:

EncodedAbsUrl: http://yourserver.com/Pages/home.aspx

FileDirRef: /Pages

FileRef: /Pages/home.aspx

var ctx = new SP.ClientContext(siteUrl);
 var items = ctx.get_web().get_lists().getByTitle('Pages').getItems(new SP.CamlQuery());
 ctx.load(items, "Include(Id, Title, Author, EncodedAbsUrl, FileDirRef, FileRef)");
 ctx.executeQueryAsync(function () {
      var listEnumerator = items.getEnumerator();
      while(listEnumerator.moveNext()) {
          var page = listEnumerator.get_current();
          alert(page.get_item('Title') + ' - ' + page.get_item('Author').get_lookupValue() + ' - ' + page.get_item('EncodedAbsUrl') + ' - ' + page.get_item('FileDirRef') + ' - ' + page.get_item('FileRef'));
      }
 });
Justin Russell
  • 488
  • 2
  • 10
  • This is close but page.get_item('Author') will return an object. I'm not sure how to access author's name? Also, how to get page url? – filip Jul 17 '14 at 21:18
  • Thank you for the answer. I was struggling to get the Author name from the list item. I was missing .get_lookupValue(). – MunkeyWrench Sep 18 '15 at 22:45