Slinger's Thoughts

June 4, 2009

Adding printers from SharePoint

Filed under: SharePoint — slingeronline @ 12:50 pm

Had an interesting case come up recently.  My manager wanted a way for our users to be able to easily add our network printers to their computers.  Apparently going to \\printerserver\printer01\ is too much of a challenge for our end users.  Initially you would think that it would be simple, and that you just create a list with a hyperlink column and add the UNC path to your printer for the link and away you go.  The problem with that is, when you click on the link, somehow a magical file:/// gets added so that your link now looks like file:///\\printerserver\printer01\ which is of course useless.  An associate of mine found an article online where the solution is to create a batch file that has the link.  This of course requires a user to choose to either “save” or “run” the file.  If they can’t figure out how to get there from the UNC path given to them, chances are they aren’t going to get the batch file right either.  There is hope however, and I have figured something out that so far works for us, and will hopefully work for you too.

This is actually several potential solutions combined into one that almost works flawlessly.  Each one had the right idea, the problem was getting them all hammered down into one solution.  First you are going to need a page that the users can access to add their printers.  Not too much of a big deal, any blank SharePoint page will work.  You could even park what we are about to add on an existing page if you so desired.  The first thing that you need to do is add the function that will enable this whole mess to work to begin with. Once you have a page created or in mind, go ahead and fire up your SharePoint designer, and open the page.

Near the top of the page (code view) you will notice a <script> tag.  Very small and unassuming.

<script>
   var navBarHelpOverrideKey = "wssmain";
</script>

We want to add another <script> tag immediately after this one.

<script type="text/vbscript">
function AddP(pName)
Set WshNetwork = CreateObject("Wscript.Network")
WshNetwork.AddWindowsPrinterConnection pName
end function
</script>

This little bit of scripting add the function to add printers using the Windows Scripting Host (from what I have read.)

The next thing I did was to create a table where I could describe the printers I wanted to give out users access to.  Nothing fancy, just a simple table. I won’t go into detail about how to create or format the table.

2009-06-04 13.18

You’ll notice that I also added some buttons to the table for our users to click.  The next part was kind of fun to figure out.  The button has an onClick action, but is also a link.  Don’t ask me why that is important, but somehow it is.  I would have thought that one instance or the other would suffice.

For simplicity’s sake, I will use the top printer as an example;

When you add a button to your page, it just needs to be a simple HTML Input button.  The code for the button will like like this:

<input name="Button1" type="button" value="Add">

Which is great, but it doesn’t actually do anything.  We need to make it a link and give it a function.

<a href="#" language="vbscript" onclick="AddP(‘\\cecprt\4e1’)"><input name="Button1" type="button" value="Add"></a>

You will notice that we make the button a link using some basic HTML, and added a VB function to the link.  The VB Function calls the script that we added earlier, and adds the printer with the UNC path \\cecprt\4e1\ .  Great! Problem is right now it won’t do a damn thing because of security settings in IE.  This is something that you may be able to get with your network/system administrator on.  If you add the page that hosts this table to the list of trusted sites in IE, then when you click on one of the “Add” buttons, you get a warning about a potentially unsafe ActiveX control running.

2009-06-04 13.32

I think this could be mitigated with the proper permissions as well.  Just to test however, if you click “Yes,” and wait about 20 seconds, the printer will be added to your computer.

There are other solutions out there, but they start running into problems when they try to do too much and ActiveX security limitations start popping up all over the place.  I just wanted to keep this as simple as possible.

Hope this helps someone out. 

The information for this article was gathered from the links below.
http://social.technet.microsoft.com/Forums/en-US/sharepointgeneral/thread/9e752df8-b60f-47c3-b862-08b9e9d9a03e
http://blogs.msdn.com/mab/archive/2009/02/13/install-a-remote-printer-via-a-hyperlink-in-html.aspx

Advertisements

7 Comments »

  1. Would love to get more details about this fantastic solution….

    Comment by Jensterd — May 8, 2012 @ 11:04 am

    • It’s been a long time since I did this, but what did you want to know? (I can’t go back and look at what I did here either, since I no longer work at this company unfortunately.)

      Comment by Jay Strickland — May 8, 2012 @ 11:08 am

      • Greetings from SPSDC :-)

        We’re moving all our printers to AD – I’m working on a SP2007 solution that (a) reads the current list of printers from AD; (b) shows a SP page with all of the printers so that users can click on a item (hyperlink? button?) and install the printer.

        I’ve got the AD part done (ADSIEdit query into a SQL table) – and am able to get to the data (using BDC); but having issues getting the last (and most important) part – having a place where users can click.

        I need to be able to pass the parameter from the printer_queue column into the HTML link – any suggestions?

        Comment by Jensterd — May 8, 2012 @ 12:14 pm

  2. The only thing that I can think of, would be to use one of the html controls. You would need to add these using SharePoint Designer. You might need to create a custom view in order to do this, using a DVWP so that you can access the column that you need, and then set that as the default view. I hope that helps, at least a little.

    Comment by Jay Strickland — May 8, 2012 @ 12:26 pm

  3. Great suggestion – thx – Ill let you know how it goes

    Comment by Jensterd — May 8, 2012 @ 1:14 pm

  4. Hello, I have scripted this out in Powershell to build a website based on your code. It works fine when I bring up it up on my Windows 7 box, but when I try to run it from a server running IIS, I get this error… any ideas? Message: ActiveX component can’t create object: ‘WScript.Shell’ Code, below…

    function AddP(pName)
    Set objWSHShell = CreateObject(“WScript.Shell”)
    Set WshNetwork = CreateObject(“Wscript.Network”)
    WshNetwork.AddWindowsPrinterConnection pName
    end function

    Chi3325X01 –

    Chi3401X01 – Chicago 3401 1st floor, in IT Network Lab; Xerox 4118; K7650

    Microsoft XPS Document Writer – Don’t add this

    Comment by Mike — June 28, 2012 @ 10:49 am

  5. It stripped all my HTML out :(, can I email it to you?

    Comment by Mike — June 28, 2012 @ 10:50 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: