C# 访问大量组列表的Web部件
我终于从PowerShell之地冒险到了SharePoint的C#stuff。我创建了一个webpart,我将在稍后介绍它,它在我的开发服务器上运行得很好,但在我的产品上运行得很慢。不同之处在于,我的生产服务器有1400多个组,而开发服务器只有20个左右 创建我的Web部件是为了解决一个常见问题:用户必须单击太多次才能访问其项目。我有一个文档库,它有12个主要类别,下面有许多子文件夹。大多数情况下,每个子文件夹都相当于一个实际的SharePoint组。对于每个主要类别,我都会得到一个包含所有子文件夹的数组,然后循环查看下面的代码中是否存在该组,我知道是什么让它变慢了 有没有比每次轮询整个组列表更容易查看组是否存在的方法?我可能会对这个名单进行650次的投票 这是我的密码:C# 访问大量组列表的Web部件,c#,sharepoint,web-parts,C#,Sharepoint,Web Parts,我终于从PowerShell之地冒险到了SharePoint的C#stuff。我创建了一个webpart,我将在稍后介绍它,它在我的开发服务器上运行得很好,但在我的产品上运行得很慢。不同之处在于,我的生产服务器有1400多个组,而开发服务器只有20个左右 创建我的Web部件是为了解决一个常见问题:用户必须单击太多次才能访问其项目。我有一个文档库,它有12个主要类别,下面有许多子文件夹。大多数情况下,每个子文件夹都相当于一个实际的SharePoint组。对于每个主要类别,我都会得到一个包含所有子文
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);
}
}
protected override void RenderWebPart(HtmlTextWriter输出)
{
尝试
{
#区域连接到当前站点
使用(SPSite siteCollection=SPContext.Current.Site)
{
#区域连接到我们要在其上完成此操作的选项卡
使用(SPWeb oWebsite=sitecolection.OpenWeb(“下载”))
{
#区域查找列表中的所有项目
foreach(oWebsite.GetList(oWebsite.ServerRelativeUrl+“/My Files”).GetItems(new SPQuery())中的SPListItem项)
{
字符串类型=Convert.ToString(项[“类型]);
字符串名称=Convert.ToString(项[“名称”]);
#区域,如果这是合法文件夹
if((String.IsNullOrEmpty(type))&&&(name!=“存档”)&&(name!=“其他”))
{
#区域获取每个主文件夹的所有子文件夹
foreach(项目.文件夹.子文件夹中的SPFolders文件夹)
{
#然后获取SharePoint中所有组的列表
foreach(oWebsite.Groups中的SPGroup部分)
{
#区域,然后检查是否有与文件夹同名的组
if(SFolder.Name==part.Name)
{
#然后检查用户是否在该组中
foreach(部分为SPUser vUser.Users)
{
字符串重定向Url=oWebsite.Url+“/My FIles/”+item.Name+“/”+part.Name;
字符串QSURL=oWebsite.Url+“/myfiles/”+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(“如果您不是管理员,您将被重定向到:“+redirectURL+”
”);}
else{this.Page.Response.Redirect(redirectURL,true);}
}
如果((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(“用户在正确的位置:)
”;}
else{output.Write(“不重定向用户:QS不为空
”;}
}
}
#端区
}
#端区
}
#端区
}
#端区
}
#端区
}
#端区
}
#端区
}
#端区
}
捕获(例外情况除外)
{
输出写入(“错误:+ex”);
}
}
在网上找到了这个
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分钟内完成之前,我无法对其进行测试。在网上找到了这个
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分钟内完成之前,我无法对此进行测试。SharePoint(2007年,还不确定2010年)没有OTB方法来检查组是否存在。您拥有的代码片段看起来是一个很好的方法SharePoint(2007,还不确定2010)没有OTB方法来检查组是否存在。您拥有的代码片段看起来是一种很好的方法