0

I trying to add a Content serach webpart to a page by Activation feature. But im getting wrong web part when trying i want Content serach part to be able to query content from other parts in the site collection

using (SPWeb web = siteCollection.RootWeb)
        {
            var file = web.GetFile(PageUrl);
            var prevAllowUnsafe = web.AllowUnsafeUpdates;
            web.AllowUnsafeUpdates = true;
            string quey = "path"+ query ;

                using (var webMngr = web.GetLimitedWebPartManager(file.Url, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
            {
            // this is wrong ..... I want Content search web part not query
               ContentByQueryWebPart contentByQueryWebPart = new ContentByQueryWebPart();
                contentByQueryWebPart.Title = "Test test";

                contentByQueryWebPart.QueryOverride = quey;
                webMngr.AddWebPart(contentByQueryWebPart, WebPartZoneId, 0);

                webMngr.SaveChanges(contentByQueryWebPart);
                web.Update();
0

You need to use ContentBySearchWebPart class which is present in Microsoft.Office.Server.Search.WebControls namespace and Microsoft.Office.Server.Search dll.

  • ContentBySearchWebPart Doesnt exist in this namespace ! – Rosengord Nov 13 '14 at 9:52
  • Added these things using Microsoft.Office.Server; using Microsoft.Office.Server.Search; using Microsoft.Office.Server.WebControls; But still cannot find ContentBySearchWebPart – Rosengord Nov 13 '14 at 9:57
  • See my updated answer. The dll should be Microsoft.Office.Server.Search. You can find it here: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.Office.Server.Search.dll – Nadeem Yousuf Nov 13 '14 at 10:19
  • You should add a reference to Microsoft.Office.Server.Search.dll assembly, then using Microsoft.Office.Server.Search.WebControls; (see msdn.microsoft.com/en-us/library/…) – pholpar Nov 13 '14 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.