1

How do I set the value of a field in a list using the JavaScript object model? I am trying to click a button on an existing page that automatically creates a new wikipage (which I have done) and populate it with content like "Hello!"

I know I need to set the field 'publishingPageContent', but I'm having trouble setting it. Thanks so much

2

The following example demonstrates how to update enterprise wiki page content:

function updateWikiPage(pageUrl,pageContent,success,error)
{
  getFileWithProperties(pageUrl,
  function(file){
     var listItem = file.get_listItemAllFields(); 
     var itemProperties = {'PublishingPageContent': pageContent};
     updateListItem(listItem,itemProperties,success,error);
  },
  error);
}

where

function getFileWithProperties(url,success,error) {

   var ctx = SP.ClientContext.get_current(); 
   var file = ctx.get_web().getFileByServerRelativeUrl(url);   
   ctx.load(file,'ListItemAllFields'); 

   ctx.executeQueryAsync(
      function () {
         success(file);
      }, 
      error);
}


function updateListItem(listItem,properties,success,error) 
{
   var ctx = listItem.get_context();
   for(var propName in properties) {
       listItem.set_item(propName, properties[propName]) 
   }
   listItem.update();
   ctx.executeQueryAsync(
       function () {
         success();
       }, 
       error);
}

Usage

updateWikiPage('/kb/Pages/Home.aspx','<b>Welcome to SharePoint</b>',
  function(){
     console.log('Wiki page has been updated succesfully');
  }, 
  function(sender,args){
     console.log(args.get_message());
  });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.