2010-06-22 22 views
1

我終於冒險從PowerShell的土地到C#的SharePoint東西。我創建了一個web部件,我將稍微介紹一下,該部件在我的開發服務器上完美工作,但運行速度慢。不同的是,我的生產服務器有1400多個組,而開發只有20多個組。Webpart訪問羣組的大型列表

我的webpart是爲解決常見問題而創建的:用戶必須點擊太多時間才能訪問其項目。我有一個文檔庫,有12個主要類別,下面有許多子文件夾。每個子文件夾大部分等於一個實際的SharePoint組。對於每個主類別,我得到一個所有子文件夾的數組,然後循環查看該組是否存在。現在,在我的代碼中,我知道是什麼讓它變慢。

有沒有更簡單的方法來查看是否存在一個組,而不是每次輪詢整個組列表?我可能會投票這個名單約650次。

這裏是我的代碼:

protected override void RenderWebPart(HtmlTextWriter output) 
{ 
    try 
    { 
     #region Connect to the Current Site 
     using (SPSite siteCollection = SPContext.Current.Site) 
     { 
      #region Connect to the tab we want to have this done on 
      using (SPWeb oWebsite = siteCollection.OpenWeb("Downloads")) 
      { 
       #region Find all the items in the list 
       foreach (SPListItem item in oWebsite.GetList(oWebsite.ServerRelativeUrl + "/My Files").GetItems(new SPQuery())) 
       { 
        string type = Convert.ToString(item["Type"]); 
        string name = Convert.ToString(item["Name"]); 

        #region If this is a legit folder 
        if ((String.IsNullOrEmpty(type)) && (name != "Archived") && (name != "Other")) 
        { 
         #region Get all the sub folders for each main folder 
         foreach (SPFolder SFolder in item.Folder.SubFolders) 
         { 
          #region Then get a list of all groups in SharePoint 
          foreach (SPGroup part in oWebsite.Groups) 
          { 
           #region Then check to see if there is a group with the same name as the folder 
           if (SFolder.Name == part.Name) 
           { 
            #region Then check to see if the user is in that group 
            foreach (SPUser vUser in part.Users) 
            { 
             string redirectURL = oWebsite.Url + "/My FIles/" + item.Name + "/" + part.Name; 
             string QSURL = oWebsite.Url + "/My Files/" + item.Name; 
             bool IsOwner = oWebsite.AssociatedOwnerGroup.ContainsCurrentUser; 
             bool IsQueryStringNull = String.IsNullOrEmpty(this.Page.Request.QueryString["RootFolder"]); 

             if ((oWebsite.CurrentUser.ID == vUser.ID) && (IsQueryStringNull)) 
             { 
              if (IsOwner) { output.Write("If you were not an admin you would be redirected to:" + redirectURL + "<br>"); } 
              else { this.Page.Response.Redirect(redirectURL, true); } 
             } 

             else if ((oWebsite.CurrentUser.ID == vUser.ID) && (!IsQueryStringNull) && (!IsOwner)) 
             { 
              if (QSURL == this.Page.Request.QueryString["RootFolder"]) { this.Page.Response.Redirect(redirectURL, true); } 
              else if (redirectURL == this.Page.Request.QueryString["RootFolder"]) { output.Write("User is at the right place :)<br>"); } 
              else { output.Write("Not Redirecting users: QS is not empty<br>"); } 
             } 
            } 
            #endregion 
           } 
           #endregion 
          } 
          #endregion 
         } 
         #endregion 
        } 
        #endregion 
       } 
       #endregion 
      } 
      #endregion 
     } 
     #endregion 
    } 
    catch (Exception ex) 
    { 
     output.Write("ERROR: " + ex); 
    } 
} 

回答

1

網上找到這一個。

public static bool GroupExists(SPGroupCollection groups, string name) 
{ 
    if (string.IsNullOrEmpty(name) || (name.Length > 255) || (groups == null) || (groups.Count == 0)) 
    { 
     return false; 
    } 
    else 
    { 
     return (groups.GetCollection(new String[] { name }).Count > 0); 
    } 
} 

它看起來更有效,但我將無法在20分鐘內,以測試這,直到一個自動化的過程結束。

+1

SharePoint(2007,尚不確定2010年左右)沒有OTB方式檢查組是否存在。你有的代碼片段看起來是一個很好的方法 – 2010-06-22 03:46:36