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();

1 Answer 1

0

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

4
  • ContentBySearchWebPart Doesnt exist in this namespace !
    – Rosengord
    Nov 13, 2014 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, 2014 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 Nov 13, 2014 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, 2014 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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