2012-12-23 29 views
1

我試圖解決許多文檔存儲在不同的SharePoint站點(不一定在同一臺服務器上,這意味着它們不是同一站點的子站點)的問題。是否有可能從網站獲得結果而無法訪問?

我正在考慮創建某種自定義SharePoint搜索,但我的問題是這樣的 - 是否有可能無法訪問特定SharePoint站點的用戶從該站點獲取搜索結果?

這意味着如果他們搜索「文檔X」,並且該文檔位於網站上,則他們沒有權限,我希望他們在搜索結果頁面上看到該文檔位於那裏,但仍然沒有讓他們在沒有獲得許可的情況下訪問它(只是看它是否存在)。

非常感謝。

回答

0

下面是如何模擬特定用戶並將用戶看到的搜索結果返回給所有用戶的示例。不用說,這當然是一個安全風險,所以謹慎使用:

using System; 
using System.ComponentModel; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.WebControls; 
using System.Data; 
using System.Security.Principal; 

using Microsoft.Office.Server.Search.Administration; 
using Microsoft.Office.Server.Search.Query; 
using Microsoft.SharePoint.Administration; 

namespace SharePointSearchImpersonation.WebPart1 
{ 
    [ToolboxItemAttribute(false)] 
    public class QuerySearchImpersonatedWebPart : WebPart 
    { 
     protected override void CreateChildControls() 
     { 
      try 
      { 
       // Run elevated since the user, the apppool account, that impersonate other users needs the following 
       // local policies (run secpol.msc): "Impersonate a client after authentication" & "Act as part of the operating system" 
       // The apppool account must be granted above rights. 
       SPSecurity.RunWithElevatedPrivileges(
        delegate 
         { 
          // Setup the windows identity to impersonate the search as. 
          WindowsIdentity identity = new WindowsIdentity("[email protected]"); 
          // Create a new ImpersonationContext for the identity 
          using (WindowsImpersonationContext wic = identity.Impersonate()) 
          { 
           SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>(); 
           // Get the search proxy by service application name 
           SearchServiceApplicationProxy searchProxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("Search Service Application"); 
           KeywordQuery query = new KeywordQuery(searchProxy); 
           query.ResultTypes = ResultType.RelevantResults; 
           query.QueryText = "test"; 
           query.RowLimit = 25; 
           ResultTableCollection result = query.Execute(); 
           DataTable dt = new DataTable(); 
           dt.Load(result[ResultType.RelevantResults]); 

           GridView gv = new GridView(); 
           Controls.Add(gv); 
           gv.AutoGenerateColumns = true; 
           gv.DataSource = dt; 
           gv.DataBind(); 

          } 
         }); 
      } 
      catch (Exception ex) 
      { 
       Controls.Add(new LiteralControl() {Text = ex.ToString()}); 
      } 
     } 
    } 
}