Asked by:
sever side validation in CRM 2013

Question
-
hi,
I want to implement server side validation in my form.
Please provide me any link or document or code to implement the same.
Thanks
Wednesday, April 2, 2014 6:01 PM
All replies
-
You can use plugins on the server to provide additional validation when a record is created, updated, etc...
Jason Lattimer
My Blog - Follow me on Twitter - LinkedIn- Proposed as answer by JLattimerMVP, Moderator Wednesday, April 2, 2014 8:27 PM
Wednesday, April 2, 2014 8:27 PMModerator -
hi,
I have created Plugins for server side validation my code is as follow:
IPluginExecutionContext context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext; IOrganizationServiceFactory factory = serviceProvider.GetService(typeof(IOrganizationServiceFactory)) as IOrganizationServiceFactory; ITracingService tracer = serviceProvider.GetService(typeof(ITracingService)) as ITracingService; tracer.Trace("Enter plugin execute method"); Entity targetEntity = null; if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { targetEntity = context.InputParameters["Target"] as Entity; if (targetEntity.LogicalName != "new_servicerequest") return; } else return; try { IOrganizationService service = factory.CreateOrganizationService(context.UserId); if (targetEntity.Attributes.Contains("new_username")) { tracer.Trace("Enter attr contain method"); string new_username = targetEntity["new_username"].ToString(); if (string.IsNullOrEmpty(new_username)) throw new InvalidPluginExecutionException("username is invalid - " + new_username); } } catch (FaultException<OrganizationServiceFault> ex) { tracer.Trace("OrganizationServiceFault RaisedServiceRequestValidation: {0}", ex.ToString()); throw new InvalidPluginExecutionException("An error occurred in the ServiceRequestValidation plug-in.", ex); } catch (Exception ex) { tracer.Trace("ServiceRequestValidation: {0}", ex.ToString()); throw; } finally { tracer.Trace("Finally"); }
but i getting error and i am not able to trace it. Error is as below:
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred. Contact a system administrator or refer to the Microsoft Dynamics CRM SDK troubleshooting guide.Detail: <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts"> <ErrorCode>-2147220891</ErrorCode> <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic"> <KeyValuePairOfstringanyType> <d2p1:key>OperationStatus</d2p1:key> <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value> </KeyValuePairOfstringanyType> <KeyValuePairOfstringanyType> <d2p1:key>SubErrorCode</d2p1:key> <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">-2146233088</d2p1:value> </KeyValuePairOfstringanyType> </ErrorDetails> <Message>An error occurred. Contact a system administrator or refer to the Microsoft Dynamics CRM SDK troubleshooting guide.</Message> <Timestamp>2014-04-04T07:26:05.8726068Z</Timestamp> <InnerFault i:nil="true" /> <TraceText> [ActivityFeeds.Plugins: ActivityFeeds.Plugins.ActivityClose] [00558047-c0bb-e311-9121-00164134ad17: ActivityFeeds.Plugins.ActivityClose: Create of new_raisedservicerequest] </TraceText> </OrganizationServiceFault>
ThanksFriday, April 4, 2014 7:38 AM -
Hi,
your code looks ok, and I don't think the error is in your plugin, as stated here:
[ActivityFeeds.Plugins: ActivityFeeds.Plugins.ActivityClose] [00558047-c0bb-e311-9121-00164134ad17: ActivityFeeds.Plugins.ActivityClose: Create of new_raisedservicerequest]
the error belongs to the OOB ActivityFeeds plugin.
Is it an upgraded environment from CRM 2011? without your plugin there are no errors? which steps did you register your plugin? what are you trying to accomplish with your plugin?
My blog: www.crmanswers.net - Rockstar 365 Profile
Friday, April 4, 2014 8:55 AM -
Hi,
I am working in CRM 2013.
Yes if i remove my plugin it is working properly.
I used CRM SDK PluginRegistration.exe(6.0.0001.0061). right now i am trying to validate that username field should not be blank.
- Edited by yogendrakmishra Friday, April 4, 2014 9:41 AM
Friday, April 4, 2014 9:33 AM -
Friday, April 4, 2014 10:00 AM
-
Hi,
to just check if the username is not blank you can just set the field as required inside CRM UI. However I think that your logic will be expanded in the future so you can accomplish this with the plugin if you prefer.
Looking at your plugin registration tool, looks like you registered 3 additional steps under ActivityFeeds.Plugins.ActivityClose, try to remove that 3 additional steps.
My blog: www.crmanswers.net - Rockstar 365 Profile
Friday, April 4, 2014 10:38 AM -
Hi,
Checking blank is just for test purpose.
I unregister ActivityFeeds.Plugins.ActivityClose now its giving me below error.
and if i remove step then plugin not apply in CRM form(no validaton popup if field is blank).
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred. Contact a system administrator or refer to the Microsoft Dynamics CRM SDK troubleshooting guide.Detail: <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts"> <ErrorCode>-2147220891</ErrorCode> <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic"> <KeyValuePairOfstringanyType> <d2p1:key>OperationStatus</d2p1:key> <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value> </KeyValuePairOfstringanyType> <KeyValuePairOfstringanyType> <d2p1:key>SubErrorCode</d2p1:key> <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">-2146233088</d2p1:value> </KeyValuePairOfstringanyType> </ErrorDetails> <Message>An error occurred. Contact a system administrator or refer to the Microsoft Dynamics CRM SDK troubleshooting guide.</Message> <Timestamp>2014-04-04T10:54:59.9693193Z</Timestamp> <InnerFault i:nil="true" /> <TraceText> [ActivityFeeds.Plugins: ActivityFeeds.Plugins.ActivityCreate] [bb52f170-e7bb-e311-9121-00164134ad17: ActivityFeeds.Plugins.ActivityCreate: Create of new_raisedservicerequest] </TraceText> </OrganizationServiceFault>
Friday, April 4, 2014 11:06 AM -
check if you registered unnecessary steps also for the ActivityFeeds.Plugins.ActivityCreate plugin
My blog: www.crmanswers.net - Rockstar 365 Profile
Friday, April 4, 2014 11:08 AM