代码之家  ›  专栏  ›  技术社区  ›  Mitchell Skurnik

访问大量组列表的Web部件

  •  1
  • Mitchell Skurnik  · 技术社区  · 16 年前

    创建我的Web部件是为了解决一个常见问题:用户必须单击太多次才能访问他们的项目。我有一个文档库,它有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 回复  |  直到 16 年前
        1
  •  1
  •   Mitchell Skurnik    16 年前

    在网上找到这个。

    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分钟内完成。