How to do this with a fast method?
I'm building a webpart for SharePoint 2010 that itereates through the whole site collection of a SharePoint site, and for each file within a document library it will save a value for each user registered at that site. Just for the record, there are over a hundred of users.
So far, I've tried the following code:
public static void CalculateValues(MyDataContext db, string siteUrl)
{
SPWebCollection webCollection = new SPSite(siteUrl).AllWebs;
Guid docLibFeatId = new Guid("00bfea71-e717-4e80-aa17-d0c71b360101");
List<int> user_ids = (from u in db.Users
select u.Id).ToList();
foreach (SPWeb web in webCollection)
{
foreach (SPList list in docLibraryColl)
{
if (list.TemplateFeatureId == docLibFeatId && !list.Hidden)
{
SPDocumentLibrary docLib = (SPDocumentLibrary)list;
SPFolder root = docLib.RootFolder;
TrackingUtility.GetFolderNode(db, user_ids, web, root);
}
}
web.Dispose();
}
}
public static List<SPFolder> GetFoldersInFolder(CogitoRecommendationsDataContext db, SPFolder folder)
{
List<SPFolder> result = new List<SPFolder>();
SPFolderCollection subFo开发者_StackOverflow社区lders = folder.SubFolders;
foreach (SPFolder subFolder in subFolders)
{
result.Add(subFolder);
}
return result;
}
public static void GetFolderNode(CogitoRecommendationsDataContext db, List<int> user_ids, SPWeb web, SPFolder folder)
{
List<SPFolder> folders = GetFoldersInFolder(db, folder);
for (int j = 0; j <= folders.Count - 1; j++)
{
SPFolder folderNode = folders[j];
foreach (var id in user_ids)
{
SPUser spUser = web.AllUsers.GetByID(id);
SaveValue(db, spUser, web, folderNode.UniqueId, SPAuditItemType.Folder);
}
SPFolder subfolder = folder.SubFolders[j];
GetFolderNode(db, user_ids, web, subfolder);
}
}
So, when I execute the CalculateValues
method, it takes too long for it to finish, and sometimes it doesn't even finish, because it throws an OutOfMemory exception
(maybe there's some undisposed object?).
Does anyone know about a faster way for doing this? I really don't know what else to try.
You won't be able to make this function run quicker if you have to access each item individually.
Your best bet is to leverage SharePoint's search engine. This would give you a fast index into all the documents and the values you need to pull. For each document value you need to calculate, add a managed metadata property in central admin.
The only downside is this won't provide up-to-the-minute results.
精彩评论