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

1 Answer 1

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());
  });
1
  • 1
    For anyone who needs help with the initial part of creating the wiki page using the JavaScript object model, @VadimGremyachev posted an answer that I used as well. sharepoint.stackexchange.com/questions/78669/… Commented Feb 27, 2015 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.