Can anyone use the Javascript CSOM SP.Publishing.PublishingWeb in non-app code?

I have been trying to write some javascript which can be used to create publishing pages in the current site (this is called in an application page opened via a custom action on the Pages library).
I am trying to use code similar to the example on the
SP Typescript demos page.  The only real difference between their and my implementation which I can see is that they are running in an app (hence checking SPHostUrl is is non-existent for me as I am on premise and non-app).
My code is as follows:
SP.SOD.executeFunc('mQuery.js', 'm$', function () {
m$.ready(function () {
m$('#CreatePage').click(function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
SP.SOD.executeOrDelayUntilScriptLoaded(function () {
SP.SOD.executeFunc('sp.publishing.js', 'SP.Publishing', function () {
createPage();
}, 'SP.Runtime.js');
function createPage() {
var context = SP.ClientContext.get_current();
var web = context.get_web();
context.load(web);
context.executeQueryAsync(function () {
var pubWeb = SP.Publishing.PublishingWeb.getPublishingWeb(context, web);
context.load(pubWeb);
context.executeQueryAsync(function () {
var pageInfo = new SP.Publishing.PublishingPageInformation();
var newPage = pubWeb.addPublishingPage(pageInfo);
context.load(newPage);
context.executeQueryAsync(function () {
var listItem = newPage.get_listItem();
context.load(listItem);
context.executeQueryAsync(function () {
var link = document.getElementById("linkToPage");
link.setAttribute("href", web.get_url() + "/Pages/" + listItem.get_fieldValues().FileLeafRef);
link.innerText = "Go to new page!";
}, function (sender, args) {
alert('Failed to get new page: ' + args.get_message());
}, function (sender, args) {
alert('Failed to Add Page: ' + args.get_message());
}, function (sender, args) {
alert('Failed to get the PublishingWeb: ' + args.get_message());
}, function (sender, args) {
alert('Failed to get the Web: ' + args.get_message());
However I cannot for the life of me figure out how to get the PublishingWeb object.  Everytime my code hits the "context.load(pubWeb)" I get a javascript error thrown in sp.runtime.js:
"Object doesn't support property or method 'get_$y_0'"
I've tried a number of alternatives such as hard coding the url for the context, but nothing seems to work.  Is this just me?  Or just non-app code?

Hi,
According to your post, my understanding is that you want to create publishing pages using ECMA Script.
The following code(a aspx page) for your reference:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ Page Language="C#" %>
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml" xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<head runat="server">
<meta name="WebPartPageExpansion" content="full" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create Publishing Pages</title>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/sites/DennisSite/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/sites/DennisSite/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/sites/DennisSite/_layouts/15/sp.publishing.js"></script>
<script type="text/javascript">
var context;
var web;
var pubWeb;
var pageInfo;
var newPage;
var listItem;
context = SP.ClientContext.get_current();
function createPage() {
web = context.get_web();
pubWeb = SP.Publishing.PublishingWeb.getPublishingWeb(context, web);
context.load(web);
context.load(pubWeb);
context.executeQueryAsync(
function () {
pageInfo = new SP.Publishing.PublishingPageInformation();
newPage = pubWeb.addPublishingPage(pageInfo);
context.load(newPage);
context.executeQueryAsync(
function () {
listItem = newPage.get_listItem();
context.load(listItem);
context.executeQueryAsync(
function () {
var link = document.getElementById("linkToPage");
link.setAttribute("href", web.get_url() + "/Pages/" + listItem.get_fieldValues().FileLeafRef);
link.innerText = "Go to new page!";
function (sender, args) {
alert('Failed to get new page: ' + args.get_message());
function (sender, args) {
alert('Failed to Add Page: ' + args.get_message());
function (sender, args) {
alert('Failed to get the PublishingWeb: ' + args.get_message());
</script>
</head>
<body>
<form id="form1" runat="server">
</form>
<button onclick="createPage()">Create Publishing Page</button>
<a id="linkToPage"></a>
</body>
</html>
The Result:
Best Regards
Dennis Guo
TechNet Community Support

Similar Messages

  • Hi, Can anyone encounter the problem that you couldn't update your apps from Mac book pro??

    Hi, Can anyone encounter the problem that you couldn't update your apps from Mac book pro??
    Currently, my mac can't update any of the software from App store as well as bulit in software like garageband.

    yeah...
    i did that but it didn't work.
    Every times, i click on the software updates, software updates dialog box is popping up and
    checking for new updates.
    After that, acknowledged me whether i want to update or not.
    once, i click on "install".
    it appears following message.
    "A network error has occurred. Check your Internet connection, and then try again."
    i totally can access to Internet as i can browser all the websites.
    Please help me, is it a "virus" or "malware" causing my mac?.
    i just changed a new mac recently and having this problem

  • Can anyone use the ZipURLConnection object?

    I have some code in an EJB which, in orer to initialize, looks for compiled classes
    in a deployed JAR file. Specifically, it looks for all classes for a package,
    as returned by the getResources(name) method on ClassLoader. This returns a Collection
    of URL objects which contain classes.
    The resource returned has a protocol of "zip". When I call openConnection on
    the URL, I get an instance of weblogic.util.zip.ZipURLConnection, althought I
    store it as a URLConnection reference (I don't want any vendor specific definitions
    in my code).
    Once I have the connection, I get an InputStream (again, an instance of weblogic.util.zip.SafeZipFileInputStream
    stored in an InputStream variable). I was under the assumption that I could pass
    that inputStream into a java.util.zip.ZipUnputStream, in order to get at the names
    of the entries. But whenever I try to do anything with the returned InputStream
    (the SafeZipFileInputStream), I get a nullPointerException. It seems that no
    matter how I try to use the stream, the exception is thrown.
    Has anyone been able to use this without resorting to WebLogic classes? Other
    vendors provide the default JAR protocol, which makes it much easier. Does WebLogic
    have to force me to use their classes to access this information?
    The classes I'm attempting to access aren't EJB classes. Just supporting classes
    which I need to access.
    Thanks for any help,
    Ed

    While I haven't been able to use it, I've been able to manipulate the URL value
    so that I can create new one which will work with JARs (which is what the file
    is anyways).
    I take the URL.toString() value, and exchange the "zip" at the start with "jar".
    Works for me.
    "Ed Hillmann" <[email protected]> wrote:
    >
    I have some code in an EJB which, in orer to initialize, looks for compiled
    classes
    in a deployed JAR file. Specifically, it looks for all classes for a
    package,
    as returned by the getResources(name) method on ClassLoader. This returns
    a Collection
    of URL objects which contain classes.
    The resource returned has a protocol of "zip". When I call openConnection
    on
    the URL, I get an instance of weblogic.util.zip.ZipURLConnection, althought
    I
    store it as a URLConnection reference (I don't want any vendor specific
    definitions
    in my code).
    Once I have the connection, I get an InputStream (again, an instance
    of weblogic.util.zip.SafeZipFileInputStream
    stored in an InputStream variable). I was under the assumption that
    I could pass
    that inputStream into a java.util.zip.ZipUnputStream, in order to get
    at the names
    of the entries. But whenever I try to do anything with the returned
    InputStream
    (the SafeZipFileInputStream), I get a nullPointerException. It seems
    that no
    matter how I try to use the stream, the exception is thrown.
    Has anyone been able to use this without resorting to WebLogic classes?
    Other
    vendors provide the default JAR protocol, which makes it much easier.
    Does WebLogic
    have to force me to use their classes to access this information?
    The classes I'm attempting to access aren't EJB classes. Just supporting
    classes
    which I need to access.
    Thanks for any help,
    Ed

  • Can I use the Profile Manager as an MDM while distributing app through app store?

    Is it possible to use the Profile Manager within the OSX Server to provision profiles through apps purchased in the app store? 
    Ex:  userX buys app in the app store, in downloading the app they get enrolled in the MDM / OSX Server I control.  They select profile settings within my app and submit to OSX Server (Profile Manager) at which point I send back OTA the provisioned profile for them to upload.
    Ex:  https://itunes.apple.com/us/app/parentkit/id600618138?mt=8
    Thanks for the feedback!

    Is this not possible?  Is it possible but not allowed? 

  • Can anyone provide the javascript code for a master form

    I like to know how I can set up a master form on acrobat standard?  My issue is that I would like, as I fill the master form, to be able to have the rest of the forms fill in automatically.  Please help me with this issue!
    Thanks,
    Raul

    Take a look at http://www.jotform.com.  You can create as many forms as you like here for free and they don't contain advertising.
    You could also try Google docs, so go to http://www.google.com and log into your account or create one and then go to settings and the docs section.  It is free an you can create as many forms as you like and there isn't really any advertising to speak of.
    Other than this, it is easy to create your own form using html.
    If you are publishing to MobileMe, then you can do it, but you need to enter <Form action Mailto:, so all the info opens in the clients e-mail and is e-mailed directly to you.
    If you are using an external server such as GoDaddy, then you can use form mail and enable this on your server, so the result is e-mailed to you.

  • How can I use the same Apple ID account in two different App Stores?

    I am from Greece and I use my Apple-ID account to download iPhone Apps from the Greek App Store. However I live in Germany and some iPhone applications I want are available only through the German App Store. When I try to download one of them I got the message that my acount cannot be used with the German App Store. Is there any way to be able to have on my phone applications form both the German and the Greek App store at the same time using or not the same Apple ID account? I have got both Greek and German credit cards if this is an issue. Thanks.

    If I create a new iTunes account for the German itunes store, what happened with my previous purchases? Do they remain on my iPhone and my Mac's iTunes?
    Nothing will happen to your existing purchases. Your computer will remain authorized - you can authorize a single computer to up to five iTunes Store accounts - and the apps and content will remain in place.
    Can I switch from one acount to the other every time I want to update an application or purchase a new one?
    As long as you're in the relevant country, yes. Again, the terms of use say that you agree not to even attempt to use an iTunes Store from outside of its country.
    Regards.

  • HT201328 after unlocked done can i use the phone with foregein sim card in non European country?

    What happens if I block my I phone can it work in non European country ?

    Yeh I had a contract but I paid all off ages ago with o2 network I owned the iPhone I just gave my phone to my brother after I went holiday to my country Turkey I just requested from o2 customer service and n gave them all required info they processed that it will take 72 hrs to done

  • Can I use the early 2009 iMac 24" with a non-thunderbolt 27" cinema display?

    Is this possible, and if so, presumably the displays - bottom levels of the glass will be at different heights?
    Thanks

    Yes, you should be able to do that.

  • OK since midday today firefox 3.6.24 apparently no longer executes javascript on ebay. Can't use the "add to watch list" buttons or the enlarge photo buttons. It says 'javascript at the bottom of the firefox window but nothing happens.

    just as I wrote above.. ebay loads and functions. I can bid and view auctions but trying to either use the "add to watch list" or "add to wish list" buttons or to switch between different posted photos of an item just gives me javascript:; at the bottom left of my screen but clicking the button has no effect.I'm still running 10.4.11 with the last java download.. is this an ebay change to a newer java version not supported by my os or is it a firefox error..oh and one more thing each page seems to have become 4 times longer..ie there is a huge empty space as I scroll down the page before I hit the bottom.Only appears to do this on ebay..haven't found any other sites showing similar errors and everything works fine on my imac running firefox 4 under 10.5

    Craig Stenton: at 12:13:04
    but I can't install windows! During the install I am asked for the product key and it tells me that it is not valid and so I can't proceed.
    Craig Stenton: at 12:14:16
    I can't use the usual methods of activation because I cannot get an activation code. I can't use the activation wizard. I can't call the number because it requires an activation code.
    Hi,
    I'm a bit confused with this paragraph, as you said Vista already install on your computer, why did you say Windows can't be installed?
    According to your description with your problem, it is Product edition and key are not match. Firstly of all, we must make sure the system you installed on your computer was
    Windows Vista Business, not matter 32 or 64 bit.
    Note: you can check current system edtion at System Properties.
    After that, you can try to input your product key and activate your system for test.
    If problem still presists, it would be better to provide the activation error message.
    Roger Lu
    TechNet Community Support

  • I stream Internet radio from my iPad air to Bluetooth speaker.  Can anyone suggest the best EQ app to use to enhance the sound.  Thanks

    I stream Internet raidiot from iPad air to Bluetooth speakers.  Can anyone suggest the best EQ app to use to enhance the sound.
    THanks

    Yeah, I know I can restore it, but I might as well go ahead and try. I just finished a backup for icloud and itunes to make sure, I'll go ahead and restore it and set it up as new and see how that works. Seems like I have no other options at this point, I'd still like to know what they did with the Download tab in itunes store, if they removed it or if I'm just overlooking it.

  • My iPad is stuck with a small blue screen saying iCloud Backup, this iPad hasn't been backed up in 4 weeks.Backups blah blah- Click OK. I can't get it off the screen and I can't use the iPad because of it. Can anyone out there help?

    my iPad is stuck with a small blue screen saying iCloud Backup, this iPad hasn't been backed up in 4 weeks.Backups blah blah… Click OK. I can't get it off the screen and I can't use the iPad because of it. Can anyone out there help?

    Have you tried restarting or resetting your iPad?
    Restart: Press On/Off button until the Slide to Power Off slider appears, select Slide to Power Off and, after the iPad shuts down, then press the On/Off button until the Apple logo appears.
    Reset: Press the Home and On/Off buttons at the same time and hold them until the Apple logo appears (about 10 seconds).

  • I am putting together a proposal for a Final Cut Pro Workstation to use with a tapeless HD camera.  Can anyone suggest the best camera (in the 5-10,000 dollar range) ?  What hardware and software should I include in the package?

    I am putting together a proposal for a Final Cut Pro Workstation to use with a tapeless HD camera.  Can anyone suggest the best camera (in the 5-10,000 dollar range) ?  What hardware and software should I include in the package?

    This is quite an open suggestion, but here are some ideas which might get you started.  There are good HD cameras in your price range from Sony, Panasonic and Canon (and maybe other manufacturers). Tapeless formats vary.  I have only worked with Sony XDCAM which seems pretty good to me.  File sizes for material shot at 35MBS are not too huge and, with the free Sony transfer and conversion programme, are easy to bring into the computer via USB direct from the camera or from a fairly inexpensive converter.  The Sony XDCAM EX3 can produce results well up to broadcast quality.
    As to hardware, you'll need a powerful MAC - iMac, Macbook Pro or MacPro with 4GB or more of RAM and a good video card.  Softwarewise, FCPX is quite a bit cheaper than FC Studio 3 and of course it's 64bit and can make better use of multi-core machines than FCP7.    It's also designed to handle HD formats with less hassle than FCP7 and is matched to the latest versions of Motion and Compressor.   But you'd probably be wise to search reviews of FCPX before you make your choice - it has some issues.
    A good HD monitor will be essential and, for colour correction etc, you may want to consider a second monitor or HD TV foe editing. ( A MacPro is generally the best machine to connect a second monitor to). 
    This should give you a good editing system, but there are many other things you could add (Photoshop, professional sound monitors, etc)
    Hope this helps a bit.
    Jonathan

  • I'm trying to delete multiple pix in iPhoto that i stupidly made duplicates of without knowing, I've tried going into the applications folder and using image capture but i think I've missed a step , can anyone send the correct info / steps pls thanks

    I'm trying to delete multiple pix in iPhoto that i stupidly made duplicates of without knowing, I've tried going into the applications folder and using image capture but i think I've missed a step , can anyone send the correct info / steps pls thanks

    again image capture is not involved with deleting photos from iPhoto in any way ever
    the paid version of iPhoto Library Manager is very good for finding duplicates as is Duplicate Annihilator
    And I have no idea who told you this - or what it means - but re-read both of my opening statements
    I was told I could delete multpiles thru image capture by transferring my iPhoto library etc
    LN

  • Can anyone explain the use of PhantomReference

    Can anyone explain the use of PhantomReference( if possible by an example..).

    Payment request is a noted document and as such does not pass any accounting entry. For effecting a payment to a
    Against the payment request you can effect the down payment. Suppose u have completly automated payment system and if you want to effect a down payment, you can do it only by payment request.
    Payment requests can be controlled through workflow for proper autorization.

  • I recently upgraded my Mac to Yosemite and I can't use the custom function (grayed out) in Image Trace Illustrator CS6 (physical version). Anyone else having this issue?

    I recently upgraded my Mac to Yosemite and I can't use the custom function (grayed out) in Image Trace Illustrator CS6 (physical version). Anyone else having this issue?

    Custom will show automatically as soon as you just edit some options. So just go ahead, you don't need to select "custom"

Maybe you are looking for

  • Slow mouseDragged callbacks in java 1.6.0_10, works fast in prior releases?

    I have this program that draws a selection rectangle by xor-ing a rectangle on the screen following the mouse cursor when clicked. In jre's before 1.6.0_10, this runs fast, but in 1.6.0_10 it runs like dog. I've used this code with JRE's from 1.5 to

  • Oracle 8.0.5 Installation problem

    Hi, I got an error message when I tried to install Oracle 8.0.5 on RedHat 7.2. The error occured when the program trying to install the Oracle RDBMS package. It said relinking failure, can't make RDBMS executables. Could anybody help me in this matte

  • Why do I have to keep saving my PDFs as enabled for commenting in Reader?

    As part of my review process, I create a PDF, enable it for commenting in Reader, send it to reviewers, receive comments back from them, reply to the review comments, and send it back to the reviewers so that they can see the replies and make further

  • FI-SL report

    While reporting SL use wants to display transaction currency. 1. SL currency in USD 2. Transaction currency is in EUR 3. Reproting currency is coming in USD User wants to disply report in EUR Suggestion appriciated

  • Asynchronous function calls in Java?

    I need to write a class whose functions i could call asynchronously. How do we do this? Is this part of the Java API or do i need something else? Thank you in advance.