Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript csom to access Page Properties

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 ?

like image 462
Godwin Avatar asked Aug 31 '14 19:08

Godwin


1 Answers

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());    
   }
);
like image 138
Vadim Gremyachev Avatar answered Oct 20 '22 14:10

Vadim Gremyachev