Friday, June 30, 2017

Display Custom Metadata in the Advanced Search Web Part within SharePoint

                        string advancedUrl = newSearchWebSearch.Url + "/Pages/advanced.aspx";
                        SPFile advanceFile = newSearchWebSearch.GetFile(advancedUrl);
                        if (advanceFile.Exists)
                        {
                            advanceFile.CheckOut();
                            //Get Result search page
                            SPListItem wikiItem = advanceFile.Item;
                            string managePropertyname = strSubjectAreaName + getPropertyType(newSearchWeb, strSubjectAreaName), managePropertytype = getPropertySearchType(newSearchWeb, strSubjectAreaName), managePropertyDisplayName= strSubjectAreaName;


                            SPLimitedWebPartManager advsearchwebPartManager = newSearchWebSearch.GetLimitedWebPartManager(advancedUrl, PersonalizationScope.Shared);
                            foreach (var wsp in advsearchwebPartManager.WebParts)
                            {
                                if (wsp is AdvancedSearchBox)
                                {
                                    var wpAdavance = wsp as AdvancedSearchBox;
                                    string SearchQuery = wpAdavance.Properties.ToString();
                                    XmlDocument xmlDocs = new XmlDocument();
                                    xmlDocs.LoadXml(SearchQuery);
                                    XmlNodeList category1 = xmlDocs.SelectNodes("/root/r");
                                    XmlNodeList category = xmlDocs.SelectNodes("/root/PropertyDefs");
                                    XmlNodeList resulttype = xmlDocs.SelectNodes("/root/ResultTypes/ResultType");

                                    var newProperty = xmlDocs.CreateNode(XmlNodeType.Element, "PropertyDef", "");


                                    var property_attribute4 = xmlDocs.CreateAttribute("Name");
                                    property_attribute4.Value = managePropertyname;
                                    var property_attribute1 = xmlDocs.CreateAttribute("DataType");
                                    property_attribute1.Value = managePropertytype;
                                    var property_attribute2 = xmlDocs.CreateAttribute("DisplayName");
                                    property_attribute2.Value = managePropertyDisplayName;

                                    newProperty.Attributes.Append(property_attribute4);
                                    newProperty.Attributes.Append(property_attribute1);
                                    newProperty.Attributes.Append(property_attribute2);
                                    foreach (XmlNode node in category)
                                    {
                                        if (node.Name == "PropertyDefs")
                                        {
                                            category[0].AppendChild(newProperty);
                                        }
                                    }
                                    for (int j = 0; j < resulttype.Count; j++)
                                    {
                                        var Result_newInnerNode = xmlDocs.CreateNode(XmlNodeType.Element, "PropertyRef", "");
                                        var result_attribute = xmlDocs.CreateAttribute("Name");
                                        result_attribute.Value = managePropertyname;
                                        Result_newInnerNode.Attributes.Append(result_attribute);
                                        resulttype[j].AppendChild(Result_newInnerNode);
                                    }
                                    wpAdavance.Properties = xmlDocs.InnerXml;
                                    advsearchwebPartManager.SaveChanges(wpAdavance);
                                }
                            }
                            newSearchWebSearch.AllowUnsafeUpdates = true;
                            advanceFile.CheckIn("Publish", SPCheckinType.MajorCheckIn);
                            advanceFile.Publish("Publish");
                            _sitePage.Update();
                            newSearchWebSearch.Update();
                            newSearchWebSearch.AllowUnsafeUpdates = false;
                        }

                        //Based on Subject Seperate search Page

No comments:

Post a Comment