Tuesday, 13 May 2014

ArtcleCreation

 EntityReference kbArticleTemplate = new EntityReference();
                kbArticleTemplate.LogicalName = "kbarticletemplate";
                kbArticleTemplate.Id = kbArticleTemplateId;    //Give KB Article template id here, for whichever template you want to create your KB.

                //Subject
                EntityReference kbArticleSubject = new EntityReference();
                kbArticleSubject.LogicalName = "subject";
                kbArticleSubject.Id = articleSubjectGuid;        //Give Subject Id here

                kbArticle.Attributes.Add(new KeyValuePair<string, object>("kbarticletemplateid", kbArticleTemplate));
                kbArticle.Attributes.Add(new KeyValuePair<string, object>("title", articleTitle));
                kbArticle.Attributes.Add(new KeyValuePair<string, object>("subjectid", kbArticleSubject));
                kbArticle.Attributes.Add(new KeyValuePair<string, object>("description", "Some Description"));
               // kbArticle.Attributes.Add(new KeyValuePair<string, object>("keywords",));
                //kbArticle.Attributes.Add(new KeyValuePair<string, object>("content", kbContent));
                kbArticle.Attributes.Add(new KeyValuePair<string, object>("articlexml", articleXml));
                Guid newTranlatedRecordId = organizationService.Create(kbArticle);    //Create the KB Article



                //Entity article = new Entity("kbarticle");
                //article["title"] = articleTitle;
                //article["subjectid"] = new EntityReference("subjectid", articleSubjectGuid);
                //article["articlexml"] = articleXml;
                //organizationService.Create(article);
            }
            catch (Exception)
            {
                throw;
            }
        }

        private string ParseArticleXml(string articleXml)
        {
            try
            {
                string strArticleXml = articleXml;
                XDocument doc1 = XDocument.Parse(articleXml);

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(articleXml);
                XmlElement root = doc.DocumentElement;

                XmlNodeList nodeList = doc.DocumentElement.SelectNodes(@"/articledata/section/content");
                foreach (XmlNode node in nodeList)
                {
                    if (node.ChildNodes[0] is XmlCDataSection)
                    {
                        XmlCDataSection cdataSection = node.ChildNodes[0] as XmlCDataSection;
                        if (cdataSection != null)
                        {
                            string cdataText = cdataSection.Value;
                            if (!string.IsNullOrEmpty(cdataText))
                            {
                                string translatedText = TranslateData(cdataText, toLanguage, clientID, clientSecret, detectmethoduri, endpointaddress);
                                if (!string.IsNullOrEmpty(translatedText))
                                {
                                    strArticleXml = strArticleXml.Replace(cdataText, translatedText);
                                }
                            }
                        }
                    }
                }
                return strArticleXml;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        private static void InitializeTranslationParameters(IOrganizationService service)
        {
            QueryExpression qe = new QueryExpression();
            qe.EntityName = "new_crmbingtranslatorconfig";
            qe.ColumnSet = new ColumnSet();
            qe.ColumnSet.Columns.Add("new_entityname");
            qe.ColumnSet.Columns.Add("new_clientid");
            qe.ColumnSet.Columns.Add("new_clientsecret");
            qe.ColumnSet.Columns.Add("new_detectmethoduri");
            qe.ColumnSet.Columns.Add("new_endpointaddress");
            qe.ColumnSet.Columns.Add("new_outputlanguage");

            ConditionExpression ce = new ConditionExpression();
            ce.AttributeName = "new_entityname";
            ce.Operator = ConditionOperator.Equal;
            ce.Values.Add("MAIN_CONFIGURATION");

            FilterExpression fe = new FilterExpression();
            fe.Conditions.Add(ce);
            qe.Criteria.AddFilter(fe);

            EntityCollection ec = service.RetrieveMultiple(qe);
            foreach (Entity config in ec.Entities)
            {
                clientID = config["new_clientid"].ToString();
                clientSecret = config["new_clientsecret"].ToString();
                detectmethoduri = config["new_detectmethoduri"].ToString();
                endpointaddress = config["new_endpointaddress"].ToString();
                toLanguage = config["new_outputlanguage"].ToString();
            }
        }

No comments:

Post a Comment