I would like to print everything in a newform.aspx/editform.aspx modal dialog. I was considering creating a Web Part or a bit of JavaScript that was nothing more than a print button that specified the current window but I wasn't sure if that would work...
3 Answers
You can create a Custom Action which will add a print button to your Newform.aspx and EditForm.aspx forms, and in the Command you can define a javascript to print current window. See sample below:
Example for NewForm.aspx
<CustomAction
Description="Print me"
Title="Show Me"
Id="{E538E8C7-65DA-454E-AD87-4A603B6CC569}"
Location="CommandUI.Ribbon.NewForm"
RegistrationId="100"
RegistrationType="List"
Sequence="0"
Rights="ViewListItems"
xmlns="http://schemas.microsoft.com/sharepoint/">
<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Define the (UI) button to be used for this custom action -->
<CommandUIDefinitions>
<CommandUIDefinition Location="CommandUI.Ribbon.ListForm.New.Page.Edit.Controls._children">
<Button
Id="Ribbon.ListForm.Display.Manage.PrintMe"
Sequence="0"
Command="PrintPage"
Image32by32="~site/_layouts/Images/BingSearch/bing32.png"
Image16by16="~site/_layouts/Images/BingSearch/bing16.png"
LabelText="Print me"
Description="Print me description"
TemplateAlias="o1"/>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<!-- Define the action expected on the Print button click -->
<CommandUIHandler Command="PrintPage" CommandAction="javascript: window.print()" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
-
I like this idea much better than my original! I was mostly worried that I wouldn't be able to print JUST the modal window (ignoring the parent window). Commented Jun 26, 2012 at 17:28
-
Parent window in this case would be EditForm.aspx or NewForm.aspx (Modal Dialog), so I don't see a problem here. Also this fulfils your requirement? Commented Jun 26, 2012 at 17:33
-
2For what it's worth, if you don't want to bother with Visual Studio, this also works via SharePoint Designer. Open the site, navigate to the details page of the appropriate list, highlight the custom actions area, use the New Custom Action button that is in the main ribbon at top left, then, copy the above CommandAction javascript and paste it into the 'Navigate to URL' field. Commented Jun 26, 2012 at 17:46
-
That can also be done with SP Designer but creating a custom action in Feature within a WSP is scalable solution especially if you have different Farms (Development, Test, Production). You WSP may contains all your resources including Features, site definitions, assemblies, Web Part packages, class resources etc etc, and also can be deploy to the entire farm in one go, with Features can be activated to sites as per Users likings. Commented Jun 26, 2012 at 18:15
-
I attempted to try the solution using SP Designer as you instructed by placing CommandAction="javascript: window.print()" in the Navigate to URL field but i get an error Could not save List change to server. 'javascript' is an unexpected token, Expecting white space Line 1, position 545? ideas?– user13177Commented Nov 24, 2012 at 0:10
Better CommandAction approach here, because it gets all dialog content w/o the underlying page.
Weakness, doesn't honor InfoPath Page breaks.
CommandAction="javascript:
var patt = /.+_FormControl.+__ViewContainer/gi;
var alldivs = document.getElementsByTagName('div');
var printpageHTML = '';
for(var i=0; i<alldivs.length; i++){
if(patt.test(alldivs[i].id)){
printpageHTML = '<HTML><HEAD>\n' +
document.getElementsByTagName('HEAD')[0].innerHTML +
'</HEAD>\n<BODY>\n' +
alldivs[i].innerHTML.replace('inline-block','block') +
'\n</BODY></HTML>';
break;
}
}
var printWindow = window.open('','printWindow');
printWindow.document.open();
printWindow.document.write(printpageHTML);
printWindow.document.close();
printWindow.print();
printWindow.close();
SP.UI.Notify.addNotification('Exiting!');
">
Add this in the SPD 2010 navigate to URL: javascript:window.print() and then add your icon