簡體   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