locked
CRM 2013 Find whether any fields are read-only or not from Plugin code RRS feed

  • Question

  • Does any one came across this situation or is it possible to find whether any fields are read-only or not from Plugin code??

    I mean field is read-only from JavaScript or form level events. But from Plugin, is there a way to validate fields  in

    (Entity)context.InputParameters["UpdateContent"]  read-only or not??

    Wednesday, July 16, 2014 7:39 PM

Answers

  • No, You won't get this information in property bag.

    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:58 PM
    Thursday, July 17, 2014 7:08 AM
    Moderator
  • Hi,

    The fact that a field is read only or mandatory is managed in the application level. So, if you were to make a field mandatory, it'll prevent you from saving that record from form level, but you can easily save a record having no value on that field using C# code.

    This explains why you cannot get whether a field is read only or not from your Plugin.


    Admin QuikView Solution for CRM 2013



    Thursday, July 17, 2014 9:03 AM
  • Using metadata surely you can get Requirement level but I don't think you can get if field is readonly or not, because there could be Js code which can make field readonly.

    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:58 PM
    Thursday, July 17, 2014 9:25 AM
    Moderator
  • I don't see IsReadOnly property under Attribute metadata ?? am I missing something ??

    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:58 PM
    Thursday, July 17, 2014 10:46 AM
    Moderator
  • Hi Mahender,

    Sorry about that! :(

    I mixed read-only with requirement level. :O There's no way to fetch if a field is read-only or not using C#.


    Admin QuikView Solution for CRM 2013

    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:58 PM
    Thursday, July 17, 2014 11:16 AM
  • thanks Mahender and Dynamotion for your quick answers and explanation.

    In CRM 4.0, i remember i use to follow below code in Plugin to identify that:-

      var formXmlRequest = new RetrieveFormXmlRequest { EntityName = entityToLookUp.Name };
                    var formXmlResponse = (RetrieveFormXmlResponse)crmService.Execute(formXmlRequest);
                    var formXml = formXmlResponse.FormXml;               
    
                    var formXmlDocument = new XmlDocument();
                    formXmlDocument.LoadXml(formXml);
                    var disabledAttributes = formXmlDocument.SelectNodes("//control[@disabled='true']/@id");
    
                    foreach (XmlNode disabledAttribute in disabledAttributes)
                    {
    
                        if (entityToLookUp.Properties.Contains(disabledAttribute.Value))
                        {
                            throw new InvalidPluginExecutionException(disabledAttribute.Value + " field is ReadOnly. Please do not select Any ReadOnly fields while Merge.");
                        }
                    }

    Does this give any hint for CRM 2013?? i tried this approach, but it doesn't work. in CRM 2013 the DOM structure is way different. but who knows, there must be a way for it.


    • Edited by Nicksoft2011 Thursday, July 17, 2014 3:03 PM
    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:58 PM
    Thursday, July 17, 2014 3:03 PM
  • Hi,

    Did some digging around with this thing. The following code should work for you, just tested it. :)

    RetrieveRequest _formRequest= new RetrieveRequest()
    {
    	ColumnSet = new ColumnSet("formxml"),
    	Target = new EntityReference("systemform", new Guid("YOURFORMGUID")) 
    };
    
    Entity _systemForm = (Entity)((RetrieveResponse)orgService.Execute(_formRequest)).Entity;
    
    if (_systemForm.Attributes.ContainsKey("formxml"))
    {
    	XDocument _xDoc = XDocument.Parse(_systemForm.Attributes["formxml"].ToString());
    	string _disabled = _xDoc.Descendants("control")
    	                   .Single(c => c.Attribute("id").Value == "YOURFIELDNAME")
    					   .Attribute("disabled").Value;
    }

    Replace YOURFORMGUID with the Form Id from Customizations and YOURFIELDNAME with the fieldname whose enabled status you want to fetch.

    Admin QuikView Solution for CRM 2013


    Friday, July 18, 2014 7:04 AM
  • Dynamotion thanks for the update here. 

    I am running this code. I am trying to go through each control and verifying whether it is read only and it is included into UpdateContent or not. 

    but i am getting error on the linq query saying object reference not set to an instance of an object.

    Any idea??

     RetrieveRequest _formRequest = new RetrieveRequest()
                    {
                        ColumnSet = new ColumnSet("formxml"),
                        Target = new EntityReference("systemform", new Guid("xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx"))
                    };
    
                    Entity _systemForm = (Entity)((RetrieveResponse)service.Execute(_formRequest)).Entity;
    
                    if (_systemForm.Attributes.ContainsKey("formxml"))
                    {
                        foreach (KeyValuePair<String, Object> attr in entityToLookUp.Attributes)
                        {
                            XDocument _xDoc = XDocument.Parse(_systemForm.Attributes["formxml"].ToString());
                            string _disabled = _xDoc.Descendants("control")
                                               .Single(c => c.Attribute("id").Value == attr.Key.ToString())
                                                .Attribute("disabled").Value;
                            trace.Trace("disable attribute : " + _disabled);
                            if (entityToLookUp.Attributes.Contains(_disabled))
                            {
                                throw new InvalidPluginExecutionException(_disabled + " field is ReadOnly. Please do not select Any ReadOnly fields.");
                            }
                        }
                    }


    • Edited by Nicksoft2011 Friday, July 18, 2014 3:47 PM
    • Proposed as answer by Anupam Bishui Friday, July 18, 2014 4:09 PM
    • Unproposed as answer by Anupam Bishui Friday, July 18, 2014 4:09 PM
    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:59 PM
    Friday, July 18, 2014 3:45 PM
  • Hi,

    I code I provided will give you the disabled status of a particular field (YOURFIELDNAME) of a form. If you wish to iterate through each of the controls and check their disabled status, you'll need to parse the formxml that you get and check.

    The linq code that I gave directly goes for the field and checks its disabled status. You'll need to play with the _xDoc variable. Also, what exactly does entityToLookup contain? This "attr.Key.ToString()" is most probably causing the exception. 


    Admin QuikView Solution for CRM 2013


    Saturday, July 19, 2014 6:05 AM

All replies

  • No, You won't get this information in property bag.

    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:58 PM
    Thursday, July 17, 2014 7:08 AM
    Moderator
  • Hi,

    The fact that a field is read only or mandatory is managed in the application level. So, if you were to make a field mandatory, it'll prevent you from saving that record from form level, but you can easily save a record having no value on that field using C# code.

    This explains why you cannot get whether a field is read only or not from your Plugin.


    Admin QuikView Solution for CRM 2013



    Thursday, July 17, 2014 9:03 AM
  • Using metadata surely you can get Requirement level but I don't think you can get if field is readonly or not, because there could be Js code which can make field readonly.

    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:58 PM
    Thursday, July 17, 2014 9:25 AM
    Moderator
  • I don't see IsReadOnly property under Attribute metadata ?? am I missing something ??

    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:58 PM
    Thursday, July 17, 2014 10:46 AM
    Moderator
  • Hi Mahender,

    Sorry about that! :(

    I mixed read-only with requirement level. :O There's no way to fetch if a field is read-only or not using C#.


    Admin QuikView Solution for CRM 2013

    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:58 PM
    Thursday, July 17, 2014 11:16 AM
  • thanks Mahender and Dynamotion for your quick answers and explanation.

    In CRM 4.0, i remember i use to follow below code in Plugin to identify that:-

      var formXmlRequest = new RetrieveFormXmlRequest { EntityName = entityToLookUp.Name };
                    var formXmlResponse = (RetrieveFormXmlResponse)crmService.Execute(formXmlRequest);
                    var formXml = formXmlResponse.FormXml;               
    
                    var formXmlDocument = new XmlDocument();
                    formXmlDocument.LoadXml(formXml);
                    var disabledAttributes = formXmlDocument.SelectNodes("//control[@disabled='true']/@id");
    
                    foreach (XmlNode disabledAttribute in disabledAttributes)
                    {
    
                        if (entityToLookUp.Properties.Contains(disabledAttribute.Value))
                        {
                            throw new InvalidPluginExecutionException(disabledAttribute.Value + " field is ReadOnly. Please do not select Any ReadOnly fields while Merge.");
                        }
                    }

    Does this give any hint for CRM 2013?? i tried this approach, but it doesn't work. in CRM 2013 the DOM structure is way different. but who knows, there must be a way for it.


    • Edited by Nicksoft2011 Thursday, July 17, 2014 3:03 PM
    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:58 PM
    Thursday, July 17, 2014 3:03 PM
  • Yes, Formxml could be your point where you can dig into to search field property, actually I also tried to see formxml first if there is any property that we can use, but I think the only way will be to traverse DOM structure.

    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, July 17, 2014 3:55 PM
    Moderator
  • Hi,

    Did some digging around with this thing. The following code should work for you, just tested it. :)

    RetrieveRequest _formRequest= new RetrieveRequest()
    {
    	ColumnSet = new ColumnSet("formxml"),
    	Target = new EntityReference("systemform", new Guid("YOURFORMGUID")) 
    };
    
    Entity _systemForm = (Entity)((RetrieveResponse)orgService.Execute(_formRequest)).Entity;
    
    if (_systemForm.Attributes.ContainsKey("formxml"))
    {
    	XDocument _xDoc = XDocument.Parse(_systemForm.Attributes["formxml"].ToString());
    	string _disabled = _xDoc.Descendants("control")
    	                   .Single(c => c.Attribute("id").Value == "YOURFIELDNAME")
    					   .Attribute("disabled").Value;
    }

    Replace YOURFORMGUID with the Form Id from Customizations and YOURFIELDNAME with the fieldname whose enabled status you want to fetch.

    Admin QuikView Solution for CRM 2013


    Friday, July 18, 2014 7:04 AM
  • Dynamotion thanks for the update here. 

    I am running this code. I am trying to go through each control and verifying whether it is read only and it is included into UpdateContent or not. 

    but i am getting error on the linq query saying object reference not set to an instance of an object.

    Any idea??

     RetrieveRequest _formRequest = new RetrieveRequest()
                    {
                        ColumnSet = new ColumnSet("formxml"),
                        Target = new EntityReference("systemform", new Guid("xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx"))
                    };
    
                    Entity _systemForm = (Entity)((RetrieveResponse)service.Execute(_formRequest)).Entity;
    
                    if (_systemForm.Attributes.ContainsKey("formxml"))
                    {
                        foreach (KeyValuePair<String, Object> attr in entityToLookUp.Attributes)
                        {
                            XDocument _xDoc = XDocument.Parse(_systemForm.Attributes["formxml"].ToString());
                            string _disabled = _xDoc.Descendants("control")
                                               .Single(c => c.Attribute("id").Value == attr.Key.ToString())
                                                .Attribute("disabled").Value;
                            trace.Trace("disable attribute : " + _disabled);
                            if (entityToLookUp.Attributes.Contains(_disabled))
                            {
                                throw new InvalidPluginExecutionException(_disabled + " field is ReadOnly. Please do not select Any ReadOnly fields.");
                            }
                        }
                    }


    • Edited by Nicksoft2011 Friday, July 18, 2014 3:47 PM
    • Proposed as answer by Anupam Bishui Friday, July 18, 2014 4:09 PM
    • Unproposed as answer by Anupam Bishui Friday, July 18, 2014 4:09 PM
    • Marked as answer by Nicksoft2011 Wednesday, July 23, 2014 2:59 PM
    Friday, July 18, 2014 3:45 PM
  • Hi,

    I code I provided will give you the disabled status of a particular field (YOURFIELDNAME) of a form. If you wish to iterate through each of the controls and check their disabled status, you'll need to parse the formxml that you get and check.

    The linq code that I gave directly goes for the field and checks its disabled status. You'll need to play with the _xDoc variable. Also, what exactly does entityToLookup contain? This "attr.Key.ToString()" is most probably causing the exception. 


    Admin QuikView Solution for CRM 2013


    Saturday, July 19, 2014 6:05 AM