IsReadOnly for a field is throwing exception
-
Thursday, April 12, 2012 4:49 AM
Hello, I have a table where a few fields by certain users cannot be added during the create screen. The first one I tried is a column called Status that has a choice list. I set a default value in article_created() method. As you can see in my last few lines of my code. I used the status_isReadOnly if the user has the CanEditAllPermissions. Yet when I go to my create screen, it throws an exception saying I cannot set value. I have other fields which i need to do the same with, I am not sure why the code below is not working. Would it be better to do a isvisible instead of isreadonly? What would be the best way to not let certain users not able to add data to certain fields in the create screen. Thanks
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.LightSwitch; namespace LightSwitchApplication { public partial class Article { partial void Article_Created() { //On Create screen when Open, show todays date in Entry Date field and Status = New this.EntryDate = System.DateTime.Today; this.Status = "New"; } partial void Status_Changed() { //When Published Status is selected, the date published is set to Today if (this.Status.Equals("Published")) { this.DataPublished = System.DateTime.Today; } } partial void LinkAgents_Changed() { if (this.LinkAgents != null && this.Status == "New") { this.Status = "Assigned"; } } partial void Status_IsReadOnly(ref bool result) { result = this.Application.User.HasPermission(Permissions.CanEditAll); } } }
- Edited by Dbdmora Thursday, April 12, 2012 5:15 AM
All Replies
-
Thursday, April 12, 2012 5:19 AM
Hi Please try this
result = !this.Application.User.HasPermission(Permissions.CanEditAll);
Slight change of your code.
May this help you...
Rashmi Ranjan Panigrahi
- Proposed As Answer by Paul Van Bladel Thursday, April 12, 2012 5:41 AM
-
Thursday, April 12, 2012 5:32 AM
Rashmi, ok i did not get the exception if my user has the permission above. However, the user that does not have the above permission, is when it throws the error of not being able to set the value, which is "new" based on my code above. How can I fix it, since Status field has a default value in code. Thanks for the help.
-
Thursday, April 12, 2012 6:01 AMModerator
Change the Status_IsReadOnly method to return false in the case where a value has not been set yet. This way, the Article_Created method can set the value of Status and it will become immediately read-only afterwards:
partial void Status_IsReadOnly(ref bool result) { // The property is read-only when the property has been set and the user does not have the edit permission result = !String.IsNullOrEmpty(this.Status) && !this.Application.User.HasPermission(Permissions.CanEditAll); }Justin Anderson, LightSwitch Development Team
- Marked As Answer by Dbdmora Friday, April 13, 2012 9:28 AM
-
Thursday, April 12, 2012 6:03 AM
Hi
As far i understand to your question, if you want to set dafault value in Status property when the property is read-only try the below code in your Article_Inserted method
partial void Article_Inserted(Article entity) { using (this.Application.User.AddPermissions(Permissions.CanEditAll) { entity.Status = "New"; } }
May this will help you...Rashmi Ranjan Panigrahi
-
Thursday, April 12, 2012 6:26 AMModeratorThe problem is client-side. In the scenario that the user does not have the permission, the this.Status = "New" line in the Article_Created method will throw an exception because the property is read-only (due to the user not having permission to change it). At this point in the scenario, server-side code (e.g. Article_Inserted) will not help.
Justin Anderson, LightSwitch Development Team
-
Thursday, April 12, 2012 6:45 AM
Hi Justin
Thanks for your suggestion.
Rashmi Ranjan Panigrahi
-
Friday, April 13, 2012 9:28 AM
Justin, thanks for your help, as well as Rashmi. I do have a question, what does the "!" in front of the query do, just make it True?
Thanks
-
Friday, April 13, 2012 7:02 PMModeratorThe ! is the Boolean NOT operator; it turns true to false and false to true.
Justin Anderson, LightSwitch Development Team

