2

I'm trying to do something in Sharepoint 2010 that ought to be very simple, create a button that changes page (to a "create new item" form as it happens).

I set up a Content Editor Webpart and put a button in it (not in a form, because in Sharepoint the whole page is a form) with an "onclick" handler that changed the windows.location.href.

In 2010 the CEWP fights you a bit when you try to enter non-trivial HTML, it keeps escaping characters like "&" which can be a real pain. However in the end I got the right content entered.

It didn't work (the page just refreshed itself without changing URL). By checking on StackOverflow I found some recommendations for a more robust form for the CEWP content, which ended up as-

<script type="text/javascript">
    function submit_rec(){
       window.location.href = "<my server root URL>/Lists/Rec/NewForm.aspx";
       return;
    }
    </script>
<button onclick="javascript:return submit_rec();return false"/>Submit a Recommendation</button>

Here's the strange part.

If I use Firebug and put a breakpoint in the submit_rec() function this works fine. But without a breakpoint, it goes back to the behaviour of always returning to the current page.

It seems there's a timing issue, or Sharepoint is taking control after my URL starts to load, and reloads the original page again!

Anyone seen this before and found a solution?

Ideas and suggestions woudl be much appreciated.

Regards: colin_e

colin_e
  • 21
  • 2
  • Try to change to http://jsfiddle.net/f0t0n/dmqdR/ without that `javascript:`. – Eugene Naydenov Nov 09 '12 at 19:47
  • This is getting stranger. I have now copied the code behind the Sharepoint standard Document Centre exactly, and changed the URLs and labels text only- `` The DocCentre button works, but mine just refreshes the current page. It's odd and rather frustrating. – colin_e Nov 09 '12 at 23:06
  • I've put an alert on the button and the script *is* being called, but somehow on my pages the effect of the attempt to change the page URL is being negated, presumably by something in the Sharepoint Javascript infrastructure (which is large and opaque). – colin_e Nov 10 '12 at 00:04
  • Try to alert `window` and `window.location` to check does that objects exist. – Eugene Naydenov Nov 10 '12 at 00:10

2 Answers2

0

Thanks to everyone who responded. With some more experimentation, and following hints from other threads on Stackoverflow, I was finally able to get this working.

My mistake with my last effort, using the Sharepoint builtin OpenNewFormUrl() function, was to expect this this would be a global function defined in a central library by SP. Turns out it's not, it has to be defined separately on every page where it's used, partly because it hard-codes the size of the popup frame for the library edit form.

(Yes this is ugly, like a lot of Sharepoint under the covers, anyway, I digress...)

I was able to get a Sharepoint 2010 style "popup" editor working with a button of the same style as the standard SP Document Centre "Submit a Document" button using the following code in a Content Editor WebPart. I have no idea what the script does in detail, I just copied it from the Document Centre site template home page-

<script type="text/javascript">

// <![CDATA[
function ULS18u(){var o=new Object;o.ULSTeamName="DLC Server";o.ULSFileName="default.aspx";return o;}
var navBarHelpOverrideKey = "wssmain";
// ]]>

function OpenNewFormUrl(url)
{ULS18u:;
var options = {width:640, height:720};
SP.UI.ModalDialog.commonModalDialogOpen(url, options, null, null);
}

</script>

<div class="ms-uploadbtnlink">
 <button onclick="javascript:OpenNewFormUrl(&#39;/dev/KfD/KfDdev/Lists/Recommendation/NewForm.aspx&#39;);return false;" type="submit"><nobr><img alt="Submit a Recommendation" src="/_layouts/Images/uploaddoc.png"/>&#160;<span>Submit a Recommendation</span></nobr>
 </button>
</div>

I'm wary of what setup this will do if (say) the users screen is smaller than the hard-coded popup size, and i'm still confused as to why my earlier (and much simpler) efforts failed, but at least I have a working soluion.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
colin_e
  • 21
  • 2
0

Try this:

javascript:SP.UI.ModalDialog.OpenPopUpPage('/dev/KfD/KfDdev/Lists/Recommendation/New‌​Form.aspx');return false;

in the onclick event

andr
  • 15,970
  • 10
  • 45
  • 59
Kristian
  • 41
  • 3