Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How I can retrieve values from fields?

Posted on by Microsoft Employee

Hi! I need get values from fields. How I can do it? I'm try

Entity entity = new Entity("entity_name");
string ent = entity.Attributes["entity_name"].toString();
but not have result. Thanks!

_1104350437044B043C044F043D043D044B043904_.png

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How I can retrieve values from fields?

    It's all my code

    public void Execute(IServiceProvider serviceProvider)
            {
               ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parmameters.
                    Entity entity = (Entity) context.InputParameters["Target"];
    
                    if (entity.LogicalName == "new_sms")
                    {
    
    
                        try
                        {
    
                            string number = "";
                            string text = "";
                            string parameters = "";
    
                            if (entity.Attributes.Contains("new_number"))
                                number = entity.GetAttributeValue<string>("new_number");
                            if (entity.Attributes.Contains("new_text"))
                                text = entity.GetAttributeValue<string>("new_text");
                            if (entity.Attributes.Contains("new_sms_parameters_smsid"))
                                parameters = entity.GetAttributeValue<string>("new_sms_parameters_smsid");
    
    
    
    
                            string fetchXmlString =
                                @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    <entity name='new_sms_parameters'>
    <attribute name='new_name' />
    <attribute name='new_url' />
    <attribute name='new_login' />
    <attribute name='new_password' />
    <attribute name='new_alphaname' />
    </entity>
    </fetch>";
                            string name = "";
                            string url = "";
                            string login = "";
                            string password = "";
                            string alphaName = "";
    
                            EntityCollection ec = ExecuteFetch(fetchXmlString, service);
                            if (ec.Entities.Count > 0)
                            {
                                //string output = string.Empty;
                                foreach (var item in ec.Entities)
                                {
    
                                    if (item.Attributes.Contains("new_name"))
                                        name = item.Attributes["new_name"].ToString();
    
                                    if (name.Contains(parameters))
                                    {
                                        if (item.Attributes.Contains("new_url"))
                                            //Check for fullname value exists or not in Entity Collection
                                            url = item.Attributes["new_url"].ToString();
    
                                        if (item.Attributes.Contains("new_login"))
                                            //Check for fullname value exists or not in Entity Collection
                                            login = item.Attributes["new_login"].ToString();
    
                                        if (item.Attributes.Contains("new_password"))
                                            //Check for fullname value exists or not in Entity Collection
                                            password = item.Attributes["new_password"].ToString();
                                        if (item.Attributes.Contains("new_alphaname"))
                                            alphaName = item.Attributes["new_alphaname"].ToString();
                                    }
                                }
    
                            }
    
    
                            string single = "<message><service id='single' source=\'" + alphaName + "'/><to>" + number +
                                            "</to><body content-type='text/plain'>" + text + "</body></message>";
    
                            if (url.Contains("api.life.com.ua/ip2sms"))
                                send(url, single, login, password);
    
                            
    
                        }
                        catch
                        {
                           
                        }
                    }
                }
            }
            public static EntityCollection ExecuteFetch(string fetchXmlString, IOrganizationService service)
            {
                return service.RetrieveMultiple(new FetchExpression(fetchXmlString));
            }
            //Autorization
            public static void SetBasicAuthHeader(WebRequest request, String userName, String userPassword)
            {
                string authInfo = userName + ":" + userPassword;
                authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
                request.Headers["Authorization"] = "Basic " + authInfo;
            }
    
            //Send request
            private static string send(string Url, string Data, string login, string password)
            {
    
                WebRequest req = WebRequest.Create(Url);
                SetBasicAuthHeader(req, login, password);
                req.Method = "POST";
                req.Timeout = 100000;
                req.ContentType = "text/xml";
                byte[] sentData = Encoding.UTF8.GetBytes(Data);
                req.ContentLength = sentData.Length;
                Stream sendStream = req.GetRequestStream();
                sendStream.Write(sentData, 0, sentData.Length);
                sendStream.Close();
    
                WebResponse res = req.GetResponse();
                Stream ReceiveStream = res.GetResponseStream();
                StreamReader sr = new StreamReader(ReceiveStream, Encoding.UTF8);
                //Кодировка указывается в зависимости от кодировки ответа сервера
                Char[] read = new Char[256];
                int count = sr.Read(read, 0, 256);
                string Out = String.Empty;
                while (count > 0)
                {
                    String str = new String(read, 0, count);
                    Out += str;
                    count = sr.Read(read, 0, 256);
                }
    
    
                return Out;
    
    
    
            }
        }
    }


    Thanks!

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: How I can retrieve values from fields?

    Unless you won't tell some details, we won't be able to help you

    "Entity entity = new Entity("new_sms");

                   string number = "";

                   string text = "";

                   string parameters = "";

                   if (entity.Attributes.Contains("new_number"))

                       number = entity.GetAttributeValue<string>("new_number");

                   if (entity.Attributes.Contains("new_text"))

                       text = entity.GetAttributeValue<string>("new_text");

                   if (entity.Attributes.Contains("new_sms_parameters_smsid"))

                       parameters = entity.GetAttributeValue<string>("new_sms_parameters_smsid");

    "

    In order to get field value from the entity object first you need to get it from CRM using CRM service ?? Where are you writing this code ?? what are you trying to do ?? I don't see any code where you retrieve this information from CRM ??

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How I can retrieve values from fields?

    I try, but not result

    Entity entity = new Entity("new_sms");
                    string number = "";
                    string text = "";
                    string parameters = "";
    
                    if (entity.Attributes.Contains("new_number"))
                        number = entity.GetAttributeValue<string>("new_number");
                    if (entity.Attributes.Contains("new_text"))
                        text = entity.GetAttributeValue<string>("new_text");
                    if (entity.Attributes.Contains("new_sms_parameters_smsid"))
                        parameters = entity.GetAttributeValue<string>("new_sms_parameters_smsid");


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How I can retrieve values from fields?

    Try this

    In java script u can get the value of the field like this

    var a=Xrm.Page.getAttribute("schema name of the field").getValue();

    in c# try this

    entity.GetAttributeValue<String>("schemaname of the field");

    If u find the answer  as useful mark it as verified

    Thanks

    Muthuraman. E

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: How I can retrieve values from fields?

    Hi,

    Could you provide some more details what you are doing, how and where you are tying to read field data ??

    to get value from entities field if you have entity object it should be like below

    entity.GetAttributeValue<data type>("field name")

    Please download CRM SDK it has sample code or share full details here so that we can help you.

  • Rajkumar Rajaraman Profile Picture
    Rajkumar Rajaraman 18,108 on at
    RE: How I can retrieve values from fields?

    Refer this thread also:

    social.microsoft.com/.../how-to-get-field-values-in-plugin-on-create-post-event

  • Rajkumar Rajaraman Profile Picture
    Rajkumar Rajaraman 18,108 on at
    RE: How I can retrieve values from fields?

    LINQ and QueryExpression will only return attributes that have a value; if an attribute is null, then it won't be included in the Entity record.

    Refer this:

    msdn.microsoft.com/.../microsoft.xrm.sdk.messages.retrieveentityrequest.aspx

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans