繁体   English   中英

如何优化此代码以创建文档库

[英]How to optimize this code to create document libraries

这是sharepoint代码,但我知道c#开发人员会理解它。

我现在无法想出一种方法来优化它。 我们的想法是基于事件的创建创建文档库。文档库的名称是某种格式的开始日期+事件标题。

问题是当用户在同一天制作具有相同标题的许多事件时。 我用一个IF做了它只复制了一次。 但应该有另一种更好的方法来做到这一点。

想法是在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();
                        }
                    }
                }
            }
        }

在伪代码中:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM