I want to add pages on pages list, when btnAddPage
is clicked opens a dialog with two inputs
, addPageTitle
and addPageTags
but I don't know how to create those pages. I've already created Items to a custom list I've made and I've tried using the same function but it didn't work. Does anyone know how to add those two fields on a new item on pages list?
Update:
I've made some research and manage to find this code:
function createFile(resultpanel) { var clientContext; var oWebsite; var oList; var fileCreateInfo; var fileContent; clientContext = new SP.ClientContext.get_current(); oWebsite = clientContext.get_web(); oList = oWebsite.get_lists().getByTitle("Pages"); fileCreateInfo = new SP.FileCreationInformation(); fileCreateInfo.set_url("title.aspx"); fileCreateInfo.set_content(new SP.Base64EncodedByteArray()); fileContent = "The content of my new file"; for (var i = 0; i < fileContent.length; i++) { fileCreateInfo.get_content().append(fileContent.charCodeAt(i)); } this.newFile = oList.get_rootFolder().get_files().add(fileCreateInfo); clientContext.load(this.newFile); clientContext.executeQueryAsync( Function.createDelegate(this, successHandler), Function.createDelegate(this, errorHandler) ); function successHandler() { resultpanel.innerHTML = "Go to the " + "<a href='../Pages'>document library</a> " + "to see your new file."; } function errorHandler() { resultpanel.innerHTML = "Request failed: " + arguments[1].get_message(); } }
That works perfectly but still can't add anything to a field Tags
.
Update 2:
The answer below indeed adds a page but a blank one, the whole document(site) is blank. So I went through Rest Api check what was wrong and found that was missing some attribute:
<d:PublishingPageLayout m:type="SP.FieldUrlValue"> <d:Description>Basic Page</d:Description> <d:Url>http://deliveryoffice.telecom.pt/_catalogs/masterpage/EnterpriseWiki.aspx</d:Url> </d:PublishingPageLayout>
Does anyone knows how to add this?