locked
Printer list for lightswitch RRS feed

  • Question

  • Hi, I want to get printer listing in "server" side with lightswitch.

    I am able to get listing of printers using general c# app but not in lightswitch.

    I thought that lightswitch server side is able to use same code as I would use in standalone c# app.

    anyone has any ideas?

    Thanks.

    Thursday, July 19, 2012 9:12 AM

Answers

  • Here is what I did...

    In the server project, create a class called Printing...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Drawing;
    using System.Drawing.Printing;
    
    namespace LightSwitchApplication
    {
        public class Printing
        {
    
            public IEnumerable<string> GetServerPrinters()
            {
                List<string> listOfPrinters = new List<string>();
    
                foreach (string printer in PrinterSettings.InstalledPrinters)
                {
                    listOfPrinters.Add(printer);
                }
    
                return listOfPrinters;
            }
    
        }
    }


    Then back in the Logical view of the solution, create an entity (table) called Printer.

    I have a Home screen that is the default screen to open whenever my LightSwitch application runs. On this Home screen I have a query that auto executes. In my case, the query is called Companies...  

    What I then do is select to add code to the Query_Executing code in the ApplicationDateService class of the Server project...

    partial void Query_Executing(QueryExecutingDescriptor queryDescriptor)
            {
                if (queryDescriptor.Name == "Companies")
                    LoadPrinters();
            }

    This code runs each time the Companies query is executed, which calls a custom method I created called LoadPrinters (also in the ApplicationDataService class)....

      public void LoadPrinters()
            {
                // filter = e => e.IntegerProperty == 0;
                Printing printing = new Printing();
                IEnumerable<string> listOfPrinters = printing.GetServerPrinters();
    
                IEnumerable<Printer> printers = DataWorkspace.ApplicationData.Printers.GetQuery().Execute();
                foreach (Printer p in printers)
                    p.Delete();
                
                foreach (string printerName in listOfPrinters)
                {
                     Printer newprinter = DataWorkspace.ApplicationData.Printers.AddNew();
                    newprinter.PrinterName = printerName;
                }
            
                DataWorkspace.ApplicationData.SaveChanges();
            }

    What will happen is; each time a query is executed, if the query is the name of the query on my screen (which is Companies), the LoadPrinters() method fires. This LoadPrinters method refreshes the Printer table with a list of printers configured on the server. I can then use that Printer table in dropdown or list, or whatever, on the client.

    Hope this helps :)

    Paul
    www.PaulSPatterson.com


    PP

    Thursday, July 19, 2012 6:33 PM

All replies

  • "I thought that lightswitch server side is able to use same code as I would use in standalone c# app"

    Yes, that should be the case. What indicates to you that it's not working that way for you? Do you get an error message? Or it won't compile?

    You have to give us "something" to work with. :-)


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Thursday, July 19, 2012 11:27 AM
    Moderator
  • Hi Yann, yes it should but

    I am not able to use same definitions as in normal c# app.

    Below example gives me list of available printers.

    example:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Printing;

    namespace Printer
    {
     public partial class Form1 : Form
     {
      public Form1()
      {
       InitializeComponent();
      }
      private void Form1_Load(object sender, EventArgs e)
      {
       try
       {
        if (PrinterSettings.InstalledPrinters.Count > 0)
        {
         foreach (String printer in PrinterSettings.InstalledPrinters)
         {
          printersList.Items.Add(printer.ToString());
         }
        }
       }
       catch (Exception ee) {
       
       }
      }
     }
    }

    Thursday, July 19, 2012 1:15 PM
  • So as Yann asked above... what is the actual problem?  It won't compile?  It gives a compile-time error?  A run-time error?  The list of printers it returns isn't the one you were expecting?  We need something to go on.

    Generic advice:

    Did you navigate to the Server project in the Solution Explorer and "Add Reference" to the necessary System.Drawing assembly?  Did you add  the appropriate using clause (System.Drawing.Printing)?


    My Blog: dotnetlore.com Twitter: @DotNetLore

    Thursday, July 19, 2012 4:26 PM
  • problem is that I cannot use that code in lightswitch the declarations of "using" are different in lightswitch than normally.

    example; using System.Drawing.Printing;

    doesnt exist.

    Thursday, July 19, 2012 4:28 PM
  • Here is what I did...

    In the server project, create a class called Printing...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Drawing;
    using System.Drawing.Printing;
    
    namespace LightSwitchApplication
    {
        public class Printing
        {
    
            public IEnumerable<string> GetServerPrinters()
            {
                List<string> listOfPrinters = new List<string>();
    
                foreach (string printer in PrinterSettings.InstalledPrinters)
                {
                    listOfPrinters.Add(printer);
                }
    
                return listOfPrinters;
            }
    
        }
    }


    Then back in the Logical view of the solution, create an entity (table) called Printer.

    I have a Home screen that is the default screen to open whenever my LightSwitch application runs. On this Home screen I have a query that auto executes. In my case, the query is called Companies...  

    What I then do is select to add code to the Query_Executing code in the ApplicationDateService class of the Server project...

    partial void Query_Executing(QueryExecutingDescriptor queryDescriptor)
            {
                if (queryDescriptor.Name == "Companies")
                    LoadPrinters();
            }

    This code runs each time the Companies query is executed, which calls a custom method I created called LoadPrinters (also in the ApplicationDataService class)....

      public void LoadPrinters()
            {
                // filter = e => e.IntegerProperty == 0;
                Printing printing = new Printing();
                IEnumerable<string> listOfPrinters = printing.GetServerPrinters();
    
                IEnumerable<Printer> printers = DataWorkspace.ApplicationData.Printers.GetQuery().Execute();
                foreach (Printer p in printers)
                    p.Delete();
                
                foreach (string printerName in listOfPrinters)
                {
                     Printer newprinter = DataWorkspace.ApplicationData.Printers.AddNew();
                    newprinter.PrinterName = printerName;
                }
            
                DataWorkspace.ApplicationData.SaveChanges();
            }

    What will happen is; each time a query is executed, if the query is the name of the query on my screen (which is Companies), the LoadPrinters() method fires. This LoadPrinters method refreshes the Printer table with a list of printers configured on the server. I can then use that Printer table in dropdown or list, or whatever, on the client.

    Hope this helps :)

    Paul
    www.PaulSPatterson.com


    PP

    Thursday, July 19, 2012 6:33 PM
  • Just to be clear, I'll ask the same question I did above:

    Did you switch to "File View" in the Solution Explorer, right-click on the Server project, choose "Add Reference" and add a reference to the System.Drawing assembly that you're trying to use?

    Also, see Paul's detailed post for a more thorough answer.


    My Blog: dotnetlore.com Twitter: @DotNetLore

    Thursday, July 19, 2012 9:06 PM
  • "the declarations of "using" are different in lightswitch than normally"

    This is ONLY true if your code is in either the Client project, or the Common project. This is because these two projects are Silverlight projects, which doesn't have the full range of .NET assemblies avalilable to them, only Silverlight assemblies.

    If your code is in the Server project, the available assemblies that you can reference will be exactly the same as for a normal C# project.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Friday, July 20, 2012 1:56 AM
    Moderator
  • I did but I didnt add any references.
    Friday, July 20, 2012 9:18 AM
  • Thats correct, however I wonder if its possible to get the printer listing on client side too?
    Friday, July 20, 2012 9:19 AM
  • Awesome, thank you Paul for the detailed example :)
    Friday, July 20, 2012 9:20 AM
  • This si great. I wonder if a LightSwitch Data source can be made using this code? That way a person could just install it and use it?

    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    Tuesday, July 24, 2012 4:31 PM