Wednesday, October 13, 2010

Limiting Data by User In Microsoft LightSwitch

I wanted to be able to limit my queries by user. In order to do this you need to add additional query code. This can only be done, if you create the query under the datasource. So for example, say I have a table, called
appointments, and the table also has a relationship to users. Right Click on Appointments, and Add A Query. If you try to do this under the screen, you will not be able to add the code.

Once you have the query, and rename it, you can click the Edit Additional Query Code button, on Properties.

The gorgous part of this, you can have a complex query, in my case there are three groups or'd togeather, and then on top of that we limit by user.

And it only takes a couple of lines.

 partial void MyTodaysAppointments_PreprocessQuery(ref IQueryable<Appointment> query)
  
     {
  
       query = from Appointments in query
  
           where Appointments.User.Name == Application.Current.User.Name
  
           select Appointments;
  
     }  

0 comments: