Javascript csom to access Page Properties
Asked Answered
E

1

6

I have some wiki pages in SharePoint 2013 Onpremise "Site Pages" Library. I have created a column "Priority" in the library . I want to access the Page Property from Client Side. I know this is possible from Server side by the following code:

SPContext.Current.ListItem["FieldName"]

But i want to access the Page properties from Client Side , is it possible ?

Exemplary answered 31/8, 2014 at 19:29 Comment(0)
I
9

Since SPContext.Current gets the context of the current HTTP request and SPContext.Current.ListItem returns the current List Item I assume you to need a similar functionality in JSOM.

In SharePoint the structure _spPageContextInfo is available on every page on the client-side that could be considered to some extent as analogue to SPContext.Current. In particular, the following properties are exposed to identify the current List Item:

  • pageListId - gets the current List Unique Id
  • pageItemId - gets the current List Item Id

How to retrieve current List Item using JSOM

The following function demonstrates how to retrieve current List Item:

function getCurrentListItem(success, error)
{
   var context = SP.ClientContext.get_current();
   var web = context.get_web(); 
   var currentList = web.get_lists().getById(_spPageContextInfo.pageListId); 
   var currentListItem = currentList.getItemById(_spPageContextInfo.pageItemId);
   context.load(currentListItem);
   context.executeQueryAsync(
     function(){
        success(currentListItem);
     }, 
     error
   );
}

Usage

getCurrentListItem(
   function(listItem) {
       var priority = listItem.get_item('Priority'); 
       console.log(priority);
   },
   function(sender,args){
        console.log(args.get_message());    
   }
);
Improvised answered 31/8, 2014 at 20:53 Comment(2)
I am not able to do this when page is not checked out is there a way where I can add .CheckOut() and do this?Ludovick
I am trying listItem.set_item() which works when page is checked outLudovick

© 2022 - 2024 — McMap. All rights reserved.