views:

22

answers:

1

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);
    }
}
+1  A: 

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.

Mitchell Skurnik
SharePoint (2007, not sure yet about 2010) does not have a OTB way to check if a group exists. The code snippet you have looks like a good approach
John Ptacek