2017-02-09 24 views
0

當刪除列表中得到錯誤「列表不存在」在某些情況下。我的順序是:刪除列表中不工作:「目錄不存在」

try { deleteList("myList"); } catch { } // delete list in case it exists 
createList("myList"); 
// Do something with the list. Does not matter 
deleteList("myList"); 

如果列表不存在的時候我運行代碼一切正常。如果列表存在,我會得到錯誤。當我瀏覽代碼時,我可以看到(在瀏覽器中)列表真的被刪除並重新創建。

當我創建createList()和deleteList(之間的新clientContext),那麼它的工作原理。我假設clientContext中的某些東西已經搞亂了。

我不認爲這與我的功能,但在任何情況下做的,在這裏,他們是:

public void CreateList(string title) 
    { 
     ListCreationInformation lci = new ListCreationInformation(); 
     lci.Title = title; 
     lci.TemplateType = (int)ListTemplateType.GenericList; 
     List spList = context.Web.Lists.Add(lci); 
     context.Load(spList); 
     spList.Update(); 
     context.ExecuteQuery(); 
    } 

    public void DeleteList(string listname) 
    { 
     List spList = context.Web.Lists.GetByTitle(listname); 
     spList.DeleteObject(); 
     context.ExecuteQuery(); 
    } 

回答

0

@Johannes我就說說哪里哪里名單已經存在,你的代碼失敗的情況下。

  1. 首先你ClientContext擁有所有列表,包括您的列表的信息:myList中
  2. 然後調用函數deleteList( 「myList中」);這將刪除您的列表沒有任何問題。
  3. 但是,當你嘗試重新建立與老ClientContext同名的名單,就會報錯,因爲它的SharePoint狀態的信息,你刪除列表中。

因此由於上下文不一致,就會報錯了。所以,正如你所提到的,只需刪除「myList」後創建新的ClientContext,它就可以正常工作。

+0

感謝這個確認。我真的很感激。 是否有可能在不重新創建客戶端上下文的情況下執行此操作通過清除客戶端上下文中的存儲狀態。 –

+0

來自像所有的功能刪除的executeQuery():CreateList(字符串名稱)和DeleteList(字符串LISTNAME),並在你的代碼的結束一次調用它。不知道它是否會起作用。 – Vaibhav