开发者

Changing the Fixed Keyword Query in search results core web part with the current user, (Author:{CurrentUser})

开发者 https://www.devze.com 2023-02-20 00:10 出处:网络
Is there a way of changing the Fixed Keyword Query in the search results core web part to get the current user (Author:{CurrentUser})?

Is there a way of changing the Fixed Keyword Query in the search results core web part to get the current user (Author:{CurrentUser})?

I have played with scopes and filters and can do this fine with supplying the fixed keyword query but am not able to change it to always get the current user.

Basically I would like to have the FixedQuery field be author:Last First.

Here are a few posts which hint at it but I would like to just change it dynamically in the XSL in the .aspx file and be done with it. Anyone?

http://www.martinhatch.com/2010/07/rcwp-part-1-spretreat-and-related.html http:开发者_Go百科//www.novolocus.com/2008/05/14/using-a-query-string-parameter-in-the-search-results-web-part/


Ok so I have found a "work around" for this. I will try and lay it out as simple as possible.

  1. Create a new ASPX page which has a "Core Results Web Part" on it and has the "Cross-Web Part query ID" under "Results Query Options" in the properties set to "User query".
  2. We will call this page from inside a "Page View Web Part", the wep part which loads another page in an iframe. We will then add the query to the URL of the page which is being loaded in the "Page View Web Part". (e.g. site.com/ourAspxFromStep1.aspx?k=author:first%20last)

I hope this is clear. I added the web parts with c# server side code dynamically on page load based on values from a list so the queries are dynamically built on page load. Let me know if you have comments or questions.

Here is my client side function which I call which adds the web parts dynamically. core_wp_for_embed.aspx is the file mentioned above in step 1.

protected void refreshFeeds(string tmpUserName, SPUserToken userToken) {
  using(SPSite site = new SPSite("http://www.site.com")) {
    using(SPWeb web = site.OpenWeb()) {
      web.AllowUnsafeUpdates = true;
      SPFile file = web.GetFile(web.Url + "/currentPage.aspx");
      using(SPLimitedWebPartManager webPartManager =
        file.GetLimitedWebPartManager(PersonalizationScope.User)) {
        SPLimitedWebPartCollection webparts = webPartManager.WebParts;
        int j = webparts.Count;
        for (int k = j - 1; k >= 0; k--) {
          Microsoft.SharePoint.WebPartPages.WebPart wp =
            (Microsoft.SharePoint.WebPartPages.WebPart)webparts[k];
          if (wp.GetType().ToString() ==
"Microsoft.SharePoint.WebPartPages.PageViewerWebPart") {
            webPartManager.DeleteWebPart(wp);
          }
        }
        // Zone should be cleared. Now loop through list on users site and add web parts for each item
        SPSite site2 = new SPSite("http://www.site.com/personal/" + tmpUserName);
        SPWeb web2 = site2.OpenWeb();
        SPList list = web2.Lists["SomeUserList"];
        int i = 0;
        foreach(SPListItem currentItem in list.Items) {
          if(Convert.ToBoolean(currentItem["BooleanField"]) == true) {
            PageViewerWebPart pvwp = new PageViewerWebPart();
            pvwp.Title = currentItem["Title"].ToString();
            pvwp.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
            pvwp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly;
            pvwp.ContentLink = "http://www.site.com/core_wp_for_embed.aspx?k=scope:"
              + currentItem["Item_Scope"].ToString();
            pvwp.AllowEdit = true;
            webPartManager.AddWebPart(pvwp, "someZone", i);
            webPartManager.SaveChanges(pvwp);
            i++;
          }
        }
      }
      web.Update();
      web.AllowUnsafeUpdates = false;
    }
  }
  Response.Redirect(Request.RawUrl);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号