访问大量组的 Web 部件

发布于 2024-09-06 08:41:27 字数 4185 浏览 3 评论 0原文

我终于冒险离开了 PowerShell 领域,转向了 SharePoint 的 C# 内容。我创建了一个 Web 部件,我将稍后对其进行描述,它在我的开发服务器上完美运行,但在我的生产中运行缓慢。不同之处在于,我的生产服务器有超过 1400 个组,而开发服务器只有 20 个左右。

创建我的 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);
    }
}

I have finally ventured away from the land of PowerShell to C# stuff for SharePoint. I created a webpart, which I will describe in a bit, that works perfectly on my development server but runs slow on my production. The difference being is that my production server has over 1400 groups while the development has only 20 or so.

My webpart was created to solve a common problem: A user has to click too many times to access their items. I have a document library that has 12 main categories with many sub folders underneath that. Each subfolder for the most part equals an actual SharePoint group. For each main category, I get an array of all the subfolders and then loop through to see if if that group exists Now in my code below I know what is slowing it down.

Is there an easier way to see if a group exists rather than polling the entire group list every time? I could potentially be polling that list about 650 times.

Here is my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独木成林 2024-09-13 08:41:27

在网上找到了这个。

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

Found this one online.

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

It looks more efficient but I wont be able to test this untill an automated process finishes in 20 minutes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文