Thursday, February 20, 2014

Appending to User Query in SP2010 CoreResults WebPart


How does one Append to the Query in CoreResults WebPart


Difference between FixedQuery and AppendedQuery
Fixed Query - Overrides the user Query... Might be useful in cases where search is to be performed automatically without User Input or through Bots
Appended Query - More commonly used. For appending to the user query


ConfigureDataSourceProperties

      protected override void ConfigureDataSourceProperties()
        {

            try
            {
                string curreURL = SPContext.Current.Site.Url;

                string searchTerm = Page.Request.QueryString["k"].ToString();

                if (curreURL.ToLower().Contains("uk"))
                {
                    this.AppendedQuery = "dtturl:\"uk\"";
                }
               
                base.ConfigureDataSourceProperties();

                CoreResultsDatasource dataSource = this.DataSource as CoreResultsDatasource;
 
            }
            catch (Exception ex)
            {
                Trace.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + " - " + ex.Message);
            }
            CloseTraceFile();
        }

Modifying Results Post Query in SharePoint 2010 CoreResults WebPart


One of the scenarios we encountered was to enhance the search results post Query


Fortunately Core Results WebPart has a provision to do so in its GetXPathNavigator method

Sorry for Pasting code as is ...Will post the formatted code snippet soon.. 


        protected override XPathNavigator GetXPathNavigator(string viewPath)
        {
           
            XmlDocument xDocument = new XmlDocument();
           

            try
            {
                QueryManager queryManager = SharedQueryManager.GetInstance(Page).QueryManager;
                xDocument = queryManager.GetResults(queryManager[0]);

                //Annotation Request
               
                CustomEnhancemer customEnhancerObject = MakeCalltoWCF.EnhancemyResults(xDocument);
                XmlNode xmlNodeSettings = xDocument.SelectSingleNode("All_Results/Result");

                XmlElement xmlNodeCustomSettings = xDocument.CreateElement(customEnhancerObject.Title);

                xmlNodeCustomSettings.SetAttribute(customEnhancerObject.Key1, customEnhancerObject.Value1);
                xmlNodeCustomSettings.InnerText = customEnhancerObject.InnerText;

                xmlNodeSettings.AppendChild(xmlNodeCustomSettings);
                //xDocument.Save()
                Trace.WriteLine(" - XmLDocument: " + xDocument.OuterXml);
                return xDocument.CreateNavigator();

            }

            catch (Exception ex)
            {
                Trace.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + " -In Xpath Exception: " + ex.Message);
            }

            finally
            {
                CloseTraceFile();
            }

            return xDocument.CreateNavigator();
        
        }

Create SharePoint 2013 Custom Content Type using Visual Studio


Add a Content Type Item in the SharePoint Project
















<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Contact (0x0106) -->
  <ContentType ID="0x0106008E38D5B2B38B4417A0B35663615FAEF7" Name="RetailContact_Decl" Group="Custom Content Types" Description="My Content Type" Inherits="TRUE" Version="0">
    <FieldRefs>
      <FieldRef ID="{F55DE332-074E-4e71-A71A-B90ABFAD51AE}" DisplayName="$Resources:core,Assistants_Phone_OL;" Name="AssistantNumber" Sealed="TRUE" />
      <FieldRef ID="{7546ad0d-6c33-4501-b470-fb3003ca14ba}" DisplayName="$Resources:cmscore,column_contactname_displayname;" Required="FALSE" Description="$Resources:cmscore,column_contactname_description;" Name="PublishingContactName" Sealed="TRUE" />
    </FieldRefs>
  </ContentType>
</Elements>





Specify Parent Content type



















Add new columns as required










Deploy the solution








The feature should be automatically activated.. You should be able to see your Content Type in the target Site Collection