C# 如何优化此代码以创建文档库
这是sharepoint代码,但我知道c#开发者会理解它 我现在想不出优化它的方法。 其思想是在创建事件的基础上创建一个文档库,文档库的名称是某种格式的startdate+事件标题 问题在于,当用户在同一天以相同的标题制作许多事件时。 我用IF做了这件事,因为只发生了一次重复。但应该有另一种更好的方法来做到这一点 其思想是在文档库/1/2/3等的末尾连接一个数字C# 如何优化此代码以创建文档库,c#,algorithm,sharepoint,sharepoint-2010,C#,Algorithm,Sharepoint,Sharepoint 2010,这是sharepoint代码,但我知道c#开发者会理解它 我现在想不出优化它的方法。 其思想是在创建事件的基础上创建一个文档库,文档库的名称是某种格式的startdate+事件标题 问题在于,当用户在同一天以相同的标题制作许多事件时。 我用IF做了这件事,因为只发生了一次重复。但应该有另一种更好的方法来做到这一点 其思想是在文档库/1/2/3等的末尾连接一个数字 using (SPSite oSPSite = new SPSite(SiteUrl)) {
using (SPSite oSPSite = new SPSite(SiteUrl))
{
using (SPWeb oSPWeb = oSPSite.RootWeb)
{
if (oSPWeb.Lists[DocumentLibraryName] == null)
{
Guid ID = oSPWeb.Lists.Add(DocumentLibraryName, DocumentLibraryName + System.DateTime.Now.ToString(), SPListTemplateType.DocumentLibrary);
SPList oSPList = oSPWeb.Lists[ID];
DocumentLibraryLink = oSPList.DefaultViewUrl;
oSPList.OnQuickLaunch = false;
oSPList.Update();
}
else
{
if (oSPWeb.Lists[DocumentLibraryName + "/1"] == null)
{
Guid ID = oSPWeb.Lists.Add(DocumentLibraryName + "/1", DocumentLibraryName + System.DateTime.Now.ToString(), SPListTemplateType.DocumentLibrary);
SPList oSPList = oSPWeb.Lists[ID];
DocumentLibraryName = DocumentLibraryName + "/1";
DocumentLibraryLink = oSPList.DefaultViewUrl;
oSPList.OnQuickLaunch = false;
oSPList.Update();
}
}
}
}
}
在伪代码中:
string docLibNameBase ="myLibname";
string docLibNameTemp = docLibNameBase; //we start with the calculated title
int iCounter = 1;
//we check if the currently calculated title is OK
while (listExists(docLibNameTemp, yourWeb)) {
docLibNameTemp = docLibNameBase + "/" + iCounter.toString();
}
//this is where you create the new list using docLibNameTemp as a good title
bool listExists(string docLibName, SPWeb web){
try {
//if there is no list with such name, it will throw an exception
return (web.Lists[docLibname]!=null);
} catch{
return false;
}
}
你说“问题是当用户在同一天用同一个标题制作了许多活动”这是什么意思?使用相同名称创建的文档库?是!保存同名文档库所需的同名文档库?这是避免重复文档库名称所需的。是的,您更改了创建文档库的代码。因此,创建的文档库具有唯一的名称。