Saturday 28 June 2014

SharePoint external List data from BDC read using c#

string txt = "Fieldvalue";

                string url = SPContext.Current.Site.Url.ToString();
                SPUserToken token = SPUserToken.SystemAccount;
                using (SPSite osite = new SPSite(url, token))
                {
                    using (SPWeb oweb = osite.OpenWeb())
                    {
                        BdcServiceApplicationProxy proxy = (Microsoft.SharePoint.BusinessData.SharedService.BdcServiceApplicationProxy)SPServiceContext.Current.GetDefaultProxy(typeof(Microsoft.SharePoint.BusinessData.SharedService.BdcServiceApplicationProxy));
                        DatabaseBackedMetadataCatalog catalog = proxy.GetDatabaseBackedMetadataCatalog();
                        IEntity ectResource = catalog.GetEntity(url, Name);
                   
                        foreach (KeyValuePair<string, IMethod> method in ectResource.GetMethods())
                        {
                            IMethodInstance methodInstance = method.Value.GetMethodInstances()[method.Key];
                     
                       
                            if (methodInstance.MethodInstanceType == MethodInstanceType.SpecificFinder)
                            {
                                Identity identity = new Identity(txt);
                                IEntityInstance entInstance = ectResource.FindSpecific(identity, ectResource.GetLobSystem().GetLobSystemInstances()[0].Value);

                                if (entInstance[Const.ITEM_NO].ToString() == txt)
                                {
                                    if (ddlBrandName.Items.Count > 0)
                                    {
                                        ddlBrandName.Items.Clear();
                                    }
                                    ddlBrandName.Items.Add(new ListItem(entInstance[Const.BRAND_DESCRIPTION].ToString()));
                                    txtSameAsDesc.Text = entInstance[Const.ITEM_DESC].ToString();
                                    txtSize.Text = entInstance[Const.PACK_SIZE].ToString();
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }