[英]How to optimize this code to create document libraries
This is sharepoint code, but I know c# developers would understand it. 这是sharepoint代码,但我知道c#开发人员会理解它。
I cant think a way right now to optimize it. 我现在无法想出一种方法来优化它。 The idea is to create a document library based on the creation of an event, The name of the document library is the startdate in some format + the event title.
我们的想法是基于事件的创建创建文档库。文档库的名称是某种格式的开始日期+事件标题。
The problem is when the user makes many events, the same day, with the same title. 问题是当用户在同一天制作具有相同标题的许多事件时。 I did it with an IF for only one occurence of the duplication.
我用一个IF做了它只复制了一次。 But there should be another better way to do it.
但应该有另一种更好的方法来做到这一点。
The idea is to concatenate a number at the end of the doc library /1/2/3 etc. 想法是在doc library / 1/2/3等结尾处连接一个数字。
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();
}
}
}
}
}
In pseudo-code: 在伪代码中:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.