Monday, December 20, 2010

Javascript:ShowModalDialog Relative Site Collection URL

Get the dynamic Site Collection URL working in your Javascript code:

{SiteUrl} - Replaces the full Site Collection URL within your Javascript:ShowModalDialog Relative URL path, use this piece of code to avoid hard coding URL's in your Custom Ribbon Action's Elements.xml file,

~site - This one does the same thing but can't be used in Context of the Javascript code, and can only be used within SharePoint.

Here is a sample Elements.xml Custom Ribbon Action code, which shows how to use the {SiteUrl}  and ~site property:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="OpenModalPopup"
RegistrationType="List"
RegistrationId="101"
Location="CommandUI.Ribbon">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition
Location="Ribbon.Documents.New.Controls._children">
<Button
Id="Ribbon.Documents.New.Controls.DemoHelloWorldButton"
Alt="Open Modal Popup Ribbon Button"
Sequence="10"
Image32by32="/_layouts/images/SharePointFix/water.gif"
Command="OpenModalWindow"
LabelText="Update Profile Info"
TemplateAlias="o2"/>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler Command="OpenModalWindow" CommandAction="javascript:SP.UI.ModalDialog.showModalDialog({~site/_layouts/SharePointFix/PrintListItems.aspx', title: 'Print List Items' });" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
<CustomAction
Id="ExternalLink"
Description="Change your user profile country"
Location="Microsoft.SharePoint.StandardMenu"
GroupId="PersonalActions"
Sequence="0"
Title="Select Country View"
ImageUrl="~site/_layouts/images/SharePointFix/water.gif"
>
<UrlAction Url="javascript:(function () { var o = { url:'{SiteUrl}/_layouts/SharePointFix/PrintListItems.aspx', title: 'Select List Item', dialogReturnValueCallback: SP.UI.ModalDialog.RefreshPage }; SP.UI.ModalDialog.showModalDialog(o); }) ();"/>
</CustomAction>
</Elements>

No comments:

Post a Comment