我终于冒险从PowerShell的土地转向C#的SharePoint东西。我创建了一个webpart,我稍后将对其进行描述,它在我的开发服务器上运行得很好但在我的生产上运行缓慢。不同之处在于我的生产服务器有超过1400个组,而开发只有20个左右。
创建我的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);
}
}
答案 0 :(得分: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分钟内完成自动化过程。